AR File Handling for Python
This package allows the reading and writing of AR (archiver) archive files, which are commonly used in Unix-like systems for static libraries and Debian packages. It is inspired by Python's standard library `tarfile` and `zipfile` modules. The library is currently in 'Beta' development status and primarily supports the BSD variant of the common AR archive format.
Common errors
-
ModuleNotFoundError: No module named 'unix_ar'
cause The package is installed via `pip install unix-ar`, but the top-level module to import is `unix_ar`, not `ar`.fixEnsure you are importing `unix_ar`. Correct: `import unix_ar` or `from unix_ar import Archive`. -
unix_ar.ARFormatError: Unknown AR format
cause The AR archive file you are trying to read is not in the supported BSD variant format. This library has limited support for different AR formats.fixConfirm the format of your AR file. If it's not BSD-compatible, this library may not be able to process it. You might need to use a different tool or library, or convert the archive to a supported format. -
FileNotFoundError: [Errno 2] No such file or directory: 'some_file.ar'
cause The specified archive file path for `unix_ar.open()` in read mode ('r') does not exist, or the path for a file being added in write mode ('w') is incorrect.fixDouble-check the file paths. Ensure the archive exists for reading, or that the source files exist when creating/adding to an archive.
Warnings
- deprecated The library has not been updated since June 2019 and is marked as 'Development Status :: 4 - Beta' on PyPI. There may be a lack of active maintenance and support for newer Python versions or bug fixes.
- gotcha The library primarily supports the BSD variant of the AR archive format. Other variants (e.g., AIX, GNU ar, System V) may not be compatible, leading to errors or corrupted archives when reading/writing.
- gotcha When interacting with AR files created by the `ar` command-line tool, especially with the GNU `ar`'s default 'deterministic mode' (`-D`), metadata like UIDs, GIDs, and modification times might be set to zero. This library may preserve or re-create full metadata, which could lead to inconsistencies if you expect 'deterministic' output.
Install
-
pip install unix-ar
Imports
- Archive
import ar
from unix_ar import Archive
- open
import unix_ar; unix_ar.open(...)
Quickstart
import unix_ar
import os
# Create some dummy files
with open("file1.txt", "w") as f:
f.write("This is file 1.")
with open("file2.txt", "w") as f:
f.write("This is file 2.")
# Create an AR archive
with unix_ar.open("my_archive.ar", "w") as ar_file:
ar_file.add("file1.txt")
ar_file.add("file2.txt", arcname="renamed_file2.txt")
print("Archive 'my_archive.ar' created with file1.txt and renamed_file2.txt.")
# Read from the archive
with unix_ar.open("my_archive.ar", "r") as ar_file:
print("\nMembers in the archive:")
for member in ar_file.getmembers():
print(f" - Member name: {member.name}, size: {member.size} bytes")
if member.name == "renamed_file2.txt":
extracted_data = ar_file.extractfile(member).read().decode('utf-8')
print(f" Content of {member.name}: '{extracted_data}'")
# Clean up dummy files and archive
os.remove("file1.txt")
os.remove("file2.txt")
os.remove("my_archive.ar")
print("\nCleaned up dummy files and archive.")