About

Welcome to Panela, Matt Harrison's take on mostly Open Source, Linux, Python, innovation in those areas, other buzzwords and Dick Proenneke. It comes complete with the illustrations as needed. Note the opinions expressed here are merely my opinions and not the opinions of my employer.

about Matt

Calendar

««Nov 2009»»
SMTWTFS
1234567
8910
11
12
1314
151617
18
192021
22232425262728
2930

Mailing List

My RSS Feeds








zipwrap - a unixy python interface for zipfiles

posted 2008.10.01 Wed

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']

tags:      

links: digg this    del.icio.us    reddit




1. Cowmix left...
2008.10.02 Thu 11:22 am

Ok.. this is truly awesome!

And it works great under Windows too! (ActivePython 2.5)