API Reference
aiogzip exposes its supported public API from the top-level package:
AsyncGzipBinaryFileAsyncGzipTextFileAsyncGzipFile
Implementation internals live in aiogzip._common, aiogzip._binary, and aiogzip._text. Treat those modules as private and unstable unless symbols are explicitly re-exported by aiogzip.
aiogzip
Async gzip file reader/writer public API.
AsyncGzipBinaryFile
AsyncGzipBinaryFile(filename: Union[str, bytes, Path, None], mode: str = 'rb', chunk_size: int = DEFAULT_CHUNK_SIZE, compresslevel: int = 6, mtime: Optional[Union[int, float]] = None, original_filename: Optional[Union[str, bytes]] = None, fileobj: Optional[WithAsyncReadWrite] = None, closefd: Optional[bool] = None)
An asynchronous gzip file reader/writer for binary data.
This class provides async gzip compression/decompression for binary data, making it a drop-in replacement for gzip.open() in binary mode.
Features: - Full compatibility with gzip.open() file format - Binary mode only (no text encoding/decoding) - Async context manager support - Configurable chunk size for performance tuning
Basic Usage
Write binary data
async with AsyncGzipBinaryFile("data.gz", "wb") as f: await f.write(b"Hello, World!")
Read binary data
async with AsyncGzipBinaryFile("data.gz", "rb") as f: data = await f.read() # Returns bytes
Interoperability with gzip.open(): # Files created by AsyncGzipBinaryFile can be read by gzip.open() async with AsyncGzipBinaryFile("data.gz", "wb") as f: await f.write(b"data")
with gzip.open("data.gz", "rb") as f:
data = f.read() # Works perfectly!
Source code in src/aiogzip/_binary.py
name
property
Return the name of the file.
This property provides compatibility with the standard file API.
Returns the filename passed to the constructor, or falls back to the
underlying file object's name attribute when available.
Returns:
| Type | Description |
|---|---|
Union[str, bytes, Path, None]
|
The filename as str, bytes, or Path, or None if no name is available. |
__aenter__
async
Enter the async context manager and initialize resources.
Source code in src/aiogzip/_binary.py
__aexit__
async
__aexit__(exc_type: Optional[type], exc_val: Optional[BaseException], exc_tb: Optional[Any]) -> None
Exit the context manager, flushing and closing the file.
__aiter__
__anext__
async
Return the next line from the binary stream.
close
async
Flushes any remaining compressed data and closes the file.
Source code in src/aiogzip/_binary.py
detach
fileno
Return the underlying file descriptor number.
Source code in src/aiogzip/_binary.py
flush
async
Flush any buffered compressed data to the file.
In write/append mode, this forces any buffered compressed data to be written to the underlying file. Note that this does NOT write the gzip trailer - use close() for that.
In read mode, this is a no-op for compatibility with the file API.
Examples:
async with AsyncGzipBinaryFile("file.gz", "wb") as f: await f.write(b"Hello") await f.flush() # Ensure data is written await f.write(b" World")
Source code in src/aiogzip/_binary.py
isatty
Return True if the underlying stream is interactive.
Source code in src/aiogzip/_binary.py
peek
async
Return up to size bytes without advancing the read position.
Source code in src/aiogzip/_binary.py
raw
read
async
Reads and decompresses binary data from the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Number of bytes to read (-1 for all remaining data) |
-1
|
Returns:
| Type | Description |
|---|---|
bytes
|
bytes |
Examples:
async with AsyncGzipBinaryFile("file.gz", "rb") as f: data = await f.read() # Returns bytes partial = await f.read(100) # Returns first 100 bytes
Source code in src/aiogzip/_binary.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | |
read1
async
Read up to size bytes from the buffer without looping.
Source code in src/aiogzip/_binary.py
readinto
async
Read bytes directly into a pre-allocated, writable buffer.
Source code in src/aiogzip/_binary.py
readinto1
async
Read directly into the buffer without looping.
Source code in src/aiogzip/_binary.py
readline
async
Read and return one line from the binary stream.
Source code in src/aiogzip/_binary.py
readlines
async
Read and return a list of lines from the binary stream.
Source code in src/aiogzip/_binary.py
seek
async
Move to a new file position, mirroring gzip.GzipFile semantics.
Source code in src/aiogzip/_binary.py
tell
async
truncate
write
async
Compresses and writes binary data to the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Union[bytes, bytearray, memoryview]
|
Bytes to write |
required |
Examples:
async with AsyncGzipBinaryFile("file.gz", "wb") as f: await f.write(b"Hello, World!") # Bytes input
Source code in src/aiogzip/_binary.py
writelines
async
Write a sequence of bytes-like lines to the binary stream.
Source code in src/aiogzip/_binary.py
AsyncGzipTextFile
AsyncGzipTextFile(filename: Union[str, bytes, Path, None], mode: str = 'rt', chunk_size: int = AsyncGzipBinaryFile.DEFAULT_CHUNK_SIZE, encoding: Optional[str] = 'utf-8', errors: Optional[str] = 'strict', newline: Union[str, None] = None, compresslevel: int = 6, mtime: Optional[Union[int, float]] = None, original_filename: Optional[Union[str, bytes]] = None, fileobj: Optional[WithAsyncReadWrite] = None, closefd: Optional[bool] = None)
An asynchronous gzip file reader/writer for text data.
This class wraps AsyncGzipBinaryFile and provides text mode operations with proper UTF-8 handling for multi-byte characters.
Features: - Full compatibility with gzip.open() file format - Text mode with automatic encoding/decoding - Proper handling of multi-byte UTF-8 characters - Line-by-line iteration support - Async context manager support
Basic Usage
Write text data
async with AsyncGzipTextFile("data.gz", "wt") as f: await f.write("Hello, World!") # String input
Read text data
async with AsyncGzipTextFile("data.gz", "rt") as f: text = await f.read() # Returns string
Line-by-line iteration
async with AsyncGzipTextFile("data.gz", "rt") as f: async for line in f: print(line.strip())
Source code in src/aiogzip/_text.py
name
property
Return the name of the file.
This property provides compatibility with the standard file API.
Returns the filename passed to the constructor, or falls back to the
underlying file object's name attribute when available.
Returns:
| Type | Description |
|---|---|
Union[str, bytes, Path, None]
|
The filename as str, bytes, or Path, or None if no name is available. |
newlines
property
Return newline types observed while reading, like TextIOWrapper.
__aenter__
async
Enter the async context manager and initialize resources.
Source code in src/aiogzip/_text.py
__aexit__
async
__aexit__(exc_type: Optional[type], exc_val: Optional[BaseException], exc_tb: Optional[Any]) -> None
Exit the context manager, flushing and closing the file.
__aiter__
__anext__
async
Return the next line from the file.
Source code in src/aiogzip/_text.py
close
async
Closes the file.
Source code in src/aiogzip/_text.py
flush
async
Flush any buffered data to the file.
In write/append mode, this forces any buffered text to be encoded and written to the underlying binary file.
In read mode, this is a no-op for compatibility with the file API.
Examples:
async with AsyncGzipTextFile("file.gz", "wt") as f: await f.write("Hello") await f.flush() # Ensure data is written await f.write(" World")
Source code in src/aiogzip/_text.py
read
async
Reads and decodes text data from the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Number of characters to read (-1 for all remaining data) |
-1
|
Returns:
| Type | Description |
|---|---|
str
|
str |
Examples:
async with AsyncGzipTextFile("file.gz", "rt") as f: text = await f.read() # Returns string partial = await f.read(100) # Returns first 100 chars as string
Source code in src/aiogzip/_text.py
readline
async
Read and return one line from the file.
A line is defined as text ending with a newline character ('\n'). If the file ends without a newline, the last line is returned without one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int
|
Maximum number of characters to return. Stops at newline, EOF, or once the limit is reached (matching TextIOBase semantics). |
-1
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The next line from the file, including the newline if present. Returns empty string at EOF. |
Examples:
async with AsyncGzipTextFile("file.gz", "rt") as f: line = await f.readline() # Read one line while line: print(line.rstrip()) line = await f.readline()
Source code in src/aiogzip/_text.py
readlines
async
Read and return a list of lines from the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hint
|
int
|
Optional size hint. If given and greater than 0, lines totaling approximately hint bytes are read (counted before decoding). The actual number of bytes read may be more or less than hint. If hint is -1 or not given, all remaining lines are read. |
-1
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of lines from the file, each including any trailing |
List[str]
|
newline character. |
Examples:
async with AsyncGzipTextFile("file.gz", "rt") as f: lines = await f.readlines() # Read all lines for line in lines: print(line.rstrip())
With size hint
async with AsyncGzipTextFile("file.gz", "rt") as f: lines = await f.readlines(1024) # Read ~1KB of lines
Source code in src/aiogzip/_text.py
write
async
Encodes and writes text data to the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str
|
String to write |
required |
Examples:
async with AsyncGzipTextFile("file.gz", "wt") as f: await f.write("Hello, World!") # String input
Source code in src/aiogzip/_text.py
writelines
async
Write a list of lines to the file.
Note that newlines are not added automatically; each string in the iterable should include its own line terminator if desired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
Iterable[str]
|
An iterable of strings to write. |
required |
Examples:
async with AsyncGzipTextFile("file.gz", "wt") as f: await f.writelines(["line1\n", "line2\n", "line3\n"])
From a generator
async with AsyncGzipTextFile("file.gz", "wt") as f: await f.writelines(f"{i}\n" for i in range(100))
Source code in src/aiogzip/_text.py
WithAsyncRead
Bases: Protocol
Protocol for async file-like objects that can be read.
WithAsyncReadWrite
Bases: Protocol
Protocol for async file-like objects that can be read and written.
WithAsyncWrite
Bases: Protocol
Protocol for async file-like objects that can be written.
AsyncGzipFile
AsyncGzipFile(filename: Union[str, bytes, Path, None], mode: str = 'rb', **kwargs: Any) -> Union[AsyncGzipBinaryFile, AsyncGzipTextFile]
Factory function that returns the appropriate AsyncGzip class based on mode.
This provides backward compatibility with the original AsyncGzipFile interface while using the new separated binary and text file classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Union[str, bytes, Path, None]
|
Path to the file |
required |
mode
|
str
|
File mode ('rb', 'wb', 'rt', 'wt', etc.) |
'rb'
|
**kwargs
|
Any
|
Additional arguments passed to the appropriate class |
{}
|
Returns:
| Type | Description |
|---|---|
Union[AsyncGzipBinaryFile, AsyncGzipTextFile]
|
AsyncGzipBinaryFile for binary modes ('rb', 'wb', 'ab') |
Union[AsyncGzipBinaryFile, AsyncGzipTextFile]
|
AsyncGzipTextFile for text modes ('rt', 'wt', 'at') |