'Exception on extracting a zipfile, after appending to it in Python

z = zipfile.ZipFile(io.BytesIO(artifact), mode='a') 
z.write("test.txt",arcname=r'bin/test.txt')
z.extractall('out')

Exception: zipfile.BadZipFile was unhandled by user code Message: File name in directory 'bin\test.txt' and header b'bin/test.txt' differ.

The interesting thing is if I write the file to disk, and try extract it, I get a invalid file error. This is on Win 7 by the way.
the bin folder already exists in the zipfile. Full Traceback



Solution 1:[1]

Actually the code works well on my Mac,and I think you should let us know that the structure of the zip file or what the variable artifact is.

Here is my advice:

  • Use forward slashes as path separators,when you create the zip file.

  • Try to print a warning not raise the exception,and check out the out folder,you will find the reason,maybe the slashes or string buffer.

Also you can read this issue.

Hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 McGrady