繁体   English   中英

Python zipfile,奇怪的文件数量限制:“文件夹无效”

[英]Python zipfile, bizarre limit to number of files: “folder is invalid”

电脑在和我玩弄,我知道!

我正在Python中创建一个zip文件夹。 单个文件在内存中生成,然后将整个文件压缩并保存到文件中。 我可以将9个文件添加到zip文件中。 我可以在zip文件中添加11个文件。 但是10个,不,不是10个文件。 该zip文件已保存到我的计算机中,但不允许我打开它; Windows说压缩的压缩文件夹无效。

我使用下面的代码,该代码来自另一个stackoverflow问题。 它会附加10个文件并保存压缩文件夹。 当我单击该文件夹时,无法解压缩它。 但是,删除其中一个appends()就可以了。 或者,添加另一个附录即可使用!

我在这里想念什么? 我怎样才能使这项工作每次?

imz = InMemoryZip() 
imz.append("1a.txt", "a").append("2a.txt", "a").append("3a.txt", "a").append("4a.txt", "a").append("5a.txt", "a").append("6a.txt", "a").append("7a.txt", "a").append("8a.txt", "a").append("9a.txt", "a").append("10a.txt", "a")
imz.writetofile("C:/path/test.zip") 


import zipfile
import StringIO
class InMemoryZip(object):
    def __init__(self):
        # Create the in-memory file-like object
        self.in_memory_zip = StringIO.StringIO()

    def append(self, filename_in_zip, file_contents):
        '''Appends a file with name filename_in_zip and contents of 
        file_contents to the in-memory zip.'''
        # Get a handle to the in-memory zip in append mode
        zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_DEFLATED, False)

        # Write the file to the in-memory zip
        zf.writestr(filename_in_zip, file_contents)

        # Mark the files as having been created on Windows so that
        # Unix permissions are not inferred as 0000
        for zfile in zf.filelist:
            zfile.create_system = 0        

        return self

    def read(self):
        '''Returns a string with the contents of the in-memory zip.'''
        self.in_memory_zip.seek(0)
        return self.in_memory_zip.read()

    def writetofile(self, filename):
        '''Writes the in-memory zip to a file.'''
        f = file(filename, "w")
        f.write(self.read())
        f.close()

创建要保存到文件系统的文件时,应使用“ wb”模式。 这将确保文件以二进制形式编写。

否则,在zip文件python中碰巧遇到换行符(\\ n)时,它将替换它以匹配Windows行尾(\\ r \\ n)。 10个文件有问题的原因是10个恰好是\\ n的代码。

因此,您的写入功能应如下所示:

def writetofile(self, filename):
    '''Writes the in-memory zip to a file.'''
    f = file(filename, 'wb')
    f.write(self.read())
    f.close()

这应该可以解决您的问题,并可以处理示例中的文件。 虽然,根据您的情况,您可能会发现将zip文件直接写入文件系统比较容易,例如此代码,其中包含上面的一些注释:

import StringIO
import zipfile

class ZipCreator:
    buffer = None

    def __init__(self, fileName=None):
        if fileName:
            self.zipFile = zipfile.ZipFile(fileName, 'w', zipfile.ZIP_DEFLATED, False)
            return

        self.buffer = StringIO.StringIO()
        self.zipFile = zipfile.ZipFile(self.buffer, 'w', zipfile.ZIP_DEFLATED, False)

    def addToZipFromFileSystem(self, filePath, filenameInZip):
        self.zipFile.write(filePath, filenameInZip)

    def addToZipFromMemory(self, filenameInZip, fileContents):
        self.zipFile.writestr(filenameInZip, fileContents)

        for zipFile in self.zipFile.filelist:
            zipFile.create_system = 0

    def write(self, fileName):            
        if not self.buffer:  # If the buffer was not initialized the file is written by the ZipFile
            self.zipFile.close()
            return

        f = file(fileName, 'wb')
        f.write(self.buffer.getvalue())
        f.close()

# Use File Handle
zipCreator = ZipCreator('C:/path/test.zip')

# Use Memory Buffer
# zipCreator = ZipCreator()

for i in range(1, 10):
    zipCreator.addToZipFromMemory('test/%sa.txt' % i, 'a')

zipCreator.write('C:/path/test.zip')

理想情况下,您可能会对内存中的zip和从一开始就与文件系统绑定的zip使用不同的类。 添加文件夹时,内存zip似乎也存在一些问题,这些问题很难重新创建并且我仍在尝试查找。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM