标签: zipfile

保留ZipFile中的文件属性

我正在寻找一种方法来保存写入zipfile.ZipFile实例的文件的文件属性(例如只读).

我添加到zip存档的文件会重置其文件属性,例如.在使用zip应用程序检查存档时以及解压缩后,只读标志已消失.

我当前的环境是Windows,我遇到了ZipInfo.external_attr方法的问题.

当然,写入时必须有一种标准的方法来保存文件属性ZipFile

python zip file zipfile

5
推荐指数
1
解决办法
3708
查看次数

奇怪的"BadZipfile:错误的CRC-32"问题

此代码简化了Django应用程序中的代码,该应用程序通过HTTP多部分POST接收上传的zip文件,并对内部数据进行只读处理:

#!/usr/bin/env python

import csv, sys, StringIO, traceback, zipfile
try:
    import io
except ImportError:
    sys.stderr.write('Could not import the `io` module.\n')

def get_zip_file(filename, method):
    if method == 'direct':
        return zipfile.ZipFile(filename)
    elif method == 'StringIO':
        data = file(filename).read()
        return zipfile.ZipFile(StringIO.StringIO(data))
    elif method == 'BytesIO':
        data = file(filename).read()
        return zipfile.ZipFile(io.BytesIO(data))


def process_zip_file(filename, method, open_defaults_file):
    zip_file    = get_zip_file(filename, method)
    items_file  = zip_file.open('items.csv')
    csv_file    = csv.DictReader(items_file)

    try:
        for idx, row in enumerate(csv_file):
            image_filename = row['image1']

            if open_defaults_file:
                z = zip_file.open('defaults.csv')
                z.close()

        sys.stdout.write('Processed %d items.\n' % …
Run Code Online (Sandbox Code Playgroud)

zipfile bytesio stringio

5
推荐指数
1
解决办法
9723
查看次数

Libzip-从zip读取文件内容

我使用libzip处理zip文件,然后一切正常,直到需要从zip读取文件,我只需要读取整个文本文件,因此实现类似PHP的“ file_get_contents”功能非常好。
要从zip读取文件,有一个函数
“ int zip_fread(struct zip_file * file,void * buf,zip_uint64_t nbytes)”
主要问题是我不知道buf的大小必须为多少,我必须读取多少nbytes(好吧,我需要读取整个文件,但是文件大小不同)。我只是可以做一个大的缓冲区来适合他们所有并读取所有它的大小,或者做一个while循环直到fread返回-1,但是我认为这不是合理的选择。

c c++ zip zipfile

5
推荐指数
1
解决办法
6235
查看次数

Php创建zip文件(来自帖子附件wordpress)

我正在尝试从wordpress中的帖子附件创建一个zip文件.

我已尝试过以下两种方法 - 但它没有结果(没有错误消息,没有创建文件) - 我做错了什么(再次......)

我不认为那些是wordpress中的帖子附件的事实与它有关 - 因为那些方法也失败了普通文件.为什么? - >看到答案.

$files_to_zip = array();// create files array
    //run a query
    $post_id = get_the_id();
    $args = array(
        'post_type' => 'attachment',
        'numberposts' => null,
        'post_status' => null,
        'post_parent' => $post_id
    );

    $attachments = get_posts($args);
    if ($attachments) {
foreach ($attachments as $attachment) {
        $files_to_zip [] = wp_get_attachment_url( $attachment->ID ); // populate files array
        }
    }
    print_r($files_to_zip);
    $zip = new ZipArchive;
    $zip->open('file.zip', ZipArchive::CREATE);
    foreach ($files_to_zip as $file) {
      $zip->addFile($file);
    }
    $zip->close();
Run Code Online (Sandbox Code Playgroud)

还有这个方法:

$files_to_zip = …
Run Code Online (Sandbox Code Playgroud)

php wordpress zipfile ziparchive

5
推荐指数
1
解决办法
4415
查看次数

如何使用Python创建文件路径的zip文件,包括空目录?

我一直在尝试使用zipfileshutil.make_archive模块递归创建目录的zip文件.两个模块都运行良好 - 除了空目录不会添加到存档.还会以静默方式跳过包含其他空目录的空目录.

我可以使用7Zip创建相同路径的存档,并保留空目录.因此我知道在文件格式本身中这是可能的.我只是不知道如何在Python中做到这一点.有任何想法吗?谢谢!

python zip shutil zipfile

5
推荐指数
1
解决办法
6389
查看次数

zipfile.write():文件的相对路径,在zip归档文件中复制

使用zip文件,我指示文件位于另一个文件夹中,例如: './data/2003-2007/metropolis/Matrix_0_1_0.csv'

我的问题是,当我提取它时,文件位于中./data/2003-2007/metropolis/Matrix_0_1_0.csv,而我希望将其提取到中./

这是我的代码:

def zip_files(src, dst):
    zip_ = zipfile.ZipFile(dst, 'w')

    print src, dst

    for src_ in src:
        zip_.write(src_, os.path.relpath(src_, './'), compress_type = zipfile.ZIP_DEFLATED)

    zip_.close()
Run Code Online (Sandbox Code Playgroud)

这是src和dst的输出:

    ['./data/2003-2007/metropolis/Matrix_0_1_0.csv', './data/2003-2007/metropolis/Matrix_0_1_1.csv'] ./data/2003-2007/metropolis/csv.zip
Run Code Online (Sandbox Code Playgroud)

python path zipfile

5
推荐指数
2
解决办法
1万
查看次数

cURL下载Zip文件 - "参数不是有效的文件句柄资源"

我正在尝试从我托管的服务器下载一个zip文件,并使用PHP和cURL将其存储在另一台服务器上.我的PHP看起来像这样:

set_time_limit( 0 );
$ci = curl_init();
curl_setopt_array( $ci, array(
    CURLOPT_FILE    =>  '/directory/images.zip',                // File Destination
    CURLOPT_TIMEOUT =>  3600,                                   // Timeout
    CURLOPT_URL     => 'http://example.com/images/images.zip'   // File Location
) );
curl_exec( $ci );
curl_close( $ci );
Run Code Online (Sandbox Code Playgroud)

每当我运行这个时,我就会收到以下错误CURLOPT_URL:

警告:curl_setopt_array():提供的参数不是...中的有效文件句柄资源

如果我直接在浏览器中访问文件位置,则会下载.我是否需要传递某种标题信息,以便它知道它是一个zip文件?有什么方法可以调试吗?

php zip curl zipfile

5
推荐指数
1
解决办法
6577
查看次数

在Elixir中断言zip存档内容的最佳方法是什么?

目前我在做什么:

  1. 通过让它压缩文件/目录来测试功能.断言它存在.
  2. 使用:zip.t:zip.tt让它列出zip文件夹的内容,看看它是否是我所期待的.

不知怎的,我想我错过了一些东西.测试更好:zip.table吗?该功能看起来令人困惑.有人可以举例说明如何使用它吗?下面是我得到的输出示例,但我无法弄清楚如何将其变成测试?对于zip档案来说,md5sum是一个更好的测试吗?

iex(4)> :zip.table('testing.zip')
{:ok,
 [{:zip_comment, []},
  {:zip_file, 'mix.exs',
   {:file_info, 930, :regular, :read_write, {{2015, 7, 15}, {2, 11, 9}},
    {{2015, 7, 15}, {2, 11, 9}}, {{2015, 7, 15}, {2, 11, 9}}, 54, 1, 0, 0, 0, 0,
    0}, [], 0, 444},
  {:zip_file, 'mix.lock',
   {:file_info, 332, :regular, :read_write, {{2015, 7, 15}, {2, 9, 6}},
    {{2015, 7, 15}, {2, 9, 6}}, {{2015, 7, 15}, {2, 9, 6}}, 54, 1, 0, 0, 0, 0,
    0}, …
Run Code Online (Sandbox Code Playgroud)

erlang zip elixir zipfile

5
推荐指数
1
解决办法
582
查看次数

使用7-Zip压缩和删除同一文件

我编写了一个批处理文件(使用7-Zip),该文件将文件放在一个文件夹中,并将每个文件分别压缩到该批处理文件中创建的另一个文件夹中,然后从原始文件夹中删除这些文件。

我试图编写它以便将其压缩到同一文件夹中,但是由于我的上一条命令指出要从同一文件夹中删除文件,因此它将删除所有内容。

原始文件夹称为SmartLogger,新文件夹名为SmartLoggerZipped

我需要将文件压缩SmartLogger并保留在同一文件夹中,然后再删除该文件夹中的原始文件。

@echo off
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%
SET SrcDir=E:\Logs\SmartLogger
SET DestDir=E:\Logs\SmartLoggerZipped
IF NOT EXIST "%DestDir%" MD "%DestDir%"
ECHO.
ECHO Compressing files and folders in E:\Logs\SmartLogger
ECHO drive and moving to E:\Logs\SmartLoggerZipped and
ECHO then delete from E:\Logs\SmartLogger
ECHO.
FOR %%A IN ("%SrcDir%\*.*") DO 7za a -tzip "%DestDir%\%%~NXA.zip" "%%~A" -mx5 && DEL /Q /F "%%~A"
ECHO.
PAUSE
Run Code Online (Sandbox Code Playgroud)

windows 7zip batch-file zipfile

5
推荐指数
1
解决办法
4624
查看次数

在ZIP文件中获取目录条目

我正在编写一个Java程序,它给出了一个限定名(如java.lang.String:),从src.zipJDK中的文件中获取并返回相应的条目以供进一步处理.

到目前为止,我的程序适用于任何引用特定.java源文件的限定名称; 但是当限定名称引用整个包时(例如:),我遇到了麻烦java.util.*.在这种情况下,我希望我的程序返回给定包中所有条目的列表.

问题是似乎没有办法(有效地)使用java.util.zip.*包中提供的实用程序来做这样的事情!我已经尝试了两个ZipFile并且ZipInputStream没有人识别文件中的src.zip目录!他们只返回单个.java源文件的条目!

在代码语言中,两者都是:

ZipEntry entry;
ZipInputStream zip = new ZipInputStream(new FileInputStream("src.zip"));
while((entry = zip.getNextEntry()) != null) 
    System.out.println(entry.isDirectory());
Run Code Online (Sandbox Code Playgroud)

和:

Enumeration<? extends ZipEntry> zip = new ZipFile("src.zip").entries();
while (zip.hasMoreElements()) {
    ZipEntry entry = zip.nextElement();
    System.out.println(entry.isDirectory());
}
Run Code Online (Sandbox Code Playgroud)

总是回归虚假; 根本没有目录!

即使以下代码是无用的,只返回null(这意味着Not Found):

ZipFile zipfile = new ZipFile("src.zip");
zipfile.getEntry("java/util/");
Run Code Online (Sandbox Code Playgroud)

解决方法是使用上面列出的两次迭代中的任何一种,并对所需的条目执行详尽的搜索:

if (entry.getName().startsWith("java/util/"))
    System.out.println(entry);
Run Code Online (Sandbox Code Playgroud)

但显然这效率不高!有没有办法检索src.zip文件中的目录条目或有效地列出给定目录路径的条目?请注意,我想直接处理ZIP文件而不提取(出于显而易见的原因).


更新

Timothy Truckle的回答中可以 …

java zipfile

5
推荐指数
1
解决办法
2325
查看次数

标签 统计

zipfile ×10

zip ×5

python ×3

php ×2

7zip ×1

batch-file ×1

bytesio ×1

c ×1

c++ ×1

curl ×1

elixir ×1

erlang ×1

file ×1

java ×1

path ×1

shutil ×1

stringio ×1

windows ×1

wordpress ×1

ziparchive ×1