Sometimes you need to play with zipfiles, jars, wars, or odt files. So I've written an interface that allows for easy creation of zip files from existing zips or directories. I think the "unixy" interface (ie, cat, touch, rm, mkdir) is simple and easier than zipfile. Perhaps this might be useful to others. I'm hoping to use it for an rst2odp (openoffice impress, you know the powerpoint clone) tool.
You can get zipwrap.py here. Note that I've only tested under cpython(2.5)/linux(ubuntu). If there are issues on other platforms please let me know.
Here's the docstring:
Modeled after unix command line.
>>> z = ZipWrap("foo.zip")
Can touch files (preceeding / is optional root is either "" or "/")
>>> z.touch("/bar", "hello world\\nstuff")
Can ``cat`` files
>>> z.cat("bar")
'hello world\\nstuff'
Can ``mkdir``
>>> z.mkdir("foo/bar/baz/biz")
Can ``cat`` root directory
>>> z.cat("/")
['foo', 'bar']
>>> z.touch("foo/bar/baz/junk", "stuff")
>>> z.cat("foo/bar/baz")
['junk', 'biz']
>>> z.cat("foo/bar/baz/junk")
'stuff'
>>> z.touch("empty")
>>> z.cat("empty")
''
Can ``rm``
>>> z.rm("bar")
>>> z.cat("bar")
Can ``save``
>>> z.zipit("test/foo.zip")
Can open existing ZIP files
>>> z2 = ZipWrap("test/foo.zip")
>>> z2.cat("foo/bar/baz/junk")
'stuff'
>>> os.remove("test/foo.zip")
Can open existing directories
>>> z3 = ZipWrap("test")
>>> z3.cat("/") # root dir
['testzipwrap.py']
Ok.. this is truly awesome!