标签: zipfile

如何将Python StringIO()对象传递给ZipFile(),还是不受支持?

所以我有一个StringIO()类似文件的对象,我正在尝试将其写入a ZipFile(),但是我得到了这个TypeError:

coercing to Unicode: need string or buffer, cStringIO.StringI found
Run Code Online (Sandbox Code Playgroud)

以下是我正在使用的代码示例:

file_like = StringIO()
archive = zipfile.ZipFile(file_like, 'w', zipfile.ZIP_DEFLATED)

# my_file is a StringIO object returned by a remote file storage server.
archive.write(my_file)
Run Code Online (Sandbox Code Playgroud)

文档说这StringIO()是一个类文件类,ZipFile()可以接受类文件对象.有什么我想念的吗?任何帮助将不胜感激.

提前致谢!

python zipfile stringio

11
推荐指数
1
解决办法
6809
查看次数

更快地替代Python的zipfile模块?

是否有一个明显更快的替代Python 2.7.4 zipfile模块(使用ZIP_DEFLATED)将大量文件压缩成单个zip文件?我看了一下czipfile https://pypi.python.org/pypi/czipfile/1.0.0,但这似乎集中在更快的解密(不压缩)上.

我经常需要处理大量的图像文件(大约12,000个.exr和.tiff文件组合的文件),每个文件大小在1MB到6MB之间(所有文件大约为9GB)到一个文件中zip文件用于发货.这种压缩处理需要大约90分钟(在Windows 7 64位上运行).

如果有人可以推荐一个不同的python模块(或者一个C/C++库甚至是一个独立的工具),它能够在比zipfile模块更短的时间内将大量文件压缩成单个.zip文件,那就是非常感谢(任何接近〜5-10%更快(或更多)的东西都会非常有帮助).

python performance zipfile

11
推荐指数
1
解决办法
6813
查看次数

将文件添加到现有zipfile

我正在使用python的zipfile模块.
将zip文件放在以下路径中:
/home/user/a/b/c/test.zip
/home/user/a/b/c/1.txt 我想要将此文件添加到现有zip的情况下创建另一个文件时,我做了:
zip.write(os.path.basename('/home/user/a/b/c/1.txt'))

在解压缩文件时,所有子文件夹都出现在路径中,如何在没有路径的子文件夹的情况下输入zip文件?

我也尝试过: zipfile 并且得到了一个错误,该文件不存在,尽管它确实存在.

注意:我没有在路径中使用硬编码值,在本例中只是为了简化它.

python zipfile

11
推荐指数
2
解决办法
8734
查看次数

Python:将文件放入没有目录的存档中?

我现在已经学习python大约3个星期了,我现在正在尝试编写一个小脚本来按文件名中出现的关键字和日期对文件进行排序(大约10.000).应将给定日期之前的文件添加到存档中.排序工作正常,但不是归档

它创建一个存档 - 名称很好 - 但在存档中是文件的完整路径.如果我打开它,它看起来像:folder1 -> folder2 -> folder3 -> files.

如何更改它,使归档只包含文件而不是整个结构?

下面是一个带有我的zip功能的片段,node是排序前文件的路径,folder是一个子文件夹,其中的文件按名称中的关键字排序,items是按日期排序的文件夹.

我使用的是Python 2.6

def ZipFolder(node, zipdate):
    xynode = node + '/xy'
    yznode = node + '/yz'
    for folder in [xynode,yznode]:
        items = os.listdir(folder)
        for item in items:
            itemdate = re.findall('(?<=_)\d\d\d\d-\d\d', item)
            print item
            if itemdate[0] <= zipdate:
                arcname = str(item) + '.zip'
                x = zipfile.ZipFile(folder + '/' + arcname, mode='w', compression = zipfile.ZIP_DEFLATED)
                files = os.listdir(folder + '/' + …
Run Code Online (Sandbox Code Playgroud)

python zipfile

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

如何使用太长/重复的路径解压缩ZipFile

在Windows中解压缩文件时,我偶尔会遇到路径问题

  1. 这对于Windows来说太长了(但在创建该文件的原始操作系统中没问题).
  2. 由于不区分大小写,它们是"重复的"

使用DotNetZip时,ZipFile.Read(path)每当阅读带有这些问题之一的zip文件时,调用都会被废弃.这意味着我甚至无法尝试过滤掉它.

using (ZipFile zip = ZipFile.Read(path))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

处理阅读这些文件的最佳方法是什么?

更新:

来自此处的示例拉链:https: //github.com/MonoReports/MonoReports/zipball/master

重复:https: //github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DataSourceType.cs https://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DatasourceType的.cs

以下是有关异常的更多详细信息:

Ionic.Zip.ZipException:无法读取它作为ZipFile
---> System.ArgumentException:已添加具有相同键的>项目. System.ChrowArgumentException
(ExceptionResource资源)
at System.Collections.Generic.Dictionary 2.Add(TKey key,TValue value) at Ionic.Zip.ZipFile.ReadCentralDirectory(ZipFile zf) at Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile) ZF) 2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary


解析度:

根据@ Cheeso的建议,我可以从流中读取所有内容,避免重复内容和路径问题:

//using (ZipFile zip = ZipFile.Read(path))
using (ZipInputStream stream = new ZipInputStream(path))
{
    ZipEntry e;
    while( (e = stream.GetNextEntry()) != null )
    //foreach( ZipEntry e in zip)
    { …
Run Code Online (Sandbox Code Playgroud)

c# zipfile dotnetzip

10
推荐指数
2
解决办法
7424
查看次数

如何使用python的标准库zipfile检查zip文件是否加密?

我使用python的标准库zipfile来测试存档:

zf = zipfile.ZipFile(archive_name)
if zf.testzip()==None: checksum_OK=True
Run Code Online (Sandbox Code Playgroud)

我得到这个运行时异常:

File "./packaging.py", line 36, in test_wgt
    if zf.testzip()==None: checksum_OK=True
  File "/usr/lib/python2.7/zipfile.py", line 844, in testzip
    f = self.open(zinfo.filename, "r")
  File "/usr/lib/python2.7/zipfile.py", line 915, in open
    "password required for extraction" % name
RuntimeError: File xxxxx/xxxxxxxx.xxx is encrypted, password required for extraction
Run Code Online (Sandbox Code Playgroud)

如果zip是加密的,在运行testzip()之前如何测试?我没有发现捕获的异常会使这项工作变得更简单.

python encryption zip zipfile python-2.7

10
推荐指数
1
解决办法
4419
查看次数

正确解码zip条目文件名 - CP437,UTF-8或?

我最近写了一个名为zipzap的zip文件I/O库,但我正在努力正确解码任意zip文件中的zip条目文件名.

现在,PKWARE规范指出:

D.1 ZIP格式历史上只支持原始的IBM PC字符编码集,通常称为IBM Code Page 437 ...

D.2如果未设置通用位11,则文件名和注释应符合原始ZIP字符编码.如果设置了通用位11,则文件名和注释必须使用UTF-8存储规范定义的字符编码格式支持Unicode标准版本4.1.0或更高版本...

这意味着符合的zip文件将文件名编码为CP437,除非设置了EFS位,在这种情况下文件名为UTF-8.

不幸的是,似乎许多zip工具要么没有正确设置EFS位(例如Mac CLI,GUI zip),要么使用其他一些编码,通常是默认的系统编码(例如WinZip?).如果您知道WinZip,7-Zip,Info-Zip,PKZIP,Java JAR/Zip,.NET zip,dotnetzip等如何编码文件名以及他们将"版本制作"字段设置为压缩时,请告诉我.

特别是,Info-Zip在解压缩时会尝试这样做:

  • 文件系统= MS-DOS(0)=> CP437
    • 除外:版本= 2.5,2.6,4.0 => ISO 8859-1
  • 文件系统= HPFS(6)=> CP437
  • 文件系统= NTFS(10)和版本= 5.0 => CP437
  • 否则,ISO 8859-1

如果我想支持检查或从任意zip文件中提取并在没有EFS标志的情况下合理地尝试文件名编码,我还能找到什么?

zip jar 7zip zipfile winzip

10
推荐指数
2
解决办法
6731
查看次数

ZipInputStream.getNextEntry在某些zip文件上返回null

我有一个简单的代码来提取zip文件,它正常工作正常,但在我的测试期间我尝试了我的代码与一些zip文件(我从互联网上下载的字体,图标和模板)只是为了确保它应该提取任何zip文件提供,但它不使用一些zip文件,这里是重新生成此问题的最小化代码:

package com.test.mytest;

import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class ZipExtractTest {

    public static final String ZIP_FILE = "/Users/XXXXX/Downloads/janne.zip";

    public static void main(String[]args) {
        unzipFile(ZIP_FILE);
        unzipStream(ZIP_FILE);
    }

    public static void unzipFile(String zipName) {
        try {

            ZipFile zf = new ZipFile(zipName);

            Enumeration ent = zf.entries();

            while(ent.hasMoreElements()) {
                System.out.println(ent.nextElement());
            }

        } catch(Exception e) {
            System.out.println(e);
        }
    }

    public static void unzipStream(String zipName) {
        try {
            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipName));
            ZipEntry ze = zis.getNextEntry();

            if(ze == …
Run Code Online (Sandbox Code Playgroud)

java compression unzip zipfile

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

如何在Python中递归提取zip文件

我有一个zip文件,其中包含三个zip文件,如下所示:

zipfile.zip\  
    dirA.zip\
         a  
    dirB.zip\
         b  
    dirC.zip\
         c
Run Code Online (Sandbox Code Playgroud)

我想在具有这些名称(dirA,dirB,dirC)的目录中提取zip文件中的所有内部zip文件.
基本上,我想最终得到以下架构:

output\  
    dirA\
         a  
    dirB\
         b  
    dirC\
         c
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方法:

import os, re
from zipfile import ZipFile

os.makedirs(directory)  # where directory is "\output"
with ZipFile(self.archive_name, "r") as archive:
    for id, files in data.items():
        if files:
            print("Creating", id)
            dirpath = os.path.join(directory, id)

            os.mkdir(dirpath)

            for file in files:
                match = pattern.match(filename)
                new = match.group(2)
                new_filename = os.path.join(dirpath, new)

                content = archive.open(file).read()
            with open(new_filename, "wb") as outfile:
                outfile.write(content)
Run Code Online (Sandbox Code Playgroud)

但它只提取zip文件,我最终得到:

output\  
    dirA\
         dirA.zip 
    dirB\
         dirB.zip 
    dirC\ …
Run Code Online (Sandbox Code Playgroud)

python zip unzip zipfile python-3.x

10
推荐指数
3
解决办法
6775
查看次数

Python zipfile模块错误地认为我有一个跨越多个磁盘的zipfile,抛出了BadZipfile错误

我有一个1.4GB的zip文件,我试图连续产生每个成员.zipfile模块不断抛出BadZipfile异常,说明这一点

"zipfile.BadZipfile:不支持跨多个磁盘的zipfiles".

这是我的代码:

import zipfile

def iterate_members(zip_file_like_object):
  zflo = zip_file_like_object
  assert zipfile.is_zipfile(zflo) # Here is where the error happens.
  # If I comment out the assert, the same error gets thrown on this next line:
  with zipfile.ZipFile(zflo) as zip:
    members = zip.namelist()
    for member in members:
      yield member

fn = "filename.zip"
iterate_members(open(fn, 'rb'))
Run Code Online (Sandbox Code Playgroud)

我正在使用Python 2.7.3.我在Windows 8和ubuntu上尝试了相同的结果.任何帮助非常感谢.

zipfile python-2.7

9
推荐指数
1
解决办法
2719
查看次数