classification
Title: zipfile to handle duplicate files in archive
Type:
Components: Extension Modules Versions: Python 2.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BitTorment, alanmcintyre, georg.brandl, techtonik
Priority: Keywords:

Created on 2008-05-11 20:17 by techtonik, last changed 2008-05-13 12:42 by BitTorment.

Messages
msg66661 (view) Author: anatoly techtonik (techtonik) Date: 2008-05-11 20:17
ZipFile allows to add the same file to archive twice. I bet it is not
intended behavior for many users who would like to either replace file
inside of archive or get runtime warning about duplicate file to be
added. http://code.google.com/p/gvn/issues/detail?id=63


from zipfile import ZipFile

zt=ZipFile("ziptest.zip","w")
zt.write("ziptest.py")
zt.write("ziptest.py")
zt.close()
msg66682 (view) Author: Georg Brandl (georg.brandl) Date: 2008-05-11 21:58
I think a warning would be sensible here. The behavior is certainly not
what I would expect.
msg66728 (view) Author: anatoly techtonik (techtonik) Date: 2008-05-12 15:03
How about adding optional "replace=True" attribute to the write method?
So that people who are not aware enough to adjust the code and handle
new warning could still get valid archives.
msg66781 (view) Author: Martin McNickle (BitTorment) Date: 2008-05-13 12:42
The mechanism for throwing an error has been written, however for the
case of a duplicated filename, it appears to have been deliberatly not
been used by the original author.:

 def _writecheck(self, zinfo):
        """Check for errors before writing a file to the archive."""
        if zinfo.filename in self.NameToInfo:
            if self.debug:      # Warning for duplicate names
                print "Duplicate name:", zinfo.filename
        if self.mode not in ("w", "a"):
            raise RuntimeError, 'write() requires mode "w" or "a"'
        ...

Putting a 'replace=True' switch seems a little clumsy, it would be much
better to raise an error, or allow the user a way to globally control
what happens in this case, i.e. overwrite the existing file or drop it.
 Adding a global behaviour switch seems to be the best way to preserve
backwards compatibility.

What do people think is the best way?

-- Martin
History
Date User Action Args
2008-05-13 12:42:28BitTormentsetnosy: + BitTorment
messages: + msg66781
2008-05-12 15:04:00techtoniksetmessages: + msg66728
2008-05-11 21:58:29georg.brandlsetnosy: + georg.brandl, alanmcintyre
messages: + msg66682
2008-05-11 20:17:49techtonikcreate