在我正在研究的程序的一些Python单元测试中,我们使用内存中的zipfiles进行端到端测试.在SetUp()中我们创建了一个简单的zip文件,但在某些测试中我们想要覆盖一些档案.为此,我们执行"zip.writestr(archive_name,zip.read(archive_name)+ new_content)".就像是
import zipfile
from StringIO import StringIO
def Foo():
zfile = StringIO()
zip = zipfile.ZipFile(zfile, 'a')
zip.writestr(
"foo",
"foo content")
zip.writestr(
"bar",
"bar content")
zip.writestr(
"foo",
zip.read("foo") +
"some more foo content")
print zip.read("bar")
Foo()
Run Code Online (Sandbox Code Playgroud)
问题是这在Python 2.4和2.5中运行良好,但不是 2.6.在Python 2.6中,打印行上的"BadZipfile:目录中的文件名"栏"和标题"foo"不同".
它似乎正在读取正确的文件栏,但它认为它应该是读取foo.
我不知所措.我究竟做错了什么?这不受支持吗?我尝试在网上搜索,但没有提到类似的问题.我读了zipfile文档,但找不到任何(我认为是)相关的东西,特别是因为我用文件名字符串调用read().
有任何想法吗?
先感谢您!
PKZIP 文件是高度结构化的,仅仅追加到末尾就会搞砸。我无法谈论早期版本的工作情况,但解决此问题的方法是打开一个 zip 文件进行读取,打开一个新文件进行写入,提取第一个文件的内容,然后在最后添加附加组件。完成后,将原始 zip 文件替换为新创建的文件。
运行代码时我得到的回溯是:
Traceback (most recent call last):
File "zip.py", line 19, in <module>
Foo()
File "zip.py", line 17, in Foo
print zip.read("bar")
File "/usr/lib/python2.6/zipfile.py", line 834, in read
return self.open(name, "r", pwd).read()
File "/usr/lib/python2.6/zipfile.py", line 874, in open
zinfo.orig_filename, fname)
zipfile.BadZipfile: File name in directory "bar" and header "foo" differ.
Run Code Online (Sandbox Code Playgroud)
经过仔细检查,我注意到您正在从使用 'a'pend 模式打开的类似文件的 StringIO 中读取,这应该会导致读取错误,因为 'a' 通常不可读,并且肯定必须在读取和读取之间进行eek()ed写道。我要闲逛一些并更新它。
更新:
从 Doug Hellmann 的优秀Python 本周模块中窃取了几乎所有这些代码,我发现它的工作原理与我的预期相当。人们不能仅仅附加到结构化的 PKZIP 文件,如果原始帖子中的代码确实有效,那也是偶然的:
import zipfile
import datetime
def create(archive_name):
print 'creating archive'
zf = zipfile.ZipFile(archive_name, mode='w')
try:
zf.write('/etc/services', arcname='services')
finally:
zf.close()
def print_info(archive_name):
zf = zipfile.ZipFile(archive_name)
for info in zf.infolist():
print info.filename
print '\tComment:\t', info.comment
print '\tModified:\t', datetime.datetime(*info.date_time)
print '\tSystem:\t\t', info.create_system, '(0 = Windows, 3 = Unix)'
print '\tZIP version:\t', info.create_version
print '\tCompressed:\t', info.compress_size, 'bytes'
print '\tUncompressed:\t', info.file_size, 'bytes'
print
zf.close()
def append(archive_name):
print 'appending archive'
zf = zipfile.ZipFile(archive_name, mode='a')
try:
zf.write('/etc/hosts', arcname='hosts')
finally:
zf.close()
def expand_hosts(archive_name):
print 'expanding hosts'
zf = zipfile.ZipFile(archive_name, mode='r')
try:
host_contents = zf.read('hosts')
finally:
zf.close
zf = zipfile.ZipFile(archive_name, mode='a')
try:
zf.writestr('hosts', host_contents + '\n# hi mom!')
finally:
zf.close()
def main():
archive = 'zipfile.zip'
create(archive)
print_info(archive)
append(archive)
print_info(archive)
expand_hosts(archive)
print_info(archive)
if __name__ == '__main__': main()
Run Code Online (Sandbox Code Playgroud)
值得注意的是最后一次调用的输出print_info:
...
hosts
Modified: 2010-05-20 03:40:24
Compressed: 404 bytes
Uncompressed: 404 bytes
hosts
Modified: 2010-05-27 11:46:28
Compressed: 414 bytes
Uncompressed: 414 bytes
Run Code Online (Sandbox Code Playgroud)
它没有附加到现有的 arcname“主机”,而是创建了一个额外的存档成员。
“Je n'ai fait celle-ci plus longue que parce que je n'ai pas eu le loisir de la faire plus Courte。”
——布莱斯·帕斯卡