ZipFile 抱怨,有没有办法使用 zipfile 模块?

esp*_*akk 1 python zip popen zipfile

我正在尝试解压缩发送给我的一些彩信。问题是有时它有效,而另一些则无效。当它不起作用时,python zipfile 模块会抱怨并说这是一个错误的 zip 文件。但是 zipfile 使用 unix unzip 命令可以很好地解压。

这是我得到的

zippedfile = open('%stemp/tempfile.zip' % settings.MEDIA_ROOT, 'w+')
zippedfile.write(string)
z = zipfile.ZipFile(zippedfile)
Run Code Online (Sandbox Code Playgroud)

我正在使用 'w+' 并向其写入一个字符串,该字符串包含一个 zip 文件的 base64 解码字符串表示。

然后我喜欢这样:

filelist = z.infolist()  
images = []  

for f in filelist:  
    raw_mimetype = mimetypes.guess_type(f.filename)[0]  
    if raw_mimetype:  
        mimetype = raw_mimetype.split('/')[0]  
    else:  
        mimetype = 'unknown'  
    if mimetype == 'image':  
        images.append(f.filename)  
Run Code Online (Sandbox Code Playgroud)

这样我就得到了 zip 文件中所有图像的列表。但这并不总是有效,因为 zipfile 模块会抱怨某些文件。

有没有办法在不使用 zipfile 模块的情况下做到这一点?

我可以以某种方式使用 unix 命令 unzip 而不是 zipfile 然后使用同样的方法从存档中检索所有图像吗?

unw*_*ind 5

在将压缩数据写入文件时,您很可能应该以二进制模式打开文件。也就是说,你应该使用

zippedfile = open('%stemp/tempfile.zip' % settings.MEDIA_ROOT, 'wb+')
Run Code Online (Sandbox Code Playgroud)