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.
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[Union[WithAsyncRead, WithAsyncWrite, WithAsyncReadWrite]] = None, closefd: Optional[bool] = None, max_decompressed_size: Optional[int] = None, max_rewind_cache_size: Optional[int] = _MAX_CHUNK_SIZE, strict_size: bool = False)
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
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 | |
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
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
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[Union[WithAsyncRead, WithAsyncWrite, WithAsyncReadWrite]] = None, closefd: Optional[bool] = None, max_decompressed_size: Optional[int] = None, max_rewind_cache_size: Optional[int] = _MAX_CHUNK_SIZE, strict_size: bool = False)
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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
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') |