examples

examples

Creating a zip archive

#!/usr/bin/python

from ziparchive import ZipArchive, compression_methods

# Create a new archive, and use DEFLATE as the default compression
z = ZipArchive('newarchie.zip', 'w', compression_methods.DEFLATE)

# open a file-like object for writing
f = z.open('member.txt', 'w')

# write something
f.write('Lorem ipsum dolor sit amet et consectetuer adispicing et in...\n')

# or write lines
f.writelines(
    
['Lorem ipsum\n'],
    
['dolor sit amet\n'],
    
['et consectetuer adispicing\n'],
    
)

# close the member file
f.close()

# and close the archive
z.close()

Reading members from a zip archive

#!/usr/bin/python

from ziparchive import ZipArchive

# open for reading, the default (just like file objects)
z = ZipArchive('archive.zip')

# open a member file from the archive for reading
f = z.open('member.txt')

# from now on, you can use it as an usual file object

for i, line in enumerate(f):
    
print i, line

# remember to close the file and the archive when finished
f.close()
z.close()