compress.zip
import compress
compressexposes this ascompress.zip, soimport compressis enough and the names are called ascompress.zip.*.import compress.zipreaches the same definitions directly.
The zip module contains classes and functions to make working with zip
archives easy.
Constants
ZIP_FILE_MAX
compress.zip.ZIP_FILE_MAX = 4294967295
ZIP_FILE_COUNT_LIMIT
compress.zip.ZIP_FILE_COUNT_LIMIT: number = 65535
The maximum number of files in a zip archive when zip64 is not used
ZIP_MAX
compress.zip.ZIP_MAX: number
The maximum size of a zip archive when zip64 is not used
ZIP_EXT
compress.zip.ZIP_EXT: string = '.zip'
The default zip file extension
ZIP_STORED
compress.zip.ZIP_STORED: number = 0
Compression method that indicates no compression
ZIP_DEFLATE
compress.zip.ZIP_DEFLATE: number = 8
Compression method that indicates zlib Deflate compression
ZIP_BZIP2
compress.zip.ZIP_BZIP2: number = 12
Compression method that indicates Bzip2 compression
Functions
extract()
compress.zip.extract(file: string, destination: ?string, is_zip64: ?bool) -> bool
Extracts the zip archive at the file path to the given destination directory. If destination is not given, the file will be extracted into the current working directory.
This function returns true if the extraction was successful and
false otherwise.
NOTE: Set
is_zip64to true if the size of the zip file exceedsZIP_MAX.
Parameters
file(string)destination(?string) — Default value isos.cwd().is_zip64(?bool) — Default value isfalse.
Returns bool
compress()
compress.zip.compress(path: string, destination: ?string, compression_method: ?number, use_zip64: ?bool) -> bool
Compresses the given path (file or directory) into the destination zip archive.
When an Error is thrown because max size was exceeded, some files could have already been compressed. In this case, the zip archive will should still be usable but not all desired files will be contained in it.
NOTE: Set
use_zip64to true when compressing files exceedingZIP_FILE_MAXorZIP_FILE_COUNT_LIMIT
Parameters
file(string)destination(?string) — Default value isos.cwd().compression_method(?number) — Default value isZIP_DEFLATEis_zip64(?bool) — Default value isfalse.
Returns bool
Raises Error if file could not be written of zip max size
exceeded.
Classes
ZipItem
class compress.zip.ZipItem
ZipItem represents a single file or directory in a zip archive.
Fields
| Field | Type | Description |
|---|---|---|
name | string | Name of the file or directory |
directory | string | The directory in which the file or subdirectory belongs |
compression_method | string | The compression method for this file |
crc | string | The crc32 checksum for the file |
last_modified | Date | The last modified date for the file |
compressed_size | number | The size of the file as compressed in the archive. |
uncompressed_size | number | The size of the file when extracted from the archive |
is_encrypted | bool | If this file is encrypted or not. |
permission | number | The file permission |
error | string | Error encountered when attempting to read/extract the file |
data | bytes | The decompressed value of the zip item |
ZipItem.from_dict()
compress.zip.ZipItem.from_dict(dict: dict) -> ZipItem
Creates a new ZipItem from a dictionary. The dictionary should contain
the following keys: - name: string - dir: string — optional -
compress_method: number - crc: number - filemtime: number -
size_compressed: number - size_uncompressed: number - encrypted:
boolean - error: string — optional - data: bytes -
permission: number
Parameters
dict(dict)
Returns ZipItem
ZipItem.export()
compress.zip.ZipItem.export(base_dir: ?string) -> bool
Exports the ZipItem to file. If base_dir is given, the file will be exported into the base_dir and all ZipItem directories will be created inside of base_dir to reflect the ZipItem’s original structure.
This function returns true if the operation succeeds or false
otherwise.
Parameters
base_dir(?string) — Default value isos.cwd().
Returns bool
ZipFile
class compress.zip.ZipFile
ZipFile represents an instance of zip file.
Fields
| Field | Type | Description |
|---|---|---|
name | string | The name of the zip file |
last_modified | Date | The last modified date for the zip file |
time_created | Date | The time when the zip file was created |
size | number | The size of the zip file |
handle | file | The file handle for this zip file |
files | List<ZipItem> | A list of the ZipItems in the zip file |
ZipFile.export()
compress.zip.ZipFile.export(base_dir: ?string) -> bool
Exports the all files in the ZipFile to files on the machine. If base_dir is given, the files will be exported into the base_dir and all directories will be created inside of base_dir as is to reflect the ZipFile’s original structure.
This function returns true if the operation succeeds or false
otherwise.
Parameters
base_dir(?string) — Default value isos.cwd().
Returns bool
ZipArchive
class compress.zip.ZipArchive
ZipArchive provides a class for zip archive creation, manipulation and extraction.
Fields
| Field | Type | Description |
|---|---|---|
comment |
Constructor
compress.zip.ZipArchive(path: string, compression_method: ?number, use_zip_64: ?bool)
Parameters
path(string)compression_method(?number) — Default value isZIP_DEFLATEuse_zip_64(?bool) — Default value isfalse.
ZipArchive.create_dir()
compress.zip.ZipArchive.create_dir(name: string) -> bool
Adds a directory to the zip with the given name.
Parameters
name(string)
Returns bool
ZipArchive.create_file()
compress.zip.ZipArchive.create_file(path: string, data: bytes|string, stat: ?dict) -> bool
Adds a file to the path specified with the contents given data.
If the stat is given, it must be a valid dictionary derived from
file.stat().
Parameters
path(string)data(bytes|string)stat(?dict)
Returns bool
ZipArchive.add_file()
compress.zip.ZipArchive.add_file(path: string, destination: ?string) -> bool
Adds an existing file to the archive. If destination is given, the file will be written to the destination path in the archive.
Parameters
path(string)destination(?string)
Returns bool
ZipArchive.add_directory()
compress.zip.ZipArchive.add_directory(directory: string, file_blacklist: ?list, ext_blacklist: ?list) -> bool
Adds the specified directory recursively to the archive and set’s it
path in the archive to dir.
- If
file_blacklistis not empty, this function will ignore every file with a matching path. - Ifext_blacklistis not empty, this function will ignore every file with a matching.
Parameters
directory(string)file_blacklist(?list) — Default value is[]ext_blacklist(?list) — Default value is[]
Returns bool
ZipArchive.read()
compress.zip.ZipArchive.read() -> ZipFile
Reads the zip file in the specified path and returns a list of ZipFile describing it’s contents.
Parameters
path(string)
Returns ZipFile
ZipArchive.save()
compress.zip.ZipArchive.save() -> bool
Saves the current Zip archive to file.
Parameters
filename(string)
Returns bool
2022, Richard Ore and The Zuri Contributors