module documentation

Read and write ZIP files.

MicroPython implementation ported from CPython 3.12 https://raw.githubusercontent.com/python/cpython/3.12/Lib/zipfile/__init__.py

PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2

From https://github.com/jonnor/micropython-zipfile/blob/master/zipfile.py

Usage:

# Import the module from zipfile import ZipFile, ZIP_DEFLATED, ZIP_STORED

# Create a .zip archive # for compression, swap ZIP_STORED with ZIP_DEFLATED path = 'myarchive.zip' with ZipFile(path, 'w', ZIP_STORED) as archive:

contents = b'Hello World Hello World Hello World
'
with archive.open('test.txt', 'w') as f:
f.write(contents)

# List the information from a .zip archive print(' Listing information') with ZipFile(path, 'r') as archive:

for info in archive.infolist():
print(info.filename) print(' System: ' + str(info.create_system) + '(0 = Windows, 3 = Unix)') print(' ZIP version: ' + str(info.create_version)) print(' Compressed: ' + str(info.compress_size) + ' bytes') print(' Uncompressed: ' + str(info.file_size) + ' bytes')

# Read a file from inside .zip archive print(' Reading file') with ZipFile(path) as myzip:

with myzip.open('test.txt') as myfile:
print(myfile.read())
Class DeflateCompressor Undocumented
Class DeflateDecompressor Undocumented
Class ZipExtFile File-like object for reading an archive member. Is returned by ZipFile.open().
Class ZipFile Class with methods to open, read, write, close, list zip files.
Class ZipInfo Class with attributes describing each file in the ZIP archive.
Exception BadZipFile Undocumented
Exception LargeZipFile Raised when writing a zipfile, the zipfile requires ZIP64 extensions and those extensions are disabled.
Exception UserWarning Undocumented
Function is_zipfile Quickly see if a file is a ZIP file by checking the magic number.
Function warn Undocumented
Constant ALTSEP Undocumented
Constant BZIP2_VERSION Undocumented
Constant CURDIR Undocumented
Constant DEFAULT_VERSION Undocumented
Constant LZMA_VERSION Undocumented
Constant MAX_EXTRACT_VERSION Undocumented
Constant OS_SEP Undocumented
Constant PARDIR Undocumented
Constant SEEK_CUR Undocumented
Constant SEEK_END Undocumented
Constant SEEK_SET Undocumented
Constant ZIP64_LIMIT Undocumented
Constant ZIP64_VERSION Undocumented
Constant ZIP_BZIP2 Undocumented
Constant ZIP_DEFLATED Undocumented
Constant ZIP_FILECOUNT_LIMIT Undocumented
Constant ZIP_LZMA Undocumented
Constant ZIP_MAX_COMMENT Undocumented
Constant ZIP_STORED Undocumented
Variable compressor_names Undocumented
Variable is_cpython Undocumented
Variable sizeCentralDir Undocumented
Variable sizeEndCentDir Undocumented
Variable sizeEndCentDir64 Undocumented
Variable sizeEndCentDir64Locator Undocumented
Variable sizeFileHeader Undocumented
Variable stringCentralDir Undocumented
Variable stringEndArchive Undocumented
Variable stringEndArchive64 Undocumented
Variable stringEndArchive64Locator Undocumented
Variable stringFileHeader Undocumented
Variable structCentralDir Undocumented
Variable structEndArchive Undocumented
Variable structEndArchive64 Undocumented
Variable structEndArchive64Locator Undocumented
Variable structFileHeader Undocumented
Class _SharedFile Undocumented
Class _Tellable Undocumented
Class _ZipWriteFile Undocumented
Function _check_compression Undocumented
Function _check_zipfile Undocumented
Function _EndRecData Return data from the "End of Central Directory" record, or None.
Function _EndRecData64 Read the ZIP64 end-of-archive records and use that to update endrec
Function _gen_crc Undocumented
Function _get_compressor Undocumented
Function _get_decompressor Undocumented
Function _path_dirname Undocumented
Function _path_exists Undocumented
Function _path_isdir Undocumented
Function _path_join Undocumented
Function _path_normpath Undocumented
Function _sanitize_filename Terminate the file name at the first null byte and ensure paths always use forward slashes as the directory separator.
Function _strip_extra Undocumented
Function _ZipDecrypter Undocumented
Constant _CD64_CREATE_VERSION Undocumented
Constant _CD64_DIRECTORY_RECSIZE Undocumented
Constant _CD64_DIRECTORY_SIZE Undocumented
Constant _CD64_DISK_NUMBER Undocumented
Constant _CD64_DISK_NUMBER_START Undocumented
Constant _CD64_EXTRACT_VERSION Undocumented
Constant _CD64_NUMBER_ENTRIES_THIS_DISK Undocumented
Constant _CD64_NUMBER_ENTRIES_TOTAL Undocumented
Constant _CD64_OFFSET_START_CENTDIR Undocumented
Constant _CD64_SIGNATURE Undocumented
Constant _CD_COMMENT_LENGTH Undocumented
Constant _CD_COMPRESS_TYPE Undocumented
Constant _CD_COMPRESSED_SIZE Undocumented
Constant _CD_CRC Undocumented
Constant _CD_CREATE_SYSTEM Undocumented
Constant _CD_CREATE_VERSION Undocumented
Constant _CD_DATE Undocumented
Constant _CD_DISK_NUMBER_START Undocumented
Constant _CD_EXTERNAL_FILE_ATTRIBUTES Undocumented
Constant _CD_EXTRA_FIELD_LENGTH Undocumented
Constant _CD_EXTRACT_SYSTEM Undocumented
Constant _CD_EXTRACT_VERSION Undocumented
Constant _CD_FILENAME_LENGTH Undocumented
Constant _CD_FLAG_BITS Undocumented
Constant _CD_INTERNAL_FILE_ATTRIBUTES Undocumented
Constant _CD_LOCAL_HEADER_OFFSET Undocumented
Constant _CD_SIGNATURE Undocumented
Constant _CD_TIME Undocumented
Constant _CD_UNCOMPRESSED_SIZE Undocumented
Constant _DD_SIGNATURE Undocumented
Constant _ECD_COMMENT Undocumented
Constant _ECD_COMMENT_SIZE Undocumented
Constant _ECD_DISK_NUMBER Undocumented
Constant _ECD_DISK_START Undocumented
Constant _ECD_ENTRIES_THIS_DISK Undocumented
Constant _ECD_ENTRIES_TOTAL Undocumented
Constant _ECD_LOCATION Undocumented
Constant _ECD_OFFSET Undocumented
Constant _ECD_SIGNATURE Undocumented
Constant _ECD_SIZE Undocumented
Constant _FH_COMPRESSED_SIZE Undocumented
Constant _FH_COMPRESSION_METHOD Undocumented
Constant _FH_CRC Undocumented
Constant _FH_EXTRA_FIELD_LENGTH Undocumented
Constant _FH_EXTRACT_SYSTEM Undocumented
Constant _FH_EXTRACT_VERSION Undocumented
Constant _FH_FILENAME_LENGTH Undocumented
Constant _FH_GENERAL_PURPOSE_FLAG_BITS Undocumented
Constant _FH_LAST_MOD_DATE Undocumented
Constant _FH_LAST_MOD_TIME Undocumented
Constant _FH_SIGNATURE Undocumented
Constant _FH_UNCOMPRESSED_SIZE Undocumented
Constant _MASK_COMPRESS_OPTION_1 Undocumented
Constant _MASK_COMPRESSED_PATCH Undocumented
Constant _MASK_ENCRYPTED Undocumented
Constant _MASK_STRONG_ENCRYPTION Undocumented
Constant _MASK_USE_DATA_DESCRIPTOR Undocumented
Constant _MASK_UTF_FILENAME Undocumented
Variable _crctable Undocumented
Variable _stat_namedtuple Undocumented
def is_zipfile(filename):

Quickly see if a file is a ZIP file by checking the magic number.

The filename argument may be a file or file-like object too.

def warn(s, stacklevel=None):

Undocumented

ALTSEP: str =

Undocumented

Value
'/'
BZIP2_VERSION: int =

Undocumented

Value
46
CURDIR: str =

Undocumented

Value
'.'
DEFAULT_VERSION: int =

Undocumented

Value
20
LZMA_VERSION: int =

Undocumented

Value
63
MAX_EXTRACT_VERSION: int =

Undocumented

Value
63
OS_SEP =

Undocumented

Value
os.sep if hasattr(os, 'sep') else '/'
PARDIR: str =

Undocumented

Value
'..'
SEEK_CUR: int =

Undocumented

Value
1
SEEK_END: int =

Undocumented

Value
2
SEEK_SET: int =

Undocumented

Value
0
ZIP64_LIMIT =

Undocumented

Value
(1 << 31) - 1
ZIP64_VERSION: int =

Undocumented

Value
45
ZIP_BZIP2: int =

Undocumented

Value
12
ZIP_DEFLATED: int =

Undocumented

Value
8
ZIP_FILECOUNT_LIMIT =

Undocumented

Value
(1 << 16) - 1
ZIP_LZMA: int =

Undocumented

Value
14
ZIP_MAX_COMMENT =

Undocumented

Value
(1 << 16) - 1
ZIP_STORED: int =

Undocumented

Value
0
compressor_names: dict[int, str] =

Undocumented

is_cpython =

Undocumented

sizeCentralDir =

Undocumented

sizeEndCentDir =

Undocumented

sizeEndCentDir64 =

Undocumented

sizeEndCentDir64Locator =

Undocumented

sizeFileHeader =

Undocumented

stringCentralDir: bytes =

Undocumented

stringEndArchive: bytes =

Undocumented

stringEndArchive64: bytes =

Undocumented

stringEndArchive64Locator: bytes =

Undocumented

stringFileHeader: bytes =

Undocumented

structCentralDir: str =

Undocumented

structEndArchive: bytes =

Undocumented

structEndArchive64: str =

Undocumented

structEndArchive64Locator: str =

Undocumented

structFileHeader: str =

Undocumented

def _check_compression(compression):

Undocumented

def _check_zipfile(fp):

Undocumented

def _EndRecData(fpin):

Return data from the "End of Central Directory" record, or None.

The data is a list of the nine items in the ZIP "End of central dir" record followed by a tenth item, the file seek offset of this record.

def _EndRecData64(fpin, offset, endrec):

Read the ZIP64 end-of-archive records and use that to update endrec

def _gen_crc(crc):

Undocumented

def _get_compressor(compress_type, compresslevel=None):

Undocumented

def _get_decompressor(compress_type):

Undocumented

def _path_dirname(path):

Undocumented

def _path_exists(path):

Undocumented

def _path_isdir(path):

Undocumented

def _path_join(first, second):

Undocumented

def _path_normpath(path):

Undocumented

def _sanitize_filename(filename):

Terminate the file name at the first null byte and ensure paths always use forward slashes as the directory separator.

def _strip_extra(extra, xids):

Undocumented

def _ZipDecrypter(pwd):

Undocumented

_CD64_CREATE_VERSION: int =

Undocumented

Value
2
_CD64_DIRECTORY_RECSIZE: int =

Undocumented

Value
1
_CD64_DIRECTORY_SIZE: int =

Undocumented

Value
8
_CD64_DISK_NUMBER: int =

Undocumented

Value
4
_CD64_DISK_NUMBER_START: int =

Undocumented

Value
5
_CD64_EXTRACT_VERSION: int =

Undocumented

Value
3
_CD64_NUMBER_ENTRIES_THIS_DISK: int =

Undocumented

Value
6
_CD64_NUMBER_ENTRIES_TOTAL: int =

Undocumented

Value
7
_CD64_OFFSET_START_CENTDIR: int =

Undocumented

Value
9
_CD64_SIGNATURE: int =

Undocumented

Value
0
_CD_COMMENT_LENGTH: int =

Undocumented

Value
14
_CD_COMPRESS_TYPE: int =

Undocumented

Value
6
_CD_COMPRESSED_SIZE: int =

Undocumented

Value
10
_CD_CRC: int =

Undocumented

Value
9
_CD_CREATE_SYSTEM: int =

Undocumented

Value
2
_CD_CREATE_VERSION: int =

Undocumented

Value
1
_CD_DATE: int =

Undocumented

Value
8
_CD_DISK_NUMBER_START: int =

Undocumented

Value
15
_CD_EXTERNAL_FILE_ATTRIBUTES: int =

Undocumented

Value
17
_CD_EXTRA_FIELD_LENGTH: int =

Undocumented

Value
13
_CD_EXTRACT_SYSTEM: int =

Undocumented

Value
4
_CD_EXTRACT_VERSION: int =

Undocumented

Value
3
_CD_FILENAME_LENGTH: int =

Undocumented

Value
12
_CD_FLAG_BITS: int =

Undocumented

Value
5
_CD_INTERNAL_FILE_ATTRIBUTES: int =

Undocumented

Value
16
_CD_LOCAL_HEADER_OFFSET: int =

Undocumented

Value
18
_CD_SIGNATURE: int =

Undocumented

Value
0
_CD_TIME: int =

Undocumented

Value
7
_CD_UNCOMPRESSED_SIZE: int =

Undocumented

Value
11
_DD_SIGNATURE: int =

Undocumented

Value
134695760
_ECD_COMMENT: int =

Undocumented

Value
8
_ECD_COMMENT_SIZE: int =

Undocumented

Value
7
_ECD_DISK_NUMBER: int =

Undocumented

Value
1
_ECD_DISK_START: int =

Undocumented

Value
2
_ECD_ENTRIES_THIS_DISK: int =

Undocumented

Value
3
_ECD_ENTRIES_TOTAL: int =

Undocumented

Value
4
_ECD_LOCATION: int =

Undocumented

Value
9
_ECD_OFFSET: int =

Undocumented

Value
6
_ECD_SIGNATURE: int =

Undocumented

Value
0
_ECD_SIZE: int =

Undocumented

Value
5
_FH_COMPRESSED_SIZE: int =

Undocumented

Value
8
_FH_COMPRESSION_METHOD: int =

Undocumented

Value
4
_FH_CRC: int =

Undocumented

Value
7
_FH_EXTRA_FIELD_LENGTH: int =

Undocumented

Value
11
_FH_EXTRACT_SYSTEM: int =

Undocumented

Value
2
_FH_EXTRACT_VERSION: int =

Undocumented

Value
1
_FH_FILENAME_LENGTH: int =

Undocumented

Value
10
_FH_GENERAL_PURPOSE_FLAG_BITS: int =

Undocumented

Value
3
_FH_LAST_MOD_DATE: int =

Undocumented

Value
6
_FH_LAST_MOD_TIME: int =

Undocumented

Value
5
_FH_SIGNATURE: int =

Undocumented

Value
0
_FH_UNCOMPRESSED_SIZE: int =

Undocumented

Value
9
_MASK_COMPRESS_OPTION_1 =

Undocumented

Value
1 << 1
_MASK_COMPRESSED_PATCH =

Undocumented

Value
1 << 5
_MASK_ENCRYPTED =

Undocumented

Value
1 << 0
_MASK_STRONG_ENCRYPTION =

Undocumented

Value
1 << 6
_MASK_USE_DATA_DESCRIPTOR =

Undocumented

Value
1 << 3
_MASK_UTF_FILENAME =

Undocumented

Value
1 << 11
_crctable =

Undocumented

_stat_namedtuple =

Undocumented