gws.lib.zipx¶
Zip archive utilities.
Thin wrappers around zipfile that create and unpack zip archives in one call.
Archives are created from a list of sources (zip_to_path, zip_to_bytes). A source is a file path,
a directory path, which is scanned recursively, or a dict of archive names and contents. Entries are compressed
with ZIP_DEFLATED. Archive names are the normalized source paths, optionally with base_dir stripped,
or only the base names with flat=True.
Archives are unpacked from a file or from bytes, into a directory (unzip_path, unzip_bytes)
or into a dict of names and contents (unzip_path_to_dict, unzip_bytes_to_dict). Directory entries
are skipped. Entries with unsafe names (absolute, starting with a dot or containing ..) are skipped
with a warning. With flat=True, entries are unpacked by their base names, so entries with the same
base name overwrite each other.
Example:
import gws.lib.zipx as zipx
zipx.zip_to_path('/tmp/out.zip', ['/data/report', {'readme.txt': 'hello'}], base_dir='/data/')
content = zipx.zip_to_bytes(['/data/a.txt', '/data/b.txt'], flat=True)
zipx.unzip_path('/tmp/out.zip', '/tmp/unpacked')
files = zipx.unzip_bytes_to_dict(content) # {'a.txt': b'...', 'b.txt': b'...'}
Source code: gws.lib.zipx
Package Contents¶
- gws.lib.zipx.unzip_bytes(source: bytes, target_dir: str, flat: bool = False) int¶
Unpack a zip archive from bytes into a directory.
- Parameters:
source – The archive content.
target_dir – Path to the target directory.
flat – If
True, files are unpacked by their base names directly intotarget_dir, which must exist; otherwise the directories of the archive are created as needed.
- Returns:
The number of unpacked files.
- gws.lib.zipx.unzip_bytes_to_dict(source: bytes, flat: bool = False) dict[str, bytes]¶
Unpack a zip archive from bytes into a dict.
- Parameters:
source – The archive content.
flat – If
True, the keys are the base names of the files, otherwise their paths in the archive.
- Returns:
A dict of file names and contents.
- gws.lib.zipx.unzip_path(path: str, target_dir: str, flat: bool = False) int¶
Unpack a zip archive file into a directory.
- Parameters:
path – Path to the archive.
target_dir – Path to the target directory.
flat – If
True, files are unpacked by their base names directly intotarget_dir, which must exist; otherwise the directories of the archive are created as needed.
- Returns:
The number of unpacked files.
- gws.lib.zipx.unzip_path_to_dict(path: str, flat: bool = False) dict[str, bytes]¶
Unpack a zip archive file into a dict.
- Parameters:
path – Path to the archive.
flat – If
True, the keys are the base names of the files, otherwise their paths in the archive.
- Returns:
A dict of file names and contents.
- gws.lib.zipx.zip_to_bytes(sources: list[str | dict], base_dir: str = '', flat: bool = False) bytes¶
Create a zip archive in memory.
- Parameters:
sources – File paths, directory paths (scanned recursively) or dicts that map archive names to contents (
strorbytes).base_dir – Prefix to remove from the beginning of the file paths in the archive.
flat – If
True, only the base names of the files are kept in the archive.
- Returns:
The archive content, or empty bytes if there are no files to add.
- Raises:
Error – If a source is neither a dict, a file nor a directory.
- gws.lib.zipx.zip_to_path(path: str, sources: list[str | dict], base_dir: str = '', flat: bool = False) int¶
Create a zip archive in a file.
If there are no files to add, no archive is created.
- Parameters:
path – Path to the archive.
sources – File paths, directory paths (scanned recursively) or dicts that map archive names to contents (
strorbytes).base_dir – Prefix to remove from the beginning of the file paths in the archive.
flat – If
True, only the base names of the files are kept in the archive.
- Returns:
The number of files in the archive.
- Raises:
Error – If a source is neither a dict, a file nor a directory.