Skip to main content

A zip file generator.

Project description

zipgen

Zipgen is a simple and performant zip archive generator for Python 3.7 and later. It supports ZIP64, uncompressed and various compression formats such as: Deflated, Bzip and LZMA.

Zipgen supports synchronous asynchronous generation. Zipgen can zip archives on the fly from stream objects such as FileIO, BytesIO and Async StreamReader.

Zipgen also supports recursive creation of zip archives from existing folders synchronously or asynchronously.

Command

Zipgen can also be used as a command: python -m zipgen dest.zip file1.txt ./any/folder.

The command supports adding several files or folders at once recursively. Compression method can be set with --comp option and comment can be set with --comment.

Install

python -m pip install zipgen


Sync example

import io
import zipgen


def main() -> None:
    """Creates dist_sync.zip synchronously."""
    builder = zipgen.ZipBuilder()

    with open("dist_sync.zip", "wb+") as file:
        # Add file, default compression is COMPRESSION_STORED
        for buf in builder.add_file("async.py", open("sync.py", "rb")):
            file.write(buf)

        # Add BytesIO
        for buf in builder.add_file("buffer.txt", io.BytesIO(b"Hell world from BytesIO!"), compression=zipgen.COMPRESSION_BZIP2):
            file.write(buf)

        # Walk src
        for buf in builder.walk("../src", "src-files-dist", compression=zipgen.COMPRESSION_DEFLATED):
            file.write(buf)

        # Add empty folders
        file.write(builder.add_folder("empty/folder/it/is"))
        # its OK to start path with / or \, library corrects everything.
        file.write(builder.add_folder("/empty/folder/indeed"))

        # End
        file.write(builder.end("This is a comment for sync.py example."))


if __name__ == "__main__":
    main()

Async example

import asyncio
import zipgen


async def main() -> None:
    """Creates dist_async.zip asynchronously."""
    builder = zipgen.ZipBuilder()

    with open("dist_async.zip", "wb+") as file:
        # Add file, default compression is COMPRESSION_STORED
        async for buf in builder.add_file_async("async.py", open("async.py", "rb")):
            file.write(buf)

        # Walk src
        async for buf in builder.walk_async("../src", "src-files-dist", compression=zipgen.COMPRESSION_DEFLATED):
            file.write(buf)

        # Read process content to zip
        proc = await asyncio.subprocess.create_subprocess_exec(
            "dir",
            stdout=asyncio.subprocess.PIPE,
        )

        if proc.stdout is not None:
            async for buf in builder.add_stream_async("dir.txt", proc.stdout, compression=zipgen.COMPRESSION_LZMA):
                file.write(buf)

        # End
        file.write(builder.end("This is a comment for async.py example."))


if __name__ == "__main__":
    asyncio.run(main())

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

zipgen-0.1.1.2.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

zipgen-0.1.1.2-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file zipgen-0.1.1.2.tar.gz.

File metadata

  • Download URL: zipgen-0.1.1.2.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for zipgen-0.1.1.2.tar.gz
Algorithm Hash digest
SHA256 c9a296b043ab594bc600735ed81c6e3424ebb936f1b289fdf4ba65d376ca5eea
MD5 04691bfda9acdd1ef8ed443d3b062810
BLAKE2b-256 90531738b68abfd5b06f16353e7db353e6205eb75ce7514881df4e63d17316da

See more details on using hashes here.

File details

Details for the file zipgen-0.1.1.2-py3-none-any.whl.

File metadata

  • Download URL: zipgen-0.1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for zipgen-0.1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 817870aff6e54da7eb66c1d31d489a0c4f2728a20b9d7559b27ce01fb0720452
MD5 cf6a1d351b76dcfef7c7973c18d96d41
BLAKE2b-256 b1b32f9e11c1130f6dd7d2e4f9935fa6765b308ce033407bd884d4ae8b9863e3

See more details on using hashes here.

Supported by

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