4
votes

I am trying to include a binary file within a zip file and below is the code snippet: I first unzip the zip contents into a temporary location and add few more files and zip it back to a new archive.

import zipfile

def test(fileName, tempDir):
    # unzip the file contents,may contain binary files
    myZipFile=zipfile.ZipFile(fileName,'r')
    for name in myZipFile.namelist(): 
        toFile = tempDir + '/' + name
        fd = open(toFile, "w")
        fd.write(myZipFile.read(name))
        fd.close()
    myZipFile.close()
    # code which post processes few of the files goes here

    #zip it back
    newZip = zipfile.ZipFile(fileName, mode='w')
    try:
        fileList = os.listdir(tempDir)
        for name in fileList:
            name = tempDir + '/' + name
            newZip.write(name,os.path.basename(name))
        newZip.close()
    except Exception:
            print 'Exception occured while writing to PAR file: ' + fileName    

Some of the files may be binary files. The zipping code works fine but when i try to unzip it using linux ' unzip or python's zip module , i get the below error:

zipfile corrupt. (please check that you have transferred or created the zipfile in the appropriate BINARY mode and that you have compiled UnZip properly)

And am using python 2.3

What's going wrong here ?

2
Please correct your intendation after newZip and for and please add import zipfileMartin Thoma
Looks pretty much right to me. Tested with Python3 + minimal changes and works.Jonas Byström
Works in Python 2.7 too. Either it is a bug in 2.3, or some other error - did you try to open it with different zip programs?Tisho

2 Answers

2
votes

You might want to upgrade, as Python 2.3 is really outdated. 2.7.3 is the latest one from the 2.x-versions and 3.2.3 the latest python version.

See docs.python.org:

 |  extractall(self, path=None, members=None, pwd=None)
 |      Extract all members from the archive to the current working
 |      directory. `path' specifies a different directory to extract to.
 |      `members' is optional and must be a subset of the list returned
 |      by namelist().

(New in version 2.6)

Take a look at Zip a folder and its content.

You might also be interested in distutlis.archive_util.

2
votes

Hmm not sure if its a bug in python 2.3. Current work environment do not allow me to upgrade to a higher version of python :-( :-( :-(

The below workaround worked:

import zipfile

def test(fileName, tempDir):
    # unzip the file contents,may contain binary files
    myZipFile=zipfile.ZipFile(fileName,'r')
    for name in myZipFile.namelist(): 
        toFile = tempDir + '/' + name

        # check if the file is a binary file
        #if binary file, open it in "wb" mode
            fd = open(toFile, "wb")
        #else open in just "w" mode
            fd = open(toFile, "w")

        fd.write(myZipFile.read(name))
        fd.close()
    myZipFile.close()
    # code which post processes few of the files goes here

    #zip it back
    newZip = zipfile.ZipFile(fileName, mode='w')
    try:
        fileList = os.listdir(tempDir)
        for name in fileList:
            name = tempDir + '/' + name
            newZip.write(name,os.path.basename(name))
        newZip.close()
    except Exception:
            print 'Exception occured while writing to PAR file: ' + fileName