unix_ar: AR file handling for Python

This module allows reading and writing of UNIX AR archive files.

unix_ar.open(file, mode)

Open an archive file.

Parameters:
  • file (bytes | unicode) – File name to open.
  • mode – Either ‘’r’ or ‘w’
Return type:

unix_ar.ArFile

class unix_ar.ArInfo(name, size=None, mtime=None, perms=None, uid=None, gid=None)

Information on a file in an archive.

This has the filename and all the metadata for a file in an archive.

It is returned by infolist() and getinfo(), and can be passed when adding or extracting a file to or from the archive.

Missing fields will be autocompleted when passed to ArFile, but note that things like size will be respected, allowing you to store or extract only part of a file.

ArInfo objects returned by ArFile have the offset to the file in the archive, allowing to extract the correct one even if multiple files with the same name are present; if you change the name attribute, the initial file will be extracted with the new name (and new metadata).

classmethod frombuffer(buffer)

Decode the archive header.

tobuffer()

Encode as an archive header.

updatefromdisk(path=None)

Fill in the missing attributes from an actual file.

This is called by ArFile when adding a file to the archive. If some attributes are missing, they have to be provided from the disk.

If the file doesn’t exist, adding will fail. There is currently no default values for the attributes, it is thus your responsibility to provide them.

class unix_ar.ArFile(file, mode)

An UNIX ar archive.

This object allows you to either read or write an AR archive.

add(name, arcname=None)

Add a file to the archive.

Parameters:
  • name (bytes | unicode) – Path to the file to be added.
  • arcname (None | bytes | unicode | unix_ar.ArInfo) – Name the file will be stored as in the archive, or a full ArInfo. If unset, name will be used.
addfile(name, fileobj=None)

Add a file to the archive from a file object.

Parameters:
  • name (bytes | unicode | unix_ar.ArInfo) – Name the file will be stored as in the archive, or a full ArInfo.
  • fileobj – File object to read from.
close()

Close this archive and the underlying file.

No method should be called on the object after this.

extract(member, path='')

Extract a single file from the archive.

Parameters:
  • member (bytes | unicode | unix_ar.ArInfo) – Either a file name or an unix_ar.ArInfo object to extract.
  • path (bytes | unicode) – Destination path (current directory by default). You can also change the name attribute on the ArInfo you pass this method to extract to any file name.
extractall(path='')

Extract all the files in the archive.

Parameters:path (bytes | unicode) – Destination path (current directory by default).
getinfo(member)

Return an ArInfo for a specific file.

This object is a copy, so feel free to change it before feeding them to add() or addfile().

Parameters:member (bytes | unicode | unix_ar.ArInfo) – Either a file name or an incomplete unix_ar.ArInfo object to search for.
Return type:unix_ar.ArInfo
infolist()

Return a list of ArInfo for files in the archive.

These objects are copy, so feel free to change them before feeding them to add() or addfile().

Return type:[unix_ar.ArInfo]