Link Search Menu Expand Document

zip

The zip module contains classes and functions to make working with zip archives easy.

Properties


zip.ZIP_FILE_MAX
The maximum size of a single file in a zip archive when zip64 is not used
type number
zip.ZIP_FILE_COUNT_LIMIT
The maximum number of files in a zip archive when zip64 is not used
type number
zip.ZIP_MAX
The maximum size of a zip archive when zip64 is not used
type number
zip.ZIP_EXT
The default zip file extension
type string

Functions


zip.extract(file: string [, destination: string = cwd [, is_zip64: bool = false]])
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

return bool
zip.compress(path: string [, destination: string [, use_zip64: bool = false]])
Compresses the given path (file or directory) into the destination zip archive.
throws Exception if file could not be written of zip max size exceeded.

When an exception is thrown becase max size 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

return bool

Classes


class ZipItem


ZipItem represents a single file or directory in a zip archive.

class ZipItem properties


ZipItem.name
Name of the file or directory
type string
ZipItem.directory
The directory in which the file or subdirectory belongs
type string
ZipItem.compression_method
The compression method for this file
type string
ZipItem.crc
The crc32 checksum for the file
type string
ZipItem.last_modified
The last modified date for the file
type Date
ZipItem.compressed_size
The size of the file as compressed in the archive
  • The value is not often dependable
type number
ZipItem.uncompressed_size
The size of the file when extracted from the archive
type number
ZipItem.is_encrypted
If this file is encrypted or not.
type bool
ZipItem.error
Error encountered when attempting to read/extract the file
type string
ZipItem.data
The decompressed value of the zip item
type bytes

class ZipItem methods


static from_dict(dict: dictionary)
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
return ZipItem
export([base_dir: string = os.cwd()
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.

return bool

class ZipFile


ZipFile represents an instance of zip file.

class ZipFile properties


ZipFile.name
The name of the zip file
type string
ZipFile.last_modified
The last modified date for the zip file
type Date
ZipFile.time_created
The time when the zip file was created
type Date
ZipFile.size
The size of the zip file
type number
ZipFile.handle
The file handle for this zip file
type file
ZipFile.files
A list of the ZipItems in the zip file
type List</span></div>

class ZipFile methods


export([base_dir: string])
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.

return bool

class ZipArchive


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

class ZipArchive methods


ZipArchive(file: string [, use_zip_64: bool = false])
constructor
create_dir(name: string)
Adds a directory to the zip with the given name
return bool
create_file(path: string, data: bytes | string)
Adds a file to the path specified with the contents given data
return bool
add_file(path: string [, destination: string])
Adds an existing file to the archive. If destination is given, the file will be written to the destination path in the archive.
add_directory(directory: string [, file_blacklist: list = [] [, ext_blacklist: list = []]])
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
read(path: string)
Reads the zip file in the specified path and returns a list of ZipFile describing it’s contents.
return ZipFile
save(filename: string)
Saves the current Zip archive to file.

Back to top

Copyright © 2021 Ore Richard Muyiwa. Distributed under the MIT license.