Overview

Namespaces

  • splitbrain
    • PHPArchive

Classes

  • Archive
  • FileInfo
  • Tar
  • Zip

Exceptions

  • ArchiveCorruptedException
  • ArchiveIllegalCompressionException
  • ArchiveIOException
  • FileInfoException
  • Overview
  • Namespace
  • Class

Class Tar

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.

splitbrain\PHPArchive\Archive
Extended by splitbrain\PHPArchive\Tar
Namespace: splitbrain\PHPArchive
Package: splitbrain\PHPArchive
License: MIT
Author: Andreas Gohr andi@splitbrain.org
Located at Tar.php
Methods summary
public
# setCompression( integer $level = 9, integer $type = Archive::COMPRESS_AUTO )

Sets the compression to use

Sets the compression to use

Parameters

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

Throws

splitbrain\PHPArchive\ArchiveIllegalCompressionException
public
# open( string $file )

Open an existing TAR file for reading

Open an existing TAR file for reading

Parameters

$file

Throws

splitbrain\PHPArchive\ArchiveIOException
splitbrain\PHPArchive\ArchiveIllegalCompressionException
public splitbrain\PHPArchive\FileInfo[]
# contents( )

Read the contents of a TAR archive

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

splitbrain\PHPArchive\FileInfo[]

Throws

splitbrain\PHPArchive\ArchiveIOException
splitbrain\PHPArchive\ArchiveCorruptedException

Returns

FileInfo[]
public
# yieldContents( )

Read the contents of a TAR archive and return each entry using yield for memory efficiency.

Read the contents of a TAR archive and return each entry using yield for memory efficiency.

Throws

splitbrain\PHPArchive\ArchiveIOException
splitbrain\PHPArchive\ArchiveCorruptedException

See

splitbrain\PHPArchive\Tar::contents()

Returns

FileInfo[]
public string
# readCurrentEntry( integer $length = PHP_INT_MAX )

Reads content of a current archive entry.

Reads content of a current archive entry.

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

Parameters

$length
maximum number of bytes to read

Returns

string
public splitbrain\PHPArchive\FileInfo[]
# extract( string $outdir, integer|string $strip = '', string $exclude = '', string $include = '' )

Extract an existing TAR archive

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

Parameters

$outdir
the target directory for extracting
$strip
either the number of path components or a fixed prefix to strip
$exclude
a regular expression of files to exclude
$include
a regular expression of files to include

Returns

splitbrain\PHPArchive\FileInfo[]

Throws

splitbrain\PHPArchive\ArchiveIOException
splitbrain\PHPArchive\ArchiveCorruptedException
public
# create( string $file = '' )

Create a new TAR file

Create a new TAR file

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

Parameters

$file

Throws

splitbrain\PHPArchive\ArchiveIOException
splitbrain\PHPArchive\ArchiveIllegalCompressionException
public
# addFile( string $file, string|splitbrain\PHPArchive\FileInfo $fileinfo = '' )

Add a file to the current TAR archive using an existing file in the filesystem

Add a file to the current TAR archive using an existing file in the filesystem

Parameters

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

Throws

splitbrain\PHPArchive\ArchiveCorruptedException
when the file changes while reading it, the archive will be corrupt and should be deleted
splitbrain\PHPArchive\ArchiveIOException
there was trouble reading the given file, it was not added
splitbrain\PHPArchive\FileInfoException
trouble reading file info, it was not added
public
# addData( string|splitbrain\PHPArchive\FileInfo $fileinfo, string $data )

Add a file to the current TAR archive using the given $data as content

Add a file to the current TAR archive using the given $data as content

Parameters

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

Throws

splitbrain\PHPArchive\ArchiveIOException
public
# close( )

Add the closing footer to the archive if in write mode, close all file handles

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"

Throws

splitbrain\PHPArchive\ArchiveIOException

Link

http://www.gnu.org/software/tar/manual/html_chapter/tar_8.html#SEC134
public
# getArchive( )

Returns the created in-memory archive data

Returns the created in-memory archive data

This implicitly calls close() on the Archive

Throws

splitbrain\PHPArchive\ArchiveIOException
public
# save( string $file )

Save the created in-memory archive data

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.

Parameters

$file

Throws

splitbrain\PHPArchive\ArchiveIOException
splitbrain\PHPArchive\ArchiveIllegalCompressionException
protected string
# readbytes( integer $length )

Read from the open file pointer

Read from the open file pointer

Parameters

$length
bytes to read

Returns

string
protected integer
# writebytes( string $data )

Write to the open filepointer or memory

Write to the open filepointer or memory

Parameters

$data

Returns

integer
number of bytes written

Throws

splitbrain\PHPArchive\ArchiveIOException
protected
# skipbytes( integer $bytes )

Skip forward in the open file pointer

Skip forward in the open file pointer

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

Parameters

$bytes
seek to this position
protected
# writeFileHeader( splitbrain\PHPArchive\FileInfo $fileinfo )

Write the given file meta data as header

Write the given file meta data as header

Parameters

$fileinfo

Throws

splitbrain\PHPArchive\ArchiveIOException
protected
# writeRawFileHeader( string $name, integer $uid, integer $gid, integer $perm, integer $size, integer $mtime, string $typeflag = '' )

Write a file header to the stream

Write a file header to the stream

Parameters

$name
$uid
$gid
$perm
$size
$mtime
$typeflag
Set to '5' for directories

Throws

splitbrain\PHPArchive\ArchiveIOException
protected array|false
# parseHeader( string $block )

Decode the given tar file header

Decode the given tar file header

Parameters

$block
a 512 byte block containing the header data

Returns

array|false
returns false when this was a null block

Throws

splitbrain\PHPArchive\ArchiveCorruptedException
protected splitbrain\PHPArchive\FileInfo
# header2fileinfo( $header )

Creates a FileInfo object from the given parsed header

Creates a FileInfo object from the given parsed header

Parameters

$header

Returns

splitbrain\PHPArchive\FileInfo
protected
# compressioncheck( $comptype )

Checks if the given compression type is available and throws an exception if not

Checks if the given compression type is available and throws an exception if not

Parameters

$comptype

Throws

splitbrain\PHPArchive\ArchiveIllegalCompressionException
public integer
# filetype( string $file )

Guesses the wanted compression from the given file

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

Parameters

$file

Returns

integer
public static
# numberDecode( string $field )

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

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

Parameters

$field
$return int
public static string
# numberEncode( integer $value, integer $length )

Encodes numeric values according to the https://www.gnu.org/software/tar/manual/html_node/Extensions.html#Extensions (basically with support for big numbers)

Encodes numeric values according to the https://www.gnu.org/software/tar/manual/html_node/Extensions.html#Extensions (basically with support for big numbers)

Parameters

$value
$length
field length

Returns

string
Methods inherited from splitbrain\PHPArchive\Archive
setCallback()
Constants summary
integer READ_CHUNK_SIZE
# 1048576
Constants inherited from splitbrain\PHPArchive\Archive
COMPRESS_AUTO, COMPRESS_BZIP, COMPRESS_GZIP, COMPRESS_NONE
Properties summary
protected string $file
# ''
protected integer $comptype
# Archive::COMPRESS_AUTO
protected integer $complevel
# 9
protected $fh
#
protected string $memory
# ''
protected boolean $closed
# true
protected boolean $writeaccess
# false
protected integer $position
# 0
protected integer $contentUntil
# 0
protected integer $skipUntil
# 0
Properties inherited from splitbrain\PHPArchive\Archive
$callback
API documentation generated by ApiGen