I am trying to decompress some MMS messages sent to me zipped. The problem is that sometimes it works, and others not. And when it doesnt work, the python zipfile module complains and says that it is a bad zip file. But the zipfile decompresses fine using the unix unzip command.
我正在尝试解压缩发送给我的一些MMS消息压缩。问题是有时它有效,有些则无效。当它不起作用时,python zipfile模块会抱怨并说它是一个糟糕的zip文件。但是zipfile使用unix unzip命令解压缩。
This is what ive got
这就是我所拥有的
zippedfile = open('%stemp/tempfile.zip' % settings.MEDIA_ROOT, 'w+')
zippedfile.write(string)
z = zipfile.ZipFile(zippedfile)
I am using 'w+' and writing a string to it, the string contains a base64 decoded string representation of a zip file.
我正在使用'w +'并为其写一个字符串,该字符串包含一个zip文件的base64解码字符串表示。
Then I do like this:
然后我喜欢这样:
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)
This way I've got a list of all the images in the zip file. But this doesnt always work, since the zipfile module complains about some of the files.
这样我就有了zip文件中所有图像的列表。但这并不总是有效,因为zipfile模块会抱怨某些文件。
Is there a way to do this, without using the zipfile module?
有没有办法做到这一点,而不使用zipfile模块?
Could I somehow use the unix command unzip instead of zipfile and then to the same thing to retrive all the images from the archive?
我可以以某种方式使用unix命令解压缩而不是zipfile然后同样的事情来从存档中检索所有图像吗?
2 个解决方案
#1
You should very probably open the file in binary mode, when writing zipped data into it. That is, you should use
在将压缩数据写入其中时,您应该很可能以二进制模式打开文件。也就是说,你应该使用
zippedfile = open('%stemp/tempfile.zip' % settings.MEDIA_ROOT, 'wb+')
#2
You might have to close and reopen the file, or maybe seek to the start of the file after writing it.
您可能必须关闭并重新打开该文件,或者可能在写入文件后寻找文件的开头。
filename = '%stemp/tempfile.zip' % settings.MEDIA_ROOT
zippedfile = open(filename , 'wb+')
zippedfile.write(string)
zippedfile.close()
z = zipfile.ZipFile(filename,"r")
You say the string is base64 decoded, but you haven't shown any code that decodes it - are you sure it's not still encoded?
你说这个字符串是base64解码的,但你没有显示任何解码它的代码 - 你确定它还没有被编码吗?
data = string.decode('base64')
#1
You should very probably open the file in binary mode, when writing zipped data into it. That is, you should use
在将压缩数据写入其中时,您应该很可能以二进制模式打开文件。也就是说,你应该使用
zippedfile = open('%stemp/tempfile.zip' % settings.MEDIA_ROOT, 'wb+')
#2
You might have to close and reopen the file, or maybe seek to the start of the file after writing it.
您可能必须关闭并重新打开该文件,或者可能在写入文件后寻找文件的开头。
filename = '%stemp/tempfile.zip' % settings.MEDIA_ROOT
zippedfile = open(filename , 'wb+')
zippedfile.write(string)
zippedfile.close()
z = zipfile.ZipFile(filename,"r")
You say the string is base64 decoded, but you haven't shown any code that decodes it - are you sure it's not still encoded?
你说这个字符串是base64解码的,但你没有显示任何解码它的代码 - 你确定它还没有被编码吗?
data = string.decode('base64')