Zipping files/zipfile module

Discussion in 'Python' started by OriginalBrownster, Aug 2, 2006.

  1. This will probably sound like a very dumb question.

    I am trying to zip some files within a directory.

    I want to zip all the files within a directory called "temp"
    and have the zip archive saved in a directory with temp called ziptemp

    I was trying to read up on how to use the zipfile module python
    provides, but I cannot seem to find adequate documentation on function
    itself.

    Perhaps someone could help me in this task?

    I am guessing it must be something like the shutile module
    something like copy(src,dst)

    THank you

    Stephen
    OriginalBrownster, Aug 2, 2006
    #1
    1. Advertising

  2. OriginalBrownster

    Brian Beck Guest

    OriginalBrownster wrote:
    > I want to zip all the files within a directory called "temp"
    > and have the zip archive saved in a directory with temp called ziptemp
    >
    > I was trying to read up on how to use the zipfile module python
    > provides, but I cannot seem to find adequate documentation on function
    > itself.
    >
    > Perhaps someone could help me in this task?


    Hello,

    This isn't completely tested, but perhaps it will help you get started:

    from os import listdir, mkdir
    from os.path import join, basename, isfile
    from zipfile import ZipFile

    def zip_dir(path, output_path, include_hidden=True):
    files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
    try:
    mkdir(output_path)
    except OSError, e:
    if e.errno == 17: # Path exists
    pass
    zip_file = ZipFile(join(output_path, 'temp.zip'), 'w')
    for f in files:
    if basename(f).startswith('.') and not include_hidden:
    continue
    print "Adding %s to archive..." % (f,)
    zip_file.write(f)
    zip_file.close()

    Use like:
    zip_dir('temp', 'temp/ziptemp')

    Note that if you want to add the entire contents of a directory
    (subdirectories, recursively), you should consider using os.walk or
    something similar. This will only add the file contents of the directory.
    I'm not sure if the zipfile module provides any nice ways to write
    directories to the archive, but I'm assuming it just involves writing an
    arcname with a '/' in it (see help(zipfile.ZipFile)).

    --
    Brian Beck
    Adventurer of the First Order
    Brian Beck, Aug 2, 2006
    #2
    1. Advertising

  3. OriginalBrownster

    Simon Forman Guest

    Brian Beck wrote:
    > OriginalBrownster wrote:
    > > I want to zip all the files within a directory called "temp"
    > > and have the zip archive saved in a directory with temp called ziptemp
    > >
    > > I was trying to read up on how to use the zipfile module python
    > > provides, but I cannot seem to find adequate documentation on function
    > > itself.
    > >
    > > Perhaps someone could help me in this task?

    >
    > Hello,
    >
    > This isn't completely tested, but perhaps it will help you get started:
    >
    > from os import listdir, mkdir
    > from os.path import join, basename, isfile
    > from zipfile import ZipFile
    >
    > def zip_dir(path, output_path, include_hidden=True):
    > files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
    > try:
    > mkdir(output_path)
    > except OSError, e:
    > if e.errno == 17: # Path exists
    > pass
    > zip_file = ZipFile(join(output_path, 'temp.zip'), 'w')
    > for f in files:
    > if basename(f).startswith('.') and not include_hidden:
    > continue
    > print "Adding %s to archive..." % (f,)
    > zip_file.write(f)
    > zip_file.close()
    >
    > Use like:
    > zip_dir('temp', 'temp/ziptemp')
    >
    > Note that if you want to add the entire contents of a directory
    > (subdirectories, recursively), you should consider using os.walk or
    > something similar. This will only add the file contents of the directory.
    > I'm not sure if the zipfile module provides any nice ways to write
    > directories to the archive, but I'm assuming it just involves writing an
    > arcname with a '/' in it (see help(zipfile.ZipFile)).
    >
    > --
    > Brian Beck
    > Adventurer of the First Order


    To avoid calling os.path.join() twice for each filename when you build
    the list of files you could write the list comprehension like so:

    [n for n in (join(path, f) for f in listdir(path)) if isfile(n)]

    Also, you should use the "symbolic" errors from the errno module rather
    than hard-coding a constant:

    from errno import EEXIST
    ....
    if e.errno == EEXIST: # Path exists

    Finally, if your using a single arg with a string interpolation and you
    know it'll never be a tuple you needn't wrap it in a tuple:

    print "Adding %s to archive..." % f
    Simon Forman, Aug 2, 2006
    #3
  4. OriginalBrownster

    Yves Lange Guest

    Simon Forman a écrit :
    > Brian Beck wrote:
    >> OriginalBrownster wrote:
    >>> I want to zip all the files within a directory called "temp"
    >>> and have the zip archive saved in a directory with temp called ziptemp
    >>>
    >>> I was trying to read up on how to use the zipfile module python
    >>> provides, but I cannot seem to find adequate documentation on function
    >>> itself.
    >>>
    >>> Perhaps someone could help me in this task?

    >> Hello,
    >>
    >> This isn't completely tested, but perhaps it will help you get started:
    >>
    >> from os import listdir, mkdir
    >> from os.path import join, basename, isfile
    >> from zipfile import ZipFile
    >>
    >> def zip_dir(path, output_path, include_hidden=True):
    >> files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
    >> try:
    >> mkdir(output_path)
    >> except OSError, e:
    >> if e.errno == 17: # Path exists
    >> pass
    >> zip_file = ZipFile(join(output_path, 'temp.zip'), 'w')
    >> for f in files:
    >> if basename(f).startswith('.') and not include_hidden:
    >> continue
    >> print "Adding %s to archive..." % (f,)
    >> zip_file.write(f)
    >> zip_file.close()
    >>
    >> Use like:
    >> zip_dir('temp', 'temp/ziptemp')
    >>
    >> Note that if you want to add the entire contents of a directory
    >> (subdirectories, recursively), you should consider using os.walk or
    >> something similar. This will only add the file contents of the directory.
    >> I'm not sure if the zipfile module provides any nice ways to write
    >> directories to the archive, but I'm assuming it just involves writing an
    >> arcname with a '/' in it (see help(zipfile.ZipFile)).
    >>
    >> --
    >> Brian Beck
    >> Adventurer of the First Order

    >
    > To avoid calling os.path.join() twice for each filename when you build
    > the list of files you could write the list comprehension like so:
    >
    > [n for n in (join(path, f) for f in listdir(path)) if isfile(n)]
    >
    > Also, you should use the "symbolic" errors from the errno module rather
    > than hard-coding a constant:
    >
    > from errno import EEXIST
    > ...
    > if e.errno == EEXIST: # Path exists
    >
    > Finally, if your using a single arg with a string interpolation and you
    > know it'll never be a tuple you needn't wrap it in a tuple:
    >
    > print "Adding %s to archive..." % f
    >

    Other solutions:
    you can try the rar command line from WinRar but it's not recommended.
    This is a very slow manner to compress file. Or you can try the Bz
    module of python.
    Yves Lange, Aug 2, 2006
    #4
  5. OriginalBrownster

    Ant Guest

    Enabling directory recursion:

    > from os import listdir, mkdir
    > from os.path import join, basename, isfile
    > from zipfile import ZipFile
    >
    > def zip_dir(path, output_path, include_hidden=True):
    > try:
    > mkdir(output_path)
    > except OSError, e:
    > if e.errno == 17: # Path exists
    > pass
    > zip_file = ZipFile(join(output_path, 'temp.zip'), 'w')


    for root, dirs, files in os.walk(dir):
    for f in files:
    fp = path.join(root, f)
    zip_file.write(fp, fp[len(dir):]) # Write to zip as a
    path relative to original dir.
    > zip_file.close()
    Ant, Aug 2, 2006
    #5
  6. OriginalBrownster

    Brian Beck Guest

    Yves Lange wrote:
    > Other solutions:
    > you can try the rar command line from WinRar but it's not recommended.
    > This is a very slow manner to compress file.


    Are you sure? This worked about 4 times faster than the zip command line
    utility in Linux, compressing the same files...

    --
    Brian Beck
    Adventurer of the First Order
    Brian Beck, Aug 2, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Tolton

    Zipping and Unzipping files

    Doug Tolton, Nov 21, 2003, in forum: Python
    Replies:
    1
    Views:
    306
    Peter Hansen
    Nov 21, 2003
  2. OriginalBrownster

    Zipping Files to user defined Directory

    OriginalBrownster, Jul 28, 2006, in forum: Python
    Replies:
    4
    Views:
    326
    Danny Milosavljevic
    Sep 2, 2006
  3. OriginalBrownster
    Replies:
    4
    Views:
    322
    Dennis Lee Bieber
    Aug 21, 2006
  4. dp_pearce

    Zipping files

    dp_pearce, Jul 15, 2008, in forum: Python
    Replies:
    3
    Views:
    304
    Tim Golden
    Jul 15, 2008
  5. Arthur
    Replies:
    2
    Views:
    115
    Arthur
    Nov 30, 2003
Loading...

Share This Page