[Tutor] how to zip data?

Branimir Petrovic BranimirP at cpas.com
Tue Nov 23 05:09:51 CET 2004



> -----Original Message-----
> From: Ramkumar Parimal Alagan [mailto:ramster6 at gmail.com]

> zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))
> 
> if os.system(zip_command) == 0:
>     print 'Successful backup to', target
> else:
>     print 'Backup FAILED'
> 


"""No such a thing as "zip" command/utility on Windows, but using
Python's standard library you could build your own.... 

Example below shows how to gz-ip a file (if you wonder why .gz and 
not .zip: gzip library compresses as good and has no upper limit 
on how large file it can process).

If below example looks too involving, you could purchase WinZip
and get command line utility that will zip files for you via
os.system call (but beware - WinZip can not zip/unzip files larger 
than 4 GB)

Branimir 
"""

######################################################################
#   FileName    gzDemo.py
######################################################################

import exceptions, os, re, gzip

def gzipIt(filetogzip, gzippedname=None):
    """Will GZIP file whose path is described by 'filetogzip' string.

    Parameters:
        filetogzip, gzippedname - STRING

    By default (if 'gzippedname' not supplied):,
        - will gunzip 'filetogzip' to same folder with the source file, 
        - will add '.gz' suffix to file name
        - will remove source file after successful gzipping.

    If 'gzippedname' is supplied, the source ('filetogzip') is not removed.

    Works like a charm although is 20% slower than GNU's gunzip utility.
    """

    chunksize=1048576
    removesource = False

    if not gzippedname: 
        gzippedname = filetogzip + r'.gz'
        removesource = True

    inputfile = file(filetogzip, r'rb', chunksize)
    try:
        chunkdata = inputfile.read(chunksize)
        gzoutfile = gzip.open(gzippedname, mode='wb', compresslevel=5)
        try:
            while chunkdata:
                gzoutfile.write(chunkdata)
                chunkdata = inputfile.read(chunksize)
        finally:
            gzoutfile.close()
    finally:
        inputfile.close()

    if removesource: os.remove(filetogzip)


def gunzipIt(filetogunzip, gunzippedname=None):
    """Will GUNZIP file whose path is described by 'filetogzip' string.

    Parameters:
        filetogunzip, gunzippedname - STRING

    By default (if 'gunzippedname' not supplied):,
        - will gunzip 'filetogunzip' to same folder with the source file, 
        - will strip '.gz' from file name suffix and 
        - will remove source file after successful gunzipping.

    If 'gunzippedname' is supplied, the source ('filetogunzip') is not
removed.

    Works like a charm although is 30% slower than GNU's gunzip utility.
    """

    chunksize=8192
    removesource = False

    if not gunzippedname:
        # strip '.gz' off the end of gzippedname
        gunzippedname = re.sub('(\.(g|G)(z|Z))', '', filetogunzip)
        removesource = True

    fgunzip = gzip.open(filetogunzip, mode='rb')
    try:
        chunkdata = fgunzip.read(chunksize)
        foutput = file(gunzippedname, r'wb', chunksize)
        try:
            while chunkdata:
                foutput.write(chunkdata)
                chunkdata = fgunzip.read(chunksize)
        finally:
            foutput.close()
    finally:
        fgunzip.close()

    if removesource: os.remove(filetogunzip)




if __name__=="__main__":

    try:
        # 1st run will "do-it:"
        toZip = r'C:\_b\textStuff.txt'
        gzipIt(toZip)
    except exceptions.IOError:
        # 2nd run will "un-do-it":
        try:
            toUnZip = r'C:\_b\textStuff.txt.gz'
            gunzipIt(toUnZip)
        except exceptions.IOError:
             pass


More information about the Tutor mailing list