Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

compress.zip

import compress

compress exposes this as compress.zip, so import compress is enough and the names are called as compress.zip.*. import compress.zip reaches 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_zip64 to true if the size of the zip file exceeds ZIP_MAX.

Parameters

  • file (string)
  • destination (?string) — Default value is os.cwd().
  • is_zip64 (?bool) — Default value is false.

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_zip64 to true when compressing files exceeding ZIP_FILE_MAX or ZIP_FILE_COUNT_LIMIT

Parameters

  • file (string)
  • destination (?string) — Default value is os.cwd().
  • compression_method (?number) — Default value is ZIP_DEFLATE
  • is_zip64 (?bool) — Default value is false.

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

FieldTypeDescription
namestringName of the file or directory
directorystringThe directory in which the file or subdirectory belongs
compression_methodstringThe compression method for this file
crcstringThe crc32 checksum for the file
last_modifiedDateThe last modified date for the file
compressed_sizenumberThe size of the file as compressed in the archive.
uncompressed_sizenumberThe size of the file when extracted from the archive
is_encryptedboolIf this file is encrypted or not.
permissionnumberThe file permission
errorstringError encountered when attempting to read/extract the file
databytesThe 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 is os.cwd().

Returns bool

ZipFile

class compress.zip.ZipFile

ZipFile represents an instance of zip file.

Fields

FieldTypeDescription
namestringThe name of the zip file
last_modifiedDateThe last modified date for the zip file
time_createdDateThe time when the zip file was created
sizenumberThe size of the zip file
handlefileThe file handle for this zip file
filesList<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 is os.cwd().

Returns bool

ZipArchive

class compress.zip.ZipArchive

ZipArchive provides a class for zip archive creation, manipulation and extraction.

Fields

FieldTypeDescription
comment

Constructor

compress.zip.ZipArchive(path: string, compression_method: ?number, use_zip_64: ?bool)

Parameters

  • path (string)
  • compression_method (?number) — Default value is ZIP_DEFLATE
  • use_zip_64 (?bool) — Default value is false.

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_blacklist is not empty, this function will ignore every file with a matching path. - If ext_blacklist is 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