Issue621502

classification
Title: zipfile usage in append mode
Type: Severity: normal
Components: Library Versions:
Milestone:
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ellisj, fwierzbicki
Priority: normal Keywords:

Created on 2002-10-10.18:37:07 by anonymous, last changed 2005-06-13.03:07:07 by fwierzbicki.

Messages
msg756 (view) Author: Nobody/Anonymous (nobody) Date: 2002-10-10.18:37:07
I have been trying to use zipfile.ZipFile() with mode="a" 
to add files to an archive. 

However when I try to perform a write, each file simply 
overwrites the previous one. 

I have tried exactly the same code in Python 2.2 and it 
works differently. For instance, in Jython, opening a non-
existent zip file with mode="a" creates the file, whereas 
a runtime error is generated if you perform the same 
action in Python, which requires the file to be present. If 
I create the file, open it with mode="a" and write files to 
the archive, this works fine with Python. 

Perhaps someone out there can verify that this is indeed 
a bug, or perhaps I am doing somthing wrong. 

I have attached some code to explain below. 

import zipfile 

#File c:\\test.zip does not exist... 
file = zipfile.ZipFile("c:\\test.zip", mode="a") 

#Above, if done in Python, creates a runtime error! 

#Add first file to archive 
file.write("c:\\test1.txt") 

#Add second file to archive 
file.write("c:\\test2.txt") 

file.close() 

#Oh dear, appears that archive now just consists of  
#c:\\test2.txt 

In the meantime the bug can be got around by reading in 
the zip file contents if it exists, then opening the same 
zip file in write mode, copying back the contents then 
adding the new file.
msg757 (view) Author: Jonathan Ellis (ellisj) Date: 2004-02-29.14:14:40
Logged In: YES 
user_id=657828

I submitted a patch that has zipfile correctly error when
mode=a for a file that does not exist, but I cannot
reproduce the main thrust of this report:
<p>
>>> f = zipfile.ZipFile("bar.zip", mode="w")
>>> f.write(".emacs")
>>> f.write(".profile")
>>> f.close()
>>> f = zipfile.ZipFile("bar.zip", mode="r")
>>> f.printdir()
File Name                                            
Modified             Size
.emacs                                         2003-12-26
16:12:42         4910
.profile                                       2003-11-12
12:14:18           41
>>> f.close()
>>> f = zipfile.ZipFile("bar.zip", mode="a")
>>> f.write(".psql_history")
>>> f.close()
>>> f = zipfile.ZipFile("bar.zip", mode="r")
>>> f.printdir()
File Name                                            
Modified             Size
.emacs                                         2003-12-26
16:12:42         4910
.profile                                       2003-11-12
12:14:18           41
.psql_history                                  2004-01-27
09:02:02         5721
>>>
</p>
msg758 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2005-06-13.03:06:17
Logged In: YES 
user_id=193969

Applied the essence of Jonathan Ellis' patch.  Only fixes
the lack of exception in the case of appending to a
non-existing file.  I was unable to reproduce the rest just
as Jonathan found.  
msg759 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2005-06-13.03:07:07
Logged In: YES 
user_id=193969

Applied the essence of Jonathan Ellis' patch.  Only fixes
the lack of exception in the case of appending to a
non-existing file.  I was unable to reproduce the rest just
as Jonathan found.  
History
Date User Action Args
2002-10-10 18:37:07anonymouscreate