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, Generators 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
from typing import Generator


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_io("async.py", open("sync.py", "rb")):
            file.write(buf)

        # Add from BytesIO
        for buf in builder.add_io("buffer.txt", io.BytesIO(b"Hello 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 from Generator
        def data_gen() -> Generator[bytes, None, None]:
            for i in range(1024):
                yield f"hello from generator {i}\n".encode()

        for buf in builder.add_gen("generator.txt", data_gen(), compression=zipgen.COMPRESSION_LZMA):
            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
from typing import AsyncGenerator


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_io_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)

        # Add from AsyncGenerator
        async def data_gen_async() -> AsyncGenerator[bytes, None]:
            for i in range(1024):
                await asyncio.sleep(0)
                yield f"hello from async generator {i}\n".encode()

        async for buf in builder.add_gen_async("generator.txt", data_gen_async(), compression=zipgen.COMPRESSION_LZMA):
            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.2.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

zipgen-0.1.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: zipgen-0.1.2.tar.gz
  • Upload date:
  • Size: 12.2 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.2.tar.gz
Algorithm Hash digest
SHA256 5433d7a4d7af0f44cc18b95ad001df5cdac85125bf108d7f6bb80f278928e0f2
MD5 31825ef52ba72fed4d002a0ce21cf4eb
BLAKE2b-256 b67d34d606ed9ce44a5992f6ad06121f6b2c8370153205220b7be12a48cad004

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipgen-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.8 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d595f5111923d3e8a42ad48d830481b7ef525ce305e2eed2f056bf239729e003
MD5 c27efb216b04b893be16873e330d0718
BLAKE2b-256 9ef897c3eeae853f4ad58cf6845d3a08af4afbaa3537b95714f605640ec9a293

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