Zip
๐ค AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
provide.foundation.archive.zip
¶
Classes¶
ZipArchive
¶
Bases: BaseArchive
ZIP archive implementation.
Creates and extracts ZIP archives with optional compression. Supports adding files to existing archives.
Security Note - Password Handling
The password parameter only decrypts existing encrypted ZIP files during
extraction/reading. It does NOT encrypt new files during creation with
stdlib zipfile. To create encrypted ZIP archives, use a third-party library
like pyzipper that supports AES encryption. The stdlib zipfile.setpassword()
method only enables reading password-protected archives.
Attributes:
| Name | Type | Description |
|---|---|---|
compression_level |
int
|
ZIP compression level 0-9 (0=store/no compression, 9=best) |
compression_type |
int
|
Compression type (zipfile.ZIP_DEFLATED, etc) |
password |
bytes | None
|
Password for decrypting existing encrypted archives (read-only) |
Functions¶
add_file
¶
Add file to existing ZIP archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
archive
|
Path
|
ZIP archive file path |
required |
file
|
Path
|
File to add |
required |
arcname
|
str | None
|
Name in archive (defaults to file name) |
None
|
Raises:
| Type | Description |
|---|---|
ArchiveError
|
If adding file fails |
Source code in provide/foundation/archive/zip.py
create
¶
Create ZIP archive from source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Source file or directory to archive |
required |
output
|
Path
|
Output ZIP file path |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to created archive |
Raises:
| Type | Description |
|---|---|
ArchiveError
|
If archive creation fails |
Note
Files are NOT encrypted during creation even if password is set. The stdlib zipfile module does not support creating encrypted archives. Use pyzipper or similar for AES-encrypted ZIP creation.
Source code in provide/foundation/archive/zip.py
extract
¶
Extract ZIP archive to output directory with decompression bomb protection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
archive
|
Path
|
ZIP archive file path |
required |
output
|
Path
|
Output directory path |
required |
limits
|
ArchiveLimits | None
|
Optional extraction limits (uses DEFAULT_LIMITS if None) |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to extraction directory |
Raises:
| Type | Description |
|---|---|
ArchiveError
|
If extraction fails, archive contains unsafe paths, or exceeds limits |
Source code in provide/foundation/archive/zip.py
extract_file
¶
Extract single file from ZIP archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
archive
|
Path
|
ZIP archive file path |
required |
member
|
str
|
Name of file in archive |
required |
output
|
Path
|
Output directory or file path |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to extracted file |
Raises:
| Type | Description |
|---|---|
ArchiveError
|
If extraction fails or member path is unsafe |
Source code in provide/foundation/archive/zip.py
list_contents
¶
List contents of ZIP archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
archive
|
Path
|
ZIP archive file path |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of file paths in archive |
Raises:
| Type | Description |
|---|---|
ArchiveError
|
If listing fails |
Source code in provide/foundation/archive/zip.py
validate
¶
Validate ZIP archive integrity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
archive
|
Path
|
ZIP archive file path |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if archive is valid, False otherwise |
Note: This method intentionally catches all exceptions and returns False. This is NOT an error suppression case - returning False on any exception is the expected validation behavior. Do NOT replace this with @resilient decorator.