Directory structure inside a ZipFile object

Discussion in 'Python' started by Bulba!, Dec 9, 2004.

  1. Bulba!

    Bulba! Guest

    Hello everyone,

    I'm a newbie to Python, learning it and tried to write the script that
    would read file, zip it, and put in the target directory. I used
    ZipFile module (code below).

    It works nice, except the zipfile created contains the directory path
    of the source file -- which I do NOT want to recreate. E.g. when
    I compress 'c:\\temp\\zz.txt', the resulting zipfile contains not just
    'zz.txt' file, but also the whole directory, i.e. first directory
    is 'c:', inside of it the directory 'temp' is located, and only then
    the 'zz.txt' file is stored.

    Changing current directory via os.chdir and then using the
    filename itself (zz.txt) doesn't work, i.e. the source directory
    structure is still re-created within the zipfile.

    Code follows. TIA for reply,


    def zf(sfpath, targetdir):
    if (sys.platform[:3] == 'win'):
    tgfpath=sfpath[2:]
    else:
    tgfpath=sfpath
    zfdir=os.path.dirname(os.path.abspath(targetdir) + tgfpath)
    zfname=zfdir + '\\' + os.path.basename(tgfpath) + '.zip'
    if(not os.path.isdir(zfdir)):
    os.makedirs(zfdir)
    archive=ZipFile(zfname, 'w', ZIP_DEFLATED)
    print 'zfname ' + zfname
    archive.write(sfpath)
    archive.close()

    >>> zf('c:\\temp\\zz.txt','c:\\tmp\\test1\\test2')

    zfname c:\tmp\test1\test2\temp\zz.txt.zip
    >>>




    --
    "Cultural ecstasy may have billions of participants, but it hardly
    has a single friend." -- Charles Freund
     
    Bulba!, Dec 9, 2004
    #1
    1. Advertisements

  2. Bulba! wrote:
    > tried to read file, zip it, and put in the target directory....
    >
    > It works nice, except the zipfile created contains the directory path
    > of the source file -- which I do NOT want to recreate.

    Look into the two-argument form of the write command:

    import zipfile
    archive = zipfile.ZipFile('box.zip', 'w', zipfile.ZIP_DEFLATED)
    archive.write('source/somewhere/one.txt', 'name.txt')
    archive.write('two.txt', 'another.txt')
    archive.close()

    --Scott David Daniels
     
    Scott David Daniels, Dec 9, 2004
    #2
    1. Advertisements

  3. Bulba!

    Bulba! Guest

    On Thu, 09 Dec 2004 07:42:57 -0800, Scott David Daniels
    <> wrote:

    >Look into the two-argument form of the write command:


    Well, I should have guessed that, it works. Thanks!


    > import zipfile
    > archive = zipfile.ZipFile('box.zip', 'w', zipfile.ZIP_DEFLATED)
    > archive.write('source/somewhere/one.txt', 'name.txt')
    > archive.write('two.txt', 'another.txt')
    > archive.close()
    >
    >--Scott David Daniels
    >




    --
    It's a man's life in a Python Programming Association.
     
    Bulba!, Dec 9, 2004
    #3
    1. Advertisements

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. john
    Replies:
    4
    Views:
    884
    Jim Higson
    Jun 15, 2006
  2. Tung Wai Yip

    add empty directory using zipfile?

    Tung Wai Yip, Jun 25, 2003, in forum: Python
    Replies:
    6
    Views:
    6,067
    Tung Wai Yip
    Jun 26, 2003
  3. ralobao
    Replies:
    1
    Views:
    371
    vincent wehren
    Dec 8, 2004
  4. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    970
    Martin Ambuhl
    Oct 26, 2004
  5. Leo Nunez
    Replies:
    3
    Views:
    1,492
    Neil Kurzman
    Feb 9, 2005
  6. stefven blonqhern
    Replies:
    3
    Views:
    496
    stefven blonqhern
    Jul 5, 2007
  7. Daz
    Replies:
    2
    Views:
    203
  8. vunet
    Replies:
    7
    Views:
    200
    vunet
    Apr 2, 2008
Loading...