简体   繁体   English

在python中压缩二进制文件

[英]Zipping a binary file in python

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. 我试图在zip文件中包含一个二进制文件,下面是代码片段:我首先将zip内容解压缩到一个临时位置,然后添加更多文件并将其压缩回新的存档。

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: 压缩代码工作正常但是当我尝试使用linux的unzip或python的zip模块解压缩时,我得到以下错误:

zipfile corrupt. zipfile损坏。 (please check that you have transferred or created the zipfile in the appropriate BINARY mode and that you have compiled UnZip properly) (请检查您是否已在相应的BINARY模式下转移或创建了zipfile,并且您已正确编译UnZip)

And am using python 2.3 我正在使用python 2.3

What's going wrong here ? 这里出了什么问题?

You might want to upgrade, as Python 2.3 is really outdated. 你可能想要升级,因为Python 2.3真的已经过时了。 2.7.3 is the latest one from the 2.x-versions and 3.2.3 the latest python version. 2.7.3是最新的2.x版本和3.2.3最新的python版本。

See docs.python.org : 请参阅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) (2.6版新增功能)

Take a look at Zip a folder and its content . 看看Zip文件夹及其内容

You might also be interested in distutlis.archive_util . 您可能也对distutlis.archive_util感兴趣。

Hmm not sure if its a bug in python 2.3. 嗯不确定它是否是python 2.3中的一个bug。 Current work environment do not allow me to upgrade to a higher version of python :-( :-( :-( 目前的工作环境不允许我升级到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    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM