AR File Handling for Python

0.2.1 · maintenance · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a new AR archive, add files to it, and then read its contents and extract specific members using `unix_ar.open()` and `Archive` methods.

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.")

view raw JSON →