[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/splitbrain/php-archive/src/ -> Tar.php (summary)

(no description)

File Size: 985 lines (35 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

Tar:: (27 methods):
  setCompression()
  open()
  contents()
  yieldContents()
  readCurrentEntry()
  extract()
  create()
  addFile()
  addData()
  close()
  getArchive()
  save()
  readbytes()
  writebytes()
  skipbytes()
  writeFileHeader()
  writeRawFileHeader()
  parseHeader()
  readMetadata()
  parsePaxRecords()
  applyPaxRecords()
  decodeHeader()
  header2fileinfo()
  compressioncheck()
  filetype()
  numberDecode()
  numberEncode()


Class: Tar  - X-Ref

Class Tar

Creates or extracts Tar archives. Supports gz and bzip compression

Long pathnames (>100 chars) are supported in POSIX ustar and GNU longlink formats. Pax
extended headers are understood when reading.

File names are expected to be UTF-8 encoded. The format has no field to declare the encoding
of a name, so names are stored exactly as they are given and returned exactly as they are
stored. For archives written by current tools that means UTF-8, older archives may use any
encoding and can not be recognized as such.

setCompression($level = 9, $type = Archive::COMPRESS_AUTO)   X-Ref
Sets the compression to use

param: int $level Compression level (0 to 9)
param: int $type Type of compression to use (use COMPRESS_* constants)

open($file)   X-Ref
Open an existing TAR file for reading

param: string $file

contents()   X-Ref
Read the contents of a TAR archive

This function lists the files stored in the archive

The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams.
Reopen the file with open() again if you want to do additional operations

returns: FileInfo[]

yieldContents()   X-Ref
Read the contents of a TAR archive and return each entry using yield
for memory efficiency.

see: contents()
returns: FileInfo[]

readCurrentEntry($length = PHP_INT_MAX)   X-Ref
Reads content of a current archive entry.

Works only when iterating trough the archive using the generator returned
by the yieldContents().

return: string
param: int $length maximum number of bytes to read

extract($outdir, $strip = '', $exclude = '', $include = '')   X-Ref
Extract an existing TAR archive

The $strip parameter allows you to strip a certain number of path components from the filenames
found in the tar file, similar to the --strip-components feature of GNU tar. This is triggered when
an integer is passed as $strip.
Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix,
the prefix will be stripped. It is recommended to give prefixes with a trailing slash.

By default this will extract all files found in the archive. You can restrict the output using the $include
and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If
$include is set only files that match this expression will be extracted. Files that match the $exclude
expression will never be extracted. Both parameters can be used in combination. Expressions are matched against
stripped filenames as described above.

The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams.
Reopen the file with open() again if you want to do additional operations

return: FileInfo[]
param: string $outdir the target directory for extracting
param: int|string $strip either the number of path components or a fixed prefix to strip
param: string $exclude a regular expression of files to exclude
param: string $include a regular expression of files to include

create($file = '')   X-Ref
Create a new TAR file

If $file is empty, the tar file will be created in memory

param: string $file

addFile($file, $fileinfo = '')   X-Ref
Add a file to the current TAR archive using an existing file in the filesystem

param: string $file path to the original file
param: string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original

addData($fileinfo, $data)   X-Ref
Add a file to the current TAR archive using the given $data as content

param: string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data
param: string          $data     binary content of the file to add

close()   X-Ref
Add the closing footer to the archive if in write mode, close all file handles

After a call to this function no more data can be added to the archive, for
read access no reading is allowed anymore

"Physically, an archive consists of a series of file entries terminated by an end-of-archive entry, which
consists of two 512 blocks of zero bytes"

link: http://www.gnu.org/software/tar/manual/html_chapter/tar_8.html#SEC134

getArchive()   X-Ref
Returns the created in-memory archive data

This implicitly calls close() on the Archive

save($file)   X-Ref
Save the created in-memory archive data

Note: It more memory effective to specify the filename in the create() function and
let the library work on the new file directly.

param: string $file

readbytes($length)   X-Ref
Read from the open file pointer

return: string
param: int $length bytes to read

writebytes($data)   X-Ref
Write to the open filepointer or memory

return: int number of bytes written
param: string $data

skipbytes($bytes)   X-Ref
Skip forward in the open file pointer

This is basically a wrapper around seek() (and a workaround for bzip2)

param: int $bytes seek to this position

writeFileHeader(FileInfo $fileinfo)   X-Ref
Write the given file meta data as header

param: FileInfo $fileinfo

writeRawFileHeader($name, $uid, $gid, $perm, $size, $mtime, $typeflag = '')   X-Ref
Write a file header to the stream

param: string $name
param: int $uid
param: int $gid
param: int $perm
param: int $size
param: int $mtime
param: string $typeflag Set to '5' for directories

parseHeader($block)   X-Ref
Parse the header of an archive entry

Metadata headers preceding an entry are resolved and applied to the entry they describe.
Entries that can not be represented as a FileInfo are consumed and reported as no entry:
their type flag either marks pure metadata (like a pax global header) or an entry type
that is not supported (like links or sparse files).

return: array|false returns false when this block held no usable entry
param: string $block a 512 byte block containing the header data

readMetadata($size)   X-Ref
Read the data blocks of a metadata header

Metadata beyond any sensible size is skipped instead of being loaded into memory, it can
only come from a broken or hostile archive. The entry then keeps what its own header says.

return: string the data without the padding of the last block, empty when it was skipped
param: int $size the size as given in the metadata header

parsePaxRecords($data)   X-Ref
Split the data of a pax header into its records

Each record is stored as "<length> <keyword>=<value>\n" with length being the length of
the whole record. Parsing stops at the first malformed record.

return: string[] record values indexed by their keyword
param: string $data the data of a pax header

applyPaxRecords($header, $records)   X-Ref
Apply the given pax records to a decoded header

A record with an empty value deletes what the entry's own header provides, that is how a
global record is suppressed for a single entry. Numbers can not be deleted, values that are
no numbers are ignored. Records without a counterpart in the header, like access times or
link targets, are ignored as well.

return: array the header with the records applied
param: array $header the decoded header of the entry the records belong to
param: string[] $records record values indexed by their keyword

decodeHeader($block)   X-Ref
Decode the given tar file header

return: array|false returns false when this was a null block
param: string $block a 512 byte block containing the header data

header2fileinfo($header)   X-Ref
Creates a FileInfo object from the given parsed header

return: FileInfo
param: $header

compressioncheck($comptype)   X-Ref
Checks if the given compression type is available and throws an exception if not

param: $comptype

filetype($file)   X-Ref
Guesses the wanted compression from the given file

Uses magic bytes for existing files, the file extension otherwise

You don't need to call this yourself. It's used when you pass Archive::COMPRESS_AUTO somewhere

return: int
param: string $file

numberDecode($field)   X-Ref
Decodes numeric values according to the
https://www.gnu.org/software/tar/manual/html_node/Extensions.html#Extensions
(basically with support for big numbers)

param: string $field

numberEncode($value, $length)   X-Ref
Encodes numeric values according to the
https://www.gnu.org/software/tar/manual/html_node/Extensions.html#Extensions
(basically with support for big numbers)

return: string
param: int $value
param: int $length field length