ArchiveFile
ArchiveFile
ArchiveFile(file: StrPath, mode: OpenArchiveMode | str = 'r', *, password: str | None = None, compression_type: CompressionType | None = None, compression_level: CompressionLevel | int | None = None, **kwargs: Any)
Bases: BaseArchiveAdapter
Open an archive file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
StrPath
|
Path to the archive file. |
required |
mode |
OpenArchiveMode
|
Mode for opening the archive file. |
'r'
|
password |
str
|
Password for encrypted archive files. |
None
|
compression_type |
CompressionType
|
The compression method for writing zip files. |
None
|
compression_level |
CompressionLevel
|
The compression level for writing zip files. |
None
|
kwargs |
Any
|
Keyword arugments to pass to the underlying library. |
{}
|
Returns:
Type | Description |
---|---|
None
|
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
Raised if the archive format is unsupported |
Notes
The compression_type
and compression_level
parameters are only applicable when creating
zip files and do not affect reading zip files or other archive formats.
Source code in src/archivefile/_core.py
compression_level
property
compression_level: CompressionLevel | None
Compression level used for writing.
compression_type
property
compression_type: CompressionType | None
Compression type used for writing.
close
extract
extract(member: StrPath | ArchiveMember, *, destination: StrPath = Path.cwd()) -> Path
Extract a member of the archive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
member |
StrPath | ArchiveMember
|
Name of the member or an ArchiveMember object. |
required |
destination |
StrPath
|
The path to the directory where the member will be extracted. If not specified, the current working directory is used as the default destination. |
cwd()
|
Returns:
Type | Description |
---|---|
Path
|
The path to the extracted file. |
Raises:
Type | Description |
---|---|
KeyError
|
Raised if the member is not found in the archive. |
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.zip") as archive:
file = archive.extract("hello-world/pyproject.toml")
print(file.read_text())
# [tool.poetry]
# name = "hello-world"
# version = "0.1.0"
# description = ""
# readme = "README.md"
# packages = [{include = "hello_world", from = "src"}]
Source code in src/archivefile/_core.py
extractall
extractall(*, destination: StrPath = Path.cwd(), members: CollectionOf[StrPath | ArchiveMember] | None = None) -> Path
Extract all the members of the archive to the destination directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
destination |
StrPath
|
The path to the directory where the members will be extracted. If not specified, the current working directory is used as the default destination. |
cwd()
|
members |
CollectionOf[StrPath | ArchiveMember]
|
Collection of member names or ArchiveMember objects to extract.
Default is |
None
|
Returns:
Type | Description |
---|---|
Path
|
The path to the destination directory. |
Raises:
Type | Description |
---|---|
KeyError
|
Raised if any member in members was not found in the archive. |
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.zip") as archive:
outdir = archive.extractall()
for file in outdir.rglob("*"):
print(file)
# /source/hello-world
# /source/hello-world/pyproject.toml
# /source/hello-world/README.md
# /source/hello-world/src
# /source/hello-world/tests
# /source/hello-world/src/hello_world
# /source/hello-world/src/hello_world/__init__.py
# /source/hello-world/tests/__init__.py
Source code in src/archivefile/_core.py
get_member
get_member(member: StrPath | ArchiveMember) -> ArchiveMember
Retrieve an ArchiveMember object by it's name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
member |
StrPath | ArchiveMember
|
Name of the member. |
required |
Returns:
Type | Description |
---|---|
ArchiveMember
|
Represents a member of the archive. |
Raises:
Type | Description |
---|---|
KeyError
|
Raised if the member is not found in the archive. |
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.tar") as archive:
archive.get_member("README.md")
# ArchiveMember(name='README.md', size=3799, compressed_size=3799, datetime=datetime.datetime(2024, 4, 10, 20, 10, 57, tzinfo=datetime.timezone.utc), checksum=5251, is_dir=False, is_file=True)
Source code in src/archivefile/_core.py
get_members
get_members() -> Generator[ArchiveMember]
Retrieve all members of the archive as a generator of ArchiveMember
objects.
Yields:
Type | Description |
---|---|
ArchiveMember
|
Each member of the archive as an |
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.tar") as archive:
for member in archive.get_members():
print(member.name)
# project/pyproject.toml
# project/src
Source code in src/archivefile/_core.py
get_names
Retrieve all members of the archive as a tuple of strings.
Returns:
Type | Description |
---|---|
tuple[str, ...]
|
Members of the archive as a tuple of strings. |
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.tar") as archive:
archive.get_names()
# (
# "project/pyproject.toml",
# "project/src",
# )
Source code in src/archivefile/_core.py
print_table
print_table(*, title: str | None = None, style: TableStyle = 'markdown', sort_by: SortBy = 'name', descending: bool = False, **kwargs: Any) -> None
Print the contents of the archive as a table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
Title of the table. Defaults to archive file name. |
None
|
style |
TableStyle
|
The style of the table. |
'markdown'
|
sort_by |
SortBy
|
Key used to sort the table. |
'name'
|
descending |
bool
|
If True, sorting will be in descending order. |
False
|
kwargs |
Any
|
Additional keyword arguments to be passed to the |
{}
|
Returns:
Type | Description |
---|---|
None
|
|
Notes
The rich
dependency is required to use this method.
You can install it via either of these commands:
pip install archivefile[rich]
pip install archivefile[all]
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.zip") as archive:
archive.print_table()
# source.zip
#
# | Name | Date modified | Type | Size | Compressed Size |
# |-----------------------------------------|---------------------------|--------|------|-----------------|
# | hello-world/ | 2024-05-02T09:41:24+00:00 | Folder | 0B | 0B |
# | hello-world/README.md | 2024-05-02T09:41:24+00:00 | File | 0B | 0B |
# | hello-world/pyproject.toml | 2024-05-02T09:41:24+00:00 | File | 363B | 241B |
# | hello-world/src/ | 2024-05-02T09:41:24+00:00 | Folder | 0B | 0B |
# | hello-world/src/hello_world/ | 2024-05-02T09:41:24+00:00 | Folder | 0B | 0B |
# | hello-world/src/hello_world/__init__.py | 2024-05-02T09:41:24+00:00 | File | 0B | 0B |
# | hello-world/tests/ | 2024-05-02T09:41:24+00:00 | Folder | 0B | 0B |
# | hello-world/tests/__init__.py | 2024-05-02T09:41:24+00:00 | File | 0B | 0B |
Source code in src/archivefile/_core.py
print_tree
Print the contents of the archive as a tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_depth |
int
|
Maximum depth to print. |
0
|
style |
TreeStyle
|
The style of the tree. |
'const'
|
Returns:
Type | Description |
---|---|
None
|
|
Notes
The bigtree
dependency is required to use this method.
You can install it via either of these commands:
pip install archivefile[bigtree]
pip install archivefile[all]
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.tar.gz") as archive:
archive.print_tree()
# source.tar.gz
# └── hello-world
# ├── pyproject.toml
# ├── README.md
# ├── src
# │ └── hello_world
# │ └── __init__.py
# └── tests
# └── __init__.py
Source code in src/archivefile/_core.py
read_bytes
read_bytes(member: StrPath | ArchiveMember) -> bytes
Read the member in bytes mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
member |
StrPath | ArchiveMember
|
Name of the member or an ArchiveMember object. |
required |
Returns:
Type | Description |
---|---|
bytes
|
The contents of the file as bytes. |
Raises:
Type | Description |
---|---|
KeyError
|
Raised if the member is not found in the archive. |
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.zip") as archive:
data = archive.read_bytes("hello-world/pyproject.toml")
print(data)
# b'[tool.poetry]\r\nname = "hello-world"\r\nversion = "0.1.0"\r\ndescription = ""\r\nreadme = "README.md"\r\npackages = [{include = "hello_world", from = "src"}]\r\n'
Source code in src/archivefile/_core.py
read_text
read_text(member: StrPath | ArchiveMember, *, encoding: str = 'utf-8', errors: ErrorHandler = 'strict') -> str
Read the member in text mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
member |
StrPath | ArchiveMember
|
Name of the member or an ArchiveMember object. |
required |
encoding |
str
|
Encoding used to read the file. Default is |
'utf-8'
|
errors |
ErrorHandler
|
String that specifies how encoding and decoding errors are to be handled. |
'strict'
|
Returns:
Type | Description |
---|---|
str
|
The contents of the file as a string. |
Raises:
Type | Description |
---|---|
KeyError
|
Raised if the member is not found in the archive. |
References
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.zip") as archive:
text = archive.read_text("hello-world/pyproject.toml")
print(text)
# [tool.poetry]
# name = "hello-world"
# version = "0.1.0"
# description = ""
# readme = "README.md"
# packages = [{include = "hello_world", from = "src"}]
Source code in src/archivefile/_core.py
write
Write a single file to the archive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
StrPath
|
Path of the file. |
required |
arcname |
StrPath
|
Name which the file will have in the archive. Default is the basename of the file. |
None
|
Returns:
Type | Description |
---|---|
None
|
|
Examples:
from archivefile import ArchiveFile
with ArchiveFile("example.zip", "w") as archive:
archive.write("/root/some/path/to/foo.txt")
archive.write("bar.txt", arcname="baz.txt")
archive.write("recipe/spam.txt", arcname="ingredients/spam.txt")
archive.write("recipe/eggs.txt", arcname="ingredients/eggs.txt")
archive.print_tree()
# example.zip
# ├── foo.txt
# ├── baz.txt
# └── ingredients
# ├── spam.txt
# └── eggs.txt
Source code in src/archivefile/_core.py
write_bytes
Write the bytes data
to a file within the archive named arcname
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes
|
The bytes data to write to the archive. |
required |
arcname |
StrPath
|
The name which the file will have in the archive. |
required |
Returns:
Type | Description |
---|---|
None
|
|
Examples:
from archivefile import ArchiveFile
with ArchiveFile("skynet.7z", "w") as archive:
archive.write_bytes(b"010010100101", arcname="terminator.py")
members = archive.get_names()
print(members)
# ('terminator.py',)
data = archive.read_bytes("terminator.py")
print(data)
# b"010010100101"
Source code in src/archivefile/_core.py
write_text
Write the string data
to a file within the archive named arcname
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
str
|
The text data to write to the archive. |
required |
arcname |
StrPath
|
The name which the file will have in the archive. |
required |
Returns:
Type | Description |
---|---|
None
|
|
Examples:
from archivefile import ArchiveFile
with ArchiveFile("newarchive.zip", "w") as archive:
archive.write_text("spam and eggs", arcname="recipe.txt")
members = archive.get_names()
print(members)
# ('recipe.txt',)
text = archive.read_text("recipe.txt")
print(text)
# 'spam and eggs'
Source code in src/archivefile/_core.py
writeall
writeall(dir: StrPath, *, root: StrPath | None = None, glob: str = '*', recursive: bool = True) -> None
Write a directory to the archive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir |
StrPath
|
Path of the directory. |
required |
root |
StrPath
|
Directory that will be the root directory of the archive, all paths in the archive will be relative to it. This must be relative to given directory path. Default is the parent of the given directory. |
None
|
glob |
str
|
Only write files that match this glob pattern to the archive. |
'*'
|
recursive |
bool
|
Recursively write all the files in the given directory. Default is True. |
True
|
Returns:
Type | Description |
---|---|
None
|
|
Examples:
from archivefile import ArchiveFile
with ArchiveFile("source.tar.gz", "w:gz") as archive:
archive.writeall(dir="hello-world/")
archive.print_tree()
# source.tar.gz
# └── hello-world
# ├── pyproject.toml
# ├── README.md
# ├── src
# │ └── hello_world
# │ └── __init__.py
# └── tests
# └── __init__.py