difficult zipefile Question

Discussion in 'Python' started by matthiasjanes, Oct 6, 2004.

  1. hello all,

    i want to zip a folder plus the files and subfolders.

    which works something like that:

    #sample code

    import os, zipfile

    def zipit(path, ziper):

    for root, dirs, files in os.walk(path):

    for anyFile in files:

    fullname = os.path.join(root, anyFile)

    if __name__ == '__main__':

    ziper = zipfile.ZipFile('test.zip', 'w')
    zipit('somefolderTozip', ziper)
    ziper.close()
    #____________________________


    this works fine as long as there is no empty subfolder.

    can anyone tell me - or better give me a small example of code how to
    add an empty folder to the zip archive.

    thanks mr. janes
    matthiasjanes, Oct 6, 2004
    #1
    1. Advertising

  2. matthiasjanes

    Dan Perl Guest

    I may be wrong, and I may have to eat my words later, but I think that the
    zip file format does not handle empty folders. I tried it myself with
    winzip just a few days ago. I think that zip only keeps a list of files
    with their path so the folder will not be stored in the zip file if there is
    no file in that folder. Like I already said, I may be wrong, but I would
    recommend that you use tarfile instead.

    Dan

    "matthiasjanes" <> wrote in message
    news:...
    > hello all,
    >
    > i want to zip a folder plus the files and subfolders.
    >
    > which works something like that:
    >
    > #sample code
    >
    > import os, zipfile
    >
    > def zipit(path, ziper):
    >
    > for root, dirs, files in os.walk(path):
    >
    > for anyFile in files:
    >
    > fullname = os.path.join(root, anyFile)
    >
    > if __name__ == '__main__':
    >
    > ziper = zipfile.ZipFile('test.zip', 'w')
    > zipit('somefolderTozip', ziper)
    > ziper.close()
    > #____________________________
    >
    >
    > this works fine as long as there is no empty subfolder.
    >
    > can anyone tell me - or better give me a small example of code how to
    > add an empty folder to the zip archive.
    >
    > thanks mr. janes
    Dan Perl, Oct 6, 2004
    #2
    1. Advertising

  3. matthiasjanes wrote:
    > def zipit(path, ziper):
    >
    > for root, dirs, files in os.walk(path):
    >
    > for anyFile in files:
    >
    > fullname = os.path.join(root, anyFile)


    Where is the "ziper" variable used? I don't see how this code does
    anything except create an empty zipfile.

    $ python samplecode.py

    $ unzip test.zip
    Archive: test.zip
    warning [test.zip]: zipfile is empty

    If you Google for <zipfile empty> you will find a python-list thread
    with this solution in it:

    http://mail.python.org/pipermail/python-list/2003-June/170526.html

    Alternatively you could submit a patch or a bug.

    HTH,
    --
    Michael Hoffman
    Michael Hoffman, Oct 7, 2004
    #3
  4. matthiasjanes

    Dan Perl Guest

    I'm going to take a break and have dinner. My words are what's on the menu.

    Dan

    "Michael Hoffman" <> wrote in
    message news:ck1tfn$q64$...
    > matthiasjanes wrote:
    >> def zipit(path, ziper):
    >>
    >> for root, dirs, files in os.walk(path):
    >>
    >> for anyFile in files:
    >>
    >> fullname = os.path.join(root, anyFile)

    >
    > Where is the "ziper" variable used? I don't see how this code does
    > anything except create an empty zipfile.
    >
    > $ python samplecode.py
    >
    > $ unzip test.zip
    > Archive: test.zip
    > warning [test.zip]: zipfile is empty
    >
    > If you Google for <zipfile empty> you will find a python-list thread with
    > this solution in it:
    >
    > http://mail.python.org/pipermail/python-list/2003-June/170526.html
    >
    > Alternatively you could submit a patch or a bug.
    >
    > HTH,
    > --
    > Michael Hoffman
    Dan Perl, Oct 7, 2004
    #4
  5. Michael Hoffman <> wrote in message news:<ck1tfn$q64$>...
    > matthiasjanes wrote:
    > > def zipit(path, ziper):
    > >
    > > for root, dirs, files in os.walk(path):
    > >
    > > for anyFile in files:
    > >
    > > fullname = os.path.join(root, anyFile)

    >
    > Where is the "ziper" variable used? I don't see how this code does
    > anything except create an empty zipfile.
    >



    Sorry I did not post all the code:

    #sample code

    import os, zipfile, string

    def zipit(path, ziper):
    for root, dirs, files in os.walk(path):

    for anyFile in files:

    fullname = os.path.join(root, anyFile)
    archiveName = string.lstrip( fullname,path + "\\" )

    ziper.write(fullname, archiveName, zipfile.ZIP_DEFLATED )


    if __name__ == '__main__':

    ziper = zipfile.ZipFile('test.zip', 'w')
    zipit('somefolderTozip', ziper)
    ziper.close()
    #______________________

    all seems to work - just empty subfolders are gone.

    I checkt it out - 7-zip for instance keps also empty subfolders.

    maybe someone has an idea.

    thanks MrJanes
    matthiasjanes, Oct 7, 2004
    #5
  6. matthiasjanes wrote:

    > maybe someone has an idea.


    Someone has more than an idea. I already posted the answer to your
    question, but you apparently only bothered to read part of my message.
    --
    Michael Hoffman
    Michael Hoffman, Oct 7, 2004
    #6
  7. Michael Hoffman <> wrote in message news:<ck4emv$9rb$>...
    > matthiasjanes wrote:
    >
    > > maybe someone has an idea.

    >
    > Someone has more than an idea. I already posted the answer to your
    > question, but you apparently only bothered to read part of my message.


    Sorry M.Hoffman - I'm in the wrong. Somehow it confused me so much
    that I had forgotten to post the whole code - so I did it straight
    away and forgot to follow your instruction. once again Sorry and
    Thanks for your help.

    Mr.Janes
    matthiasjanes, Oct 8, 2004
    #7
    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. zbiszko

    difficult question?:)

    zbiszko, Sep 13, 2005, in forum: XML
    Replies:
    3
    Views:
    371
    Joris Gillis
    Sep 13, 2005
  2. manunam
    Replies:
    0
    Views:
    302
    manunam
    Oct 23, 2003
  3. Werner
    Replies:
    6
    Views:
    251
    Werner
    Aug 30, 2007
  4. amit

    Very difficult floating point question

    amit, Nov 28, 2009, in forum: C Programming
    Replies:
    33
    Views:
    804
    David Thompson
    Dec 17, 2009
  5. Frank Boegner

    Difficult Perl DBI Question, multiple charsets

    Frank Boegner, Sep 30, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    79
    Frank Boegner
    Sep 30, 2003
Loading...

Share This Page