【问题标题】:Python - error when zipping files, 'L' format requires 0 <= number <= 4294967295Python - 压缩文件时出错,'L' 格式需要 0 <= number <= 4294967295
【发布时间】:2011-08-02 12:06:22
【问题描述】:

我运行此代码来压缩文件夹和文件。

def save_folder_as_zip(self, folder_to_zip, save_as):
    zipf = zipfile.ZipFile(str(save_as), mode="w", allowZip64=True)
    self.create_zip(zipf, folder_to_zip)
    zipf.close()

    return zipf

def create_zip(self, zipf, directory, folder=""):

    directory = directory.encode("utf-8")

    for item in os.listdir(directory):
        if temp_folder == directory + os.sep:
            continue

        try:
            if os.path.isfile(os.path.join(directory, item)):
                zipf.write(os.path.join(directory, item), folder + os.sep + item)
            elif os.path.isdir(os.path.join(directory, item)):
                self.create_zip(zipf, os.path.join(directory, item).decode("utf-8"), folder + os.sep + item)
        except Exception, e:
            self.schedule.machine.log_warning(str(e))

然后我得到这个错误:

'L' format requires 0 <= number <= 4294967295

发生这种情况的文件名示例:

/[Wii]TrackMania[PAL][WiiSOS.com]/ws_tramap.iso

有什么办法可以解决这个问题吗?

追溯:

Traceback (most recent call last):
File "test.py", line 29, in <module>
   save_folder_as_zip("/Users/f/Downloads/", "hei.zip")
File "test.py", line 13, in save_folder_as_zip
   create_zip(zipf, folder_to_zip)
File "test.py", line 26, in create_zip
   create_zip(zipf, os.path.join(directory, item).decode("utf-8"), folder + os.sep + item)  
File "test.py", line 24, in create_zip
   zipf.write(os.path.join(directory, item), folder + os.sep + item)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 1100, in write
   zinfo.file_size))
struct.error: 'L' format requires 0 <= number <= 4294967295

【问题讨论】:

  • 哪一行触发了错误?
  • 您的文件是否大于 4GB?请粘贴完整的回溯。
  • 被压缩的目录总大小为6GB,但示例中的文件约为1GB。
  • 很好。请使用完整的回溯更新您的原始帖子。并且目录中没有其他文件超过4GB?
  • 我认为这与this bug有关。如果是这样,您可能无法在不接触 zipfile.py 的情况下解决您的问题...

标签: python zip filenames


【解决方案1】:

我认为应用此错误报告所附的补丁可以修复它:

http://bugs.python.org/issue9720

当您尝试包含在 zip 文件中的文件大于原始或压缩形式的 2^32-1 字节时,就会出现问题。在这种情况下,库需要存储 -1 作为文件大小/压缩大小,并且必须分配一些额外的字段来存储实际大小,这不适合 32 位 dword。

由于某种原因,该补丁在 3.2 或 2.7.2+ 中未被接受,但我已经对其进行了测试,并且可以正常工作。我认为它不能用 writestr() 方法解决一些极端情况,但如果你只使用 write() 来存储文件,它会很好用。

【讨论】:

    猜你喜欢
    • 2015-03-08
    • 2020-09-06
    • 2013-07-18
    • 2023-03-22
    • 1970-01-01
    • 2017-03-15
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多