0

电脑在玩我,我知道!

我正在 Python 中创建一个 zip 文件夹。单个文件在内存中生成,然后将整个文件压缩并保存到文件中。我可以在 zip 中添加 9 个文件。我可以在 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()
4

1 回答 1

1

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

否则,每当在 zip 文件中遇到换行符 (\n) 时,python 都会将其替换为与 windows 行结尾 (\r\n) 匹配。10 个文件有问题的原因是 10 个恰好是 \n 的代码。

所以你的 write 函数应该是这样的:

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 似乎也存在一些问题,这些问题难以重新创建,我仍在努力追查。

于 2013-09-05T22:46:43.907 回答