Python 2.6 does not like appending to existing archives in zip files
Posted
by user313661
on Stack Overflow
See other posts from Stack Overflow
or by user313661
Published on 2010-05-27T11:50:48Z
Indexed on
2010/05/27
14:51 UTC
Read the original article
Hit count: 194
Hello,
In some Python unit tests of a program I'm working on we use in-memory zipfiles for end to end tests. In SetUp() we create a simple zip file, but in some tests we want to overwrite some archives. For this we do "zip.writestr(archive_name, zip.read(archive_name) + new_content)". Something like
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()
The problem is that this works fine in Python 2.4 and 2.5, but not 2.6. In Python 2.6 this fails on the print line with "BadZipfile: File name in directory "bar" and header "foo" differ."
It seems that it is reading the correct file bar, but that it thinks it should be reading foo instead.
I'm at a loss. What am I doing wrong? Is this not supported? I tried searching the web but could find no mention of similar problems. I read the zipfile documentation, but could not find anything (that I thought was) relevant, especially since I'm calling read() with the filename string.
Any ideas?
Thank you in advance!
© Stack Overflow or respective owner