### Get Entry Names with ZipFile.namelist() Source: https://context7.com/tktech/fasterzip/llms.txt Convenience method to get a list of all entry names (filenames) in the archive. This is equivalent to iterating through `infolist()` and extracting the `m_filename` field. ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: zf.writestr('a.txt', 'aaa') zf.writestr('b/c.txt', 'ccc') tf.seek(0) zf = ZipFile(tf) names = list(zf.namelist()) print(names) # [b'a.txt', b'b/c.txt'] ``` -------------------------------- ### Get Zip Archive Entry Metadata with ZipFile.getinfo() Source: https://context7.com/tktech/fasterzip/llms.txt Retrieve metadata for a specific archive entry using its path. Raises `KeyError` if the entry does not exist, similar to the standard library. ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: zf.writestr('readme.txt', 'Hello!') tf.seek(0) zf = ZipFile(tf) stat = zf.getinfo(b'readme.txt') print(stat['m_filename']) # b'readme.txt' print(stat['m_uncomp_size']) # 6 print(stat['m_is_directory']) # False print(stat['m_is_encrypted']) # False # KeyError for missing entries try: zf.getinfo(b'missing.txt') except KeyError as e: print(e) # 'There is no item named b\'missing.txt\' in the archive' ``` -------------------------------- ### Count Archive Entries with len(ZipFile) Source: https://context7.com/tktech/fasterzip/llms.txt Get the total number of entries in the ZIP archive using the `len()` function. This wraps the underlying miniz library's count functionality. ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: for i in range(100): zf.writestr(f'{i}.txt', str(i)) tf.seek(0) zf = ZipFile(tf) print(len(zf)) # 100 ``` -------------------------------- ### Open ZipFile by Path or File-like Object Source: https://context7.com/tktech/fasterzip/llms.txt Demonstrates opening a ZipFile from a filesystem path (bytes) or a file-like object supporting fileno(). BytesIO is not supported and will raise NotImplementedError. ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile # --- Open by filesystem path (bytes) --- with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tf: path = tf.name with stdlib_zipfile.ZipFile(path, 'w') as zf: zf.writestr('hello.txt', 'Hello, world!') zf = ZipFile(path.encode('utf-8')) print(len(zf)) # 1 # --- Open from a file-like object (must support fileno()) --- with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: zf.writestr('hello.txt', 'Hello, world!') tf.seek(0) zf = ZipFile(tf) print(len(zf)) # 1 # --- BytesIO is NOT supported (no real file descriptor) --- import io, pytest try: ZipFile(io.BytesIO(b'not a zip')) except NotImplementedError as e: print(e) # File-like object passed to ZipFile does not support fileno() ``` -------------------------------- ### ZipFile.infolist() Source: https://context7.com/tktech/fasterzip/llms.txt Generates metadata for all entries in the archive. Each yielded item is a dictionary containing file statistics. ```APIDOC ## ZipFile.infolist() ### Description Iterate over metadata for all archive entries. ### Method `infolist() -> Generator[dict, None, None]` ### Response #### Success Response - **Generator[dict, None, None]**: A generator yielding stat dictionaries for each archive entry. ### Request Example ```python for entry in zf.infolist(): print(entry['m_filename'].decode(), entry['m_uncomp_size'], 'bytes') ``` ### Error Handling - **zipfile.BadZipFile**: Raised if any entry's stat cannot be read. ``` -------------------------------- ### Iterate Metadata for All Archive Entries with ZipFile.infolist() Source: https://context7.com/tktech/fasterzip/llms.txt Generate metadata dictionaries for all entries in the archive. This method is useful for iterating through archive contents and accessing metadata for each file. Raises `zipfile.BadZipFile` if an entry's stat cannot be read. ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: for i in range(5): zf.writestr(f'file_{i}.txt', f'content {i}') tf.seek(0) zf = ZipFile(tf) for entry in zf.infolist(): print( entry['m_filename'].decode(), entry['m_uncomp_size'], 'bytes' ) # file_0.txt 9 bytes # file_1.txt 9 bytes # file_2.txt 9 bytes # file_3.txt 9 bytes # file_4.txt 9 bytes ``` -------------------------------- ### ZipFile(file) — Open a ZIP archive Source: https://context7.com/tktech/fasterzip/llms.txt Constructs a ZipFile reader from either a bytes filesystem path or a file-like object that supports fileno(). Raises zipfile.BadZipFile if the archive cannot be opened, and NotImplementedError if a file-like object without a real file descriptor (e.g., io.BytesIO) is passed. ```APIDOC ## ZipFile(file) — Open a ZIP archive ### Description Constructs a `ZipFile` reader from either a bytes filesystem path or a file-like object that supports `fileno()`. Raises `zipfile.BadZipFile` if the archive cannot be opened, and `NotImplementedError` if a file-like object without a real file descriptor (e.g., `io.BytesIO`) is passed. ### Parameters #### Path Parameters - **file** (bytes | file-like object) - Required - Filesystem path or a file-like object supporting `fileno()`. ### Request Example ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile # --- Open by filesystem path (bytes) --- with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tf: path = tf.name with stdlib_zipfile.ZipFile(path, 'w') as zf: zf.writestr('hello.txt', 'Hello, world!') zf = ZipFile(path.encode('utf-8')) print(len(zf)) # 1 # --- Open from a file-like object (must support fileno()) --- with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: zf.writestr('hello.txt', 'Hello, world!') tf.seek(0) zf = ZipFile(tf) print(len(zf)) # 1 # --- BytesIO is NOT supported (no real file descriptor) --- import io, pytest try: ZipFile(io.BytesIO(b'not a zip')) except NotImplementedError as e: print(e) # File-like object passed to ZipFile does not support fileno() ``` ``` -------------------------------- ### ZipFile.getinfo(path) Source: https://context7.com/tktech/fasterzip/llms.txt Retrieves metadata for a specific archive entry. Returns a dictionary containing file statistics similar to miniz's mz_zip_archive_file_stat struct. Raises KeyError if the entry does not exist. ```APIDOC ## ZipFile.getinfo(path) ### Description Retrieve metadata for an archive entry. ### Method `getinfo(path: bytes)` ### Parameters #### Path Parameters - **path** (bytes) - Required - The path to the archive entry. ### Response #### Success Response - **dict**: A dictionary populated from `mz_zip_archive_file_stat` struct. Key fields include `m_filename` (bytes), `m_uncomp_size` (int), `m_comp_size` (int), `m_crc32` (int), `m_time` (time_t), `m_is_directory` (bool), and `m_is_encrypted` (bool). ### Request Example ```python stat = zf.getinfo(b'readme.txt') print(stat['m_filename']) # b'readme.txt' print(stat['m_uncomp_size']) # 6 ``` ### Error Handling - **KeyError**: Raised if the entry does not exist. ``` -------------------------------- ### ZipFile.namelist() Source: https://context7.com/tktech/fasterzip/llms.txt A convenience method that yields only the filenames (as bytes) for each entry in the archive. ```APIDOC ## ZipFile.namelist() ### Description Iterate over the names of all archive entries. ### Method `namelist() -> Generator[bytes, None, None]` ### Response #### Success Response - **Generator[bytes, None, None]**: A generator yielding the filename (bytes) for each archive entry. ### Request Example ```python names = list(zf.namelist()) print(names) # [b'a.txt', b'b/c.txt'] ``` ``` -------------------------------- ### len(ZipFile) Source: https://context7.com/tktech/fasterzip/llms.txt Returns the total number of entries within the zip archive. ```APIDOC ## len(ZipFile) ### Description Get the number of entries in the archive. ### Method `__len__() -> int` ### Response #### Success Response - **int**: The total number of entries in the archive. ### Request Example ```python print(len(zf)) # 100 ``` ``` -------------------------------- ### Extract Single Entry to Memory with ZipFile.read Source: https://context7.com/tktech/fasterzip/llms.txt Extracts a ZIP archive entry entirely into memory as a memoryview. The buffer is freed upon exiting the 'with' block, so do not use the view afterwards. Raises zipfile.BadZipFile on decompression failure. ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile sample_data = b'\x00' + b'A' * (1024 * 1024) # 1 MB + leading null byte with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: zf.writestr('data.bin', sample_data) tf.seek(0) zf = ZipFile(tf) with zf.read(b'data.bin') as view: # `view` is a memoryview — zero-copy access assert view.tobytes() == sample_data print(f'Read {len(view)} bytes') # Read 1048577 bytes # Buffer is freed here — do NOT use `view` after this point ``` -------------------------------- ### ZipFile.read(path) — Extract a single entry into memory Source: https://context7.com/tktech/fasterzip/llms.txt Context-manager method that decompresses the archive entry at `path` (bytes) entirely into heap memory and yields a read-only `memoryview` over the raw bytes. The underlying buffer is freed immediately upon exiting the `with` block. Raises `zipfile.BadZipFile` on decompression failure. ```APIDOC ## ZipFile.read(path) — Extract a single entry into memory ### Description Context-manager method that decompresses the archive entry at `path` (bytes) entirely into heap memory and yields a read-only `memoryview` over the raw bytes. The underlying buffer is freed immediately upon exiting the `with` block. Raises `zipfile.BadZipFile` on decompression failure. ### Method GET (implicitly, as it reads data) ### Endpoint N/A (This is a method call on a Python object) ### Parameters #### Path Parameters - **path** (bytes) - Required - The path to the archive member to extract. ### Request Example ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile sample_data = b'\x00' + b'A' * (1024 * 1024) # 1 MB + leading null byte with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: zf.writestr('data.bin', sample_data) tf.seek(0) zf = ZipFile(tf) with zf.read(b'data.bin') as view: # `view` is a memoryview — zero-copy access assert view.tobytes() == sample_data print(f'Read {len(view)} bytes') # Read 1048577 bytes # Buffer is freed here — do NOT use `view` after this point ``` ### Response #### Success Response - **memoryview** - A read-only memoryview over the decompressed entry data. #### Response Example (The response is a memoryview object, not a JSON structure. The example shows how to access its content.) ```python # Example usage within the 'with' block: # assert view.tobytes() == sample_data # print(f'Read {len(view)} bytes') ``` ``` -------------------------------- ### ZipFile.read_iter(path, max_chunk_size=0x100000) — Stream-extract an entry in chunks Source: https://context7.com/tktech/fasterzip/llms.txt Generator method that decompresses the archive entry at `path` (bytes) incrementally, yielding `bytes` chunks of at most `max_chunk_size` bytes. Suitable for large files that should not be loaded entirely into memory at once. miniz guarantees each yield is exactly `max_chunk_size` bytes until the final (potentially smaller) chunk. ```APIDOC ## ZipFile.read_iter(path, max_chunk_size=0x100000) — Stream-extract an entry in chunks ### Description Generator method that decompresses the archive entry at `path` (bytes) incrementally, yielding `bytes` chunks of at most `max_chunk_size` bytes. Suitable for large files that should not be loaded entirely into memory at once. miniz guarantees each yield is exactly `max_chunk_size` bytes until the final (potentially smaller) chunk. ### Method GET (implicitly, as it streams data) ### Endpoint N/A (This is a method call on a Python object) ### Parameters #### Path Parameters - **path** (bytes) - Required - The path to the archive member to extract. - **max_chunk_size** (int) - Optional - The maximum size of each chunk to yield. Defaults to 1MB (0x100000). ### Request Example ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile TEN_MB = b'B' * (10 * 1024 * 1024) CHUNK = 1024 * 1024 # 1 MB chunks with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: zf.writestr('large.bin', TEN_MB) tf.seek(0) zf = ZipFile(tf) total_bytes = 0 chunk_count = 0 for chunk in zf.read_iter(b'large.bin', max_chunk_size=CHUNK): total_bytes += len(chunk) chunk_count += 1 # Process chunk — e.g., write to disk, hash, etc. print(f'Chunks: {chunk_count}') # Chunks: 10 print(f'Total: {total_bytes} B') # Total: 10485760 B ``` ### Response #### Success Response - **bytes** - Chunks of decompressed data from the archive entry. #### Response Example (The response is a generator yielding bytes chunks. The example shows how to iterate and process these chunks.) ```python # Example usage within the loop: # total_bytes += len(chunk) # chunk_count += 1 ``` ``` -------------------------------- ### Stream Extract Entry in Chunks with ZipFile.read_iter Source: https://context7.com/tktech/fasterzip/llms.txt Streams a ZIP archive entry incrementally, yielding bytes chunks. This is suitable for large files to avoid loading them entirely into memory. Each yielded chunk is max_chunk_size bytes, except for the final one. ```python import tempfile import zipfile as stdlib_zipfile from fasterzip import ZipFile TEN_MB = b'B' * (10 * 1024 * 1024) CHUNK = 1024 * 1024 # 1 MB chunks with tempfile.TemporaryFile() as tf: with stdlib_zipfile.ZipFile(tf, 'w') as zf: zf.writestr('large.bin', TEN_MB) tf.seek(0) zf = ZipFile(tf) total_bytes = 0 chunk_count = 0 for chunk in zf.read_iter(b'large.bin', max_chunk_size=CHUNK): total_bytes += len(chunk) chunk_count += 1 # Process chunk — e.g., write to disk, hash, etc. print(f'Chunks: {chunk_count}') # Chunks: 10 print(f'Total: {total_bytes} B') # Total: 10485760 B ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.