Skip to main content

PyZip is a package for handling zip-in-memory content as a dictionary.

Project description

https://travis-ci.org/ipazc/pyzip.svg?branch=master https://coveralls.io/repos/github/ipazc/pyzip/badge.svg?branch=master Code Health

PyZip is a package for managing a zip content as a dictionary.

>>> from pyzip import PyZip
>>>
>>> pyzip = PyZip()
>>> pyzip['key1'] = b"content_bytes"
>>> pyzip['key2'] = file_bytes
>>>
>>> pyzip.save("/path/to/file.zip")
>>> zip_bytes = pyzip.to_bytes() # Alternatively, to bytes

It is run on top of the module zipfile, however, in addition to its functionality, PyZip accepts to edit and remove elements of a zip.

Installation

Currently it is only supported Python 3.4.1 onwards:

sudo pip3 install pyzip

Basic Usage

PyZip can easily store content into a zip on the fly. The usage is the same as a normal dictionary:

  • Add content to in-memory zip:

>>> from pyzip import PyZip
>>>
>>> pyzip = PyZip()
>>> pyzip['key1'] = b"content_bytes"
  • Get specific content:

>>> print(pyzip['key1'])
b"content_bytes"
  • Edit content:

>>> pyzip['key1'] = b"replaced_content_bytes"
  • Remove content:

>>> del pyzip['key1']
  • Get zip bytes:

>>> zip_bytes = pyzip.to_bytes()
  • Load from bytes:

>>> pyzip = PyZip.from_bytes(zip_bytes)
  • Save to zip file:

>>> pyzip.save("path/to/file.zip")
  • Load from zip file:

>>> pyzip = PyZip.from_file("path/to/file.zip")
  • Convert existing dictionary into PyZip:

>>> pyzip = PyZip({'file1': b'example', 'file2': b'example2'})

Use case

Compressing a folder into a zip:

>>> from pyzip import PyZip
>>> import os
>>>
>>> path_to_compress = "route/to/files"
>>>
>>> pyzip = PyZip()
>>>
>>> for file in os.listdir(path_to_compress):
>>>     with open(path_to_compress, "rb") as f:
>>>        pyzip[file] = f.read()
>>>
>>> pyzip.save("compressed_folder.zip")

Uncompressing a folder from a zip:

>>> from pyzip import PyZip
>>> import os
>>>
>>> destination = "route/for/uncompress"
>>>
>>> pyzip = PyZip.from_file("compressed_folder.zip")
>>>
>>> for filename, content in pyzip.items():
>>>     with open(os.path.join(destination, filename), "wb") as f:
>>>        f.write(content)
>>>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyzip-0.0.1.tar.gz (4.4 kB view details)

Uploaded Source

File details

Details for the file pyzip-0.0.1.tar.gz.

File metadata

  • Download URL: pyzip-0.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyzip-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9b426ebd92b2f14ea631d3b13f74a0af4dcf74f01fc584132020159e301b11b0
MD5 93c0f170e8a4fa0a476db0abe72e8ee0
BLAKE2b-256 5b3dc8747172db365462bca7c7f9238ce61ae9d326ec6fb2bad54b50efa649e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page