繁体   English   中英

Python zipfile.ZipFile 压缩损坏的文件

[英]Python zipfile.ZipFile zips a corrupt file

我有一个 Django 视图,用户可以在我的本地服务器上调用 zip 文件。 它使用zipfile.ZipFile将多个文件压缩成一个 zip 如下:

with ZipFile(my_dir + 'folder.zip', 'w') as zipObj:
                zipObj.write(my_dir + '1.json')
                zipObj.write(my_dir + '2.json')

然后我将此文件返回给用户作为响应:

folder_file = open(full_path, "r", encoding='Cp437')
            response = HttpResponse(FileWrapper(folder_file), content_type='application/zip')

但是下载的文件已损坏,我无法使用 ubuntu 存档管理器打开它。

然后,当我尝试在我的 django 服务器中使用 python 和相同的 package 解压缩文件时,我仍然收到错误消息:

with ZipFile(file_path, 'r') as zip_ref:
            zip_ref.extractall(my_dir)

我得到的错误是:

  File ".../views.py", line 38, in post
    with ZipFile(file_path, 'r') as zip_ref:
  File "/usr/lib/python3.8/zipfile.py", line 1269, in __init__
    self._RealGetContents()
  File "/usr/lib/python3.8/zipfile.py", line 1354, in _RealGetContents
    fp.seek(self.start_dir, 0)
OSError: [Errno 22] Invalid argument

知道我在这里做错了什么吗?

这应该是一条评论,但我认为它太长了。

我认为你应该看看你的路径,因为错误的路径会导致不需要的行为。 zipfile 写

存档名称应该相对于存档根目录,也就是说,它们不应该以路径分隔符开头。

zipfile 提取(全部)

如果成员文件名是绝对路径,驱动器/UNC sharepoint 和前导(反)斜杠将被删除,例如:///foo/bar 在 Unix 上变为 foo/bar,而 C:\foo\bar 变为 foo\bar在 Windows 上。成员文件名中的所有“..”组件将被删除,例如:../../foo../../ba..r 变为 foo../ba..r。 Windows 上的非法字符(:、<、>、|、"、? 和 *)替换为下划线 (_)。

所以确保你使用正确的路径。 并确保他们没有像这里那样有问题的字符(如通配符或反斜杠)

也许你应该用其他(非)zip 工具进行测试,看看它是否有所作为,有时它们更具体( 像这里一样

你可以尝试这样的事情。

zipf = ZipFile("whatever.zip", "w")

for file in files_to_add:
    this_file = urlopen(file).read()
    this_filename = 'file name.json'
    zipf.writestr(this_filename, this_file)

zipf.close()

response = HttpResponse(io.open("whatever.zip", mode="rb").read(), content_type="application/zip")
response["Content-Disposition"] = "attachment; filename=whatever_name_you_want.zip"

return response

创建 HttpResponse 时以二进制模式打开 zip 文件以避免换行符转换错误:

folder_file = open(full_path, "rb")
        response = HttpResponse(FileWrapper(folder_file), content_type='application/zip')

暂无
暂无

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

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