[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

(no description)

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

Defines 1 class

Zip:: (32 methods):
  setCompression()
  open()
  contents()
  yieldContents()
  extract()
  create()
  addFile()
  addData()
  close()
  getArchive()
  save()
  readRecord()
  readCentralDir()
  readCentralFileHeader()
  isFolder()
  headerMode()
  readFileHeader()
  parseExtra()
  header2fileinfo()
  utf8Name()
  cpToUtf8()
  makeEntryName()
  makeExternalAttributes()
  makeGeneralPurposeFlag()
  writebytes()
  writebytesAt()
  dataOffset()
  makeDosTime()
  makeUnixTime()
  makeCentralFileRecord()
  makeLocalFileHeader()
  makeCrcAndSize()


Class: Zip  - X-Ref

Class Zip

Creates or extracts Zip archives

for specs see http://www.pkware.com/appnote

File names are expected to be UTF-8 encoded. Names outside the ASCII range are stored as UTF-8
and flagged accordingly, names read from an archive are always returned as UTF-8: they are
taken as they are when the archive flags them or provides them in an extra field, and are
converted from CP437 otherwise, which is the encoding the format prescribes for unflagged names.

setCompression($level = 9, $type = Archive::COMPRESS_AUTO)   X-Ref
Set the compression level.

Compression Type is ignored for ZIP

You can call this function before adding each file to set differen compression levels
for each file.

param: int $level Compression level (0 to 9)
param: int $type  Type of compression to use ignored for ZIP

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

param: string $file

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

This function lists the files stored in the archive, and returns an indexed array of FileInfo objects

The archive is closed afer reading the contents, for API compatibility with TAR files
Reopen the file with open() again if you want to do additional operations

return: FileInfo[]

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

return: FileInfo[]
see: contents()

extract($outdir, $strip = '', $exclude = '', $include = '')   X-Ref
Extract an existing ZIP 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.

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 ZIP file

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

param: string $file

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

param: string $file path to the original file
param: string|FileInfo $fileinfo either the name to use 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 Zip 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

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's more memory effective to specify the filename in the create() function and
let the library work on the new file directly.

param: $file

readRecord($bytes, $what)   X-Ref
Read a fixed number of bytes from the archive

return: string
param: int $bytes the number of bytes to read
param: string $what the structure being read, used in the error message

readCentralDir()   X-Ref
Read the central directory

This key-value list contains general information about the ZIP file

return: array

readCentralFileHeader()   X-Ref
Read the next central file header

Assumes the current file pointer is pointing at the right position

return: array

isFolder($header)   X-Ref
Checks if the given header describes a directory

Directories are stored with a trailing slash in their name. Some archivers rely on the
MS-DOS directory attribute or on the file type of the Unix mode instead.

return: bool
param: array $header a central file header

headerMode($header)   X-Ref
Returns the file mode stored in the given header

Only archives created on Unix keep a mode in the upper half of the external file
attributes, other systems store data there that is of no use here.

return: int the mode including its file type bits, zero when the header holds none
param: array $header a central file header

readFileHeader($header)   X-Ref
Reads the local file header

This header precedes each individual file inside the zip file. Assumes the current file pointer is pointing at
the right position already. Enhances the given central header with the data found at the local header.

return: array
param: array $header the central file header read previously (see above)

parseExtra($header)   X-Ref
Parse the extra headers into fields

return: array
param: string $header

header2fileinfo($header)   X-Ref
Create fileinfo object from header data

return: FileInfo
param: $header

utf8Name($header, $field, $extrafield)   X-Ref
Returns the UTF-8 version of a name stored in the given header

An extra field holding the UTF-8 name is used when it is available. Otherwise the name is
taken as it is when the header flags it as UTF-8 and converted from CP437 when it does not.

return: string
param: array $header the header the name is stored in
param: string $field the header field holding the name
param: string $extrafield the extra data field holding the UTF-8 version of the name

cpToUtf8($string)   X-Ref
Convert the given CP437 encoded string to UTF-8

Tries iconv with the correct encoding first, falls back to mbstring with CP850 which is
similar enough. CP437 seems not to be available in mbstring. Lastly falls back to keeping the
string as is, which is still better than nothing.

On some systems iconv is available, but the codepage is not. We also check for that.

return: string
param: $string

makeEntryName(FileInfo $fileinfo)   X-Ref
Returns the name to store the given file under

Directories are stored with a trailing slash. Readers that look at neither the attributes
nor the mode of an entry rely on it to recognize a directory.

return: string
param: FileInfo $fileinfo

makeExternalAttributes(FileInfo $fileinfo)   X-Ref
Returns the external file attributes for the given file

The upper half holds the Unix mode, the lower one the MS-DOS attributes, of which only the
directory bit is used here. A mode that comes without file type bits is completed with them,
otherwise readers can not tell what kind of entry they are looking at.

return: int
param: FileInfo $fileinfo

makeGeneralPurposeFlag($name)   X-Ref
Returns the general purpose flag for an entry with the given file name

File names outside the 7bit ASCII range are stored as UTF-8, which has to be flagged for
readers to decode them correctly. A name that is no valid UTF-8 is stored as it is, there
is no way to tell readers what encoding it uses.

return: int
param: string $name file name

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

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

writebytesAt($data, $offset)   X-Ref
Write to the open filepointer or memory at the specified offset

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

dataOffset()   X-Ref
Current data pointer position

return: int

makeDosTime($time)   X-Ref
Create a DOS timestamp from a UNIX timestamp

DOS timestamps start at 1980-01-01, earlier UNIX stamps will be set to this date

return: int
param: $time

makeUnixTime($mdate = null, $mtime = null)   X-Ref
Create a UNIX timestamp from a DOS timestamp

return: int
param: $mdate
param: $mtime

makeCentralFileRecord($offset, $ts, $crc, $len, $clen, $name, $comp = null, $external = 0)   X-Ref
Returns a local file header for the given data

return: string
param: int $offset location of the local header
param: int $ts unix timestamp
param: int $crc CRC32 checksum of the uncompressed data
param: int $len length of the uncompressed data
param: int $clen length of the compressed data
param: string $name file name
param: boolean|null $comp if compression is used, if null it's determined from $len != $clen
param: int $external external file attributes as returned by makeExternalAttributes()

makeLocalFileHeader($ts, $crc, $len, $clen, $name, $comp = null)   X-Ref
Returns a local file header for the given data

return: string
param: int $ts unix timestamp
param: int $crc CRC32 checksum of the uncompressed data
param: int $len length of the uncompressed data
param: int $clen length of the compressed data
param: string $name file name
param: boolean|null $comp if compression is used, if null it's determined from $len != $clen

makeCrcAndSize($crc, $len, $clen)   X-Ref
Returns only a part of the local file header containing the CRC, size and compressed size.
Used to update these fields for an already written header.

return: string
param: int $crc CRC32 checksum of the uncompressed data
param: int $len length of the uncompressed data
param: int $clen length of the compressed data