create zip archive

Steve Holden sholden at holdenweb.com
Thu Mar 7 10:01:25 EST 2002


"99miles" <mac at magusdesigns.com> wrote in message
news:f5adfd88.0203061519.608151a7 at posting.google.com...
> hi-
>  i am creating a zip file from a directory. it is working great, but
> the files in the subdirectories get put into the top level directory
> in the zip file. I want the directory structure to remain intact when
> i extract the zip file.
>  How can I do this? Any ideas? thanks a lot-
>  Mac
>
> I am using this code, currently:
>
> for name in glob.glob("mypath/*"):
>      if os.path.isfile(name):
>           file.write(name, os.path.basename(name),
> zipfile.ZIP_DEFLATED)

Seems like all you have to do is remove some of the stuff you already put in
<wink/>. The second (arcname) argument to the zipfile.write() method assigns
the path within the archive with which the named file's content will be
associated). so, if your last line became <untested>::

    file.write(name, name, zipfile.ZIP_DEFLATED)

I'm guessing you'd be in better shape </untested>.

Tips:
    for testing, just omit the last argument to .write() -- it still works.
    It may even work out faster given the defaults and your environment.
    WinZip et al should be able to handle the resulting files.
    You can even compress them!

The code looks OK, though I guess you are aware that it only goes one level
deep in the directory hierarchy. You only have to ask and you will get
several brilliant suggestions as to how to generalize your perfectly
adequate algorithm (none from me, BTW <wink/>). You may already know that
PythonTime, the universal standard on c.l.py, sometimes gets manipulated in
ways we only become aware of later.

To save you some time, here's a synopsis of the next seven days on this
thread. Twenty-four hours after your original posting the discussion turns
to suggestions for recursive solutions of your problem. A further
twenty-four hours sees the focus change to whether recursive methods are
better generally (for several different values of "generally"). Several
people nostalgically remark how like their favorite LISP Python is, and
they'll still be talking about LISP in another thread in another ten years.
They will be right.

After three days, someone complainsthat the interpreter still doesn't
optimize tail recursion. A day later, when a mild flurry of "this has been
discussed many times before" and "have you never used Google" postings has
died down, people are talking about how the zipfile is the salvation of the
import world. Nobody will ever ask you whether you got your original problem
solved, and after two weeks the thread becomes a discussion on how distutils
could import modules from XML namespaces with authenticated access. But
we'll love it when you tell us how you did solve your problem. Especially as
a HOWTO.

Welcome to c.l.py

in-the-spirit-of-less-is-more-ly y'rs  - steve

FRED - THIS ONE'S COMING THROUGH ON A FAT PIPE YOU MAY HAVE TO PULL THE PLUG






More information about the Python-list mailing list