tar archive manipulation classes

The libtar units provides 2 classes to read and write .tar archives: class can be used to read a tar file, and the class can be used to write a tar file. The unit was implemented originally by Stefan Heymann.

File permissions TTarPermission denotes part of a files permission as it it stored in the .tar archive. Each of these enumerated constants correspond with one of the permission bits from a unix file permission. Owner can read the file Owner can write the file Owner can execute the file Group can read the file Group can write the file Group can execute the file Other people can read the file. Other people can write the file Other people can execute the file Complete set of permissions for file TTarPermissions describes the complete set of permissions that a file has. It is used in the Permissions field of the record. File type TFileType describes the file type of a file in the archive. It is used in the FileType field of the record. Normal file Hard link Symbolic link Character device file Block device file Directory FIFO file Contiguous file List of files Multi-volume file part Volume header, can appear only as first entry in the archive Tar mode TTarMode describes extra file modes. It is used in the Mode field of the record. File has SetUID bit set. File has SetGID bit set Bit $200 is set Full set of modes TTarModes denotes the full set of permission bits for the file in the field Mode field of the record. Tar Directory entry TTarDirRec describes an entry in the tar archive. It is similar to a directory entry as in TSearchRec, and is returned by the call. File name File size File time stamp File permissions File type Symbolic link target name Owner user ID Owner group ID Owner username Owner group name Checksum OK ? Extra file mode bits ? Major device number Minor device number Start position of file in archive Class to examine and read tar archives TTarArchive is the class used to read and examine .tar archives. It can be constructed from a stream or from a filename. Creating an instance will not perform any operation on the stream yet. FindNext Create a new instance of the archive Create can be used to create a new instance of TTarArchive using either a Stream TStream descendent or using a name of a file to open: FileName. In case of the filename, an open mode can be specified. In case a filename is specified and the file cannot be opened, an exception will occur. FindNext Stream to read archive from Filename to open and read archive from Filemode to open file with. Destroy TTarArchive instance Destroy closes the archive stream (if it created a stream) and cleans up the TTarArchive instance. Reset archive Reset sets the archive file position on the beginning of the archive. Find next archive entry FindNext positions the file pointer on the next archive entry, and returns all information about the entry in DirRec. It returns True if the operation was succeful, or False if not (for instance, when the end of the archive was reached). In case there are no more entries, False is returned. True if the next entry was found. Information about the archive Read a file from the archive

ReadFile can be used to read the current file in the archive. It can be called after the archive was succesfully positioned on an entry in the archive. The file can be read in various ways:

  • directly in a memory buffer. No checks are performed to see whether the buffer points to enough memory.
  • It can be copied to a Stream.
  • It can be copied to a file with name FileName.
  • The file content can be copied to a string
An exception may occur if the buffer is not large enough, or when the file specified in filename cannot be opened.
Memory buffer to copy file contents to. Stream to copy file contents to. Filename for file in which to write file content. File contents as string Return current archive position GetFilePos returns the position in the tar archive in Current and the complete archive size in Size. Current position in archive Archive size Set position in archive SetFilePos can be used to set the absolute position in the tar archive. New position in the archive Class to write tar archives TTarWriter can be used to create .tar archives. It can be created using a filename, in which case the archive will be written to the filename, or it can be created using a stream, in which case the archive will be written to the stream - for instance a compression stream. Create a new archive Create creates a new TTarWriter instance. This will start a new .tar archive. The archive will be written to the TargetStream stream or to a file with name TargetFileName, which will be opened with filemode Mode. In case TargetFileName cannot be opened, an exception will be raised. Stream to write archive to. Filename to write archive to. Mode to open target file with. File must be writable Close archive and clean up TTarWriter Destroy will close the archive (i.e. it writes the end-of-archive marker, if it was not yet written), and then frees the TTarWriter instance. Add a file to the archive

AddFile adds a file to the archive: the contents is read from FileName. Optionally, an alternative filename can be specified in TarFileName. This name should contain only forward slash path separators. If it is not specified, the name will be computed from FileName.

The archive entry is written with the current owner data and permissions.

If FileName cannot be opened, an exception will be raised.
File to add to the archive Alternative filename, stored in archive Add stream contents to archive.

AddStream will add the contents of Stream to the archive. The Stream will not be reset: only the contents of the stream from the current position will be written to the archive. The entry will be written with file name TarFileName. This name should contain only forward slash path separators. The entry will be written with timestamp FileDateGmt.

The archive entry is written with the current owner data and permissions.

Stream data to be added to archive Archive entry name Archive entry timestamp Add string as file data

AddString adds the string Contents as the data of an entry with file name TarFileName. This name should contain only forward slash path separators. The entry will be written with timestamp FileDateGmt.

The archive entry is written with the current owner data and permissions.

Data for file entry Name for filename Timestamp for filename. Add directory to archive

AddDir adds a directory entry to the archive. The entry is written with name DirName, maximum directory size MaxDirSize (0 means unlimited) and timestamp DateGmt.

Note that this call only adds an entry for a directory to the archive: if DirName is an existing directory, it does not write all files in the directory to the archive.

The directory entry is written with the current owner data and permissions.

Directory name to add Directory timestamp Max directory size Add a symbolic link to the archive

AddSymbolicLink adds a symbolic link entry to the archive, with name FileName, pointing to LinkName. The entry is written with timestamp DateGmt.

The link entry is written with the current owner data and permissions.

Filename for entry Target of symbolic link Timestamp of entry Add hard link to archive

AddLink adds a hard link entry to the archive. The entry has name FileName, timestamp DateGmt and points to LinkName.

The link entry is written with the current owner data and permissions.

Entry filename Target of hard link Timestamp of entry Add volume header entry

AddVolumeHeader adds a volume header entry to the archive. The entry is written with name VolumeID and timestamp DateGmt.

The volume header entry is written with the current owner data and permissions.

Volume name to write Timestamp of archive entry Finalize the archive

Finalize writes the end-of-archive marker to the archive. No more entries can be added after Finalize was called.

If the TTarWriter instance is destroyed, it will automatically call finalize if finalize was not yet called.

Archive entry permissions Permissions is used for the permissions field of the archive entries. Archive entry user ID UID is used for the UID field of the archive entries. Archive entry group ID GID is used for the GID field of the archive entries. Archive entry user name UserName is used for the UserName field of the archive entries. Archive entry group name GroupName is used for the GroupName field of the archive entries. Archive entry mode Mode is used for the Mode field of the archive entries. Archive entry Magic constant Magic is used for the Magic field of the archive entries. Names of filetypes FILETYPE_NAME can be used to get a textual description for each of the possible entry file types. Set with all permissions ALL_PERMISSIONS is a set constant containing all possible permissions (read/write/execute, for all groups of users) for an archive entry. Set with all read permissions READ_PERMISSIONS is a set constant containing all possible read permissions set for an archive entry. Set with all write permissions WRITE_PERMISSIONS is a set constant containing all possible write permissions set for an archive entry. Set with all execute permissions WRITE_PERMISSIONS is a set constant containing all possible execute permissions set for an archive entry. Convert a set of permissions to a string PermissionString can be used to convert a set of Permissions to a string in the same format as used by the unix 'ls' command. String representation of Permissions Set of permissions Convert filename to archive format ConvertFileName converts the file name FileName to a format allowed by the tar archive. Basically, it converts directory specifiers to forward slashes. Corrected filename in archive format Filename to correct Extract filetime FileTimeGMT returns the timestamp of a filename (FileName must exist) or a search rec (TSearchRec) to a GMT representation that can be used in a tar entry. File time in GMT notation Name of file to examine Searchrec from which timestamp is taken Initialize tar archive entry ClearDirRec clears the DirRec entry, it basically zeroes out all fields. Record to clear.