Overview

Namespaces

  • splitbrain
    • PHPArchive

Classes

  • Archive
  • FileInfo
  • Tar
  • Zip

Exceptions

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

Class Zip

Class Zip

Creates or extracts Zip archives

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

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

Set the compression level.

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.

Parameters

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

Throws

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

Open an existing ZIP file for reading

Open an existing ZIP file for reading

Parameters

$file

Throws

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

Read the contents of a ZIP archive

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

Returns

splitbrain\PHPArchive\FileInfo[]

Throws

splitbrain\PHPArchive\ArchiveIOException
public splitbrain\PHPArchive\FileInfo[]
# yieldContents( )

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

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

Returns

splitbrain\PHPArchive\FileInfo[]

Throws

splitbrain\PHPArchive\ArchiveIOException

See

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

Extract an existing ZIP archive

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.

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
public
# create( string $file = '' )

Create a new ZIP file

Create a new ZIP file

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

Parameters

$file

Throws

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

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

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

Parameters

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

Throws

splitbrain\PHPArchive\ArchiveIOException
splitbrain\PHPArchive\FileInfoException
public
# addData( string|splitbrain\PHPArchive\FileInfo $fileinfo, string $data )

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

Add a file to the current Zip 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

Throws

splitbrain\PHPArchive\ArchiveIOException
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( $file )

Save the created in-memory archive data

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.

Parameters

$file

Throws

splitbrain\PHPArchive\ArchiveIOException
protected array
# readCentralDir( )

Read the central directory

Read the central directory

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

Returns

array
protected array
# readCentralFileHeader( )

Read the next central file header

Read the next central file header

Assumes the current file pointer is pointing at the right position

Returns

array
protected array
# readFileHeader( array $header )

Reads the local file header

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.

Parameters

$header
the central file header read previously (see above)

Returns

array
protected array
# parseExtra( string $header )

Parse the extra headers into fields

Parse the extra headers into fields

Parameters

$header

Returns

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

Create fileinfo object from header data

Create fileinfo object from header data

Parameters

$header

Returns

splitbrain\PHPArchive\FileInfo
protected string
# cpToUtf8( $string )

Convert the given CP437 encoded string to UTF-8

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.

Parameters

$string

Returns

string
protected string
# utf8ToCp( $string )

Convert the given UTF-8 encoded string to CP437

Convert the given UTF-8 encoded string to CP437

Same caveats as for cpToUtf8() apply

Parameters

$string

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 integer
# writebytesAt( string $data, integer $offset )

Write to the open filepointer or memory at the specified offset

Write to the open filepointer or memory at the specified offset

Parameters

$data
$offset

Returns

integer
number of bytes written

Throws

splitbrain\PHPArchive\ArchiveIOException
protected integer
# dataOffset( )

Current data pointer position

Current data pointer position

Returns

integer

Fixme

might need a -1
protected integer
# makeDosTime( $time )

Create a DOS timestamp from a UNIX timestamp

Create a DOS timestamp from a UNIX timestamp

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

Parameters

$time

Returns

integer
protected integer
# makeUnixTime( $mdate = null, $mtime = null )

Create a UNIX timestamp from a DOS timestamp

Create a UNIX timestamp from a DOS timestamp

Parameters

$mdate
$mtime

Returns

integer
protected string
# makeCentralFileRecord( integer $offset, integer $ts, integer $crc, integer $len, integer $clen, string $name, boolean|null $comp = null )

Returns a local file header for the given data

Returns a local file header for the given data

Parameters

$offset
location of the local header
$ts
unix timestamp
$crc
CRC32 checksum of the uncompressed data
$len
length of the uncompressed data
$clen
length of the compressed data
$name
file name
$comp
if compression is used, if null it's determined from $len != $clen

Returns

string
protected string
# makeLocalFileHeader( integer $ts, integer $crc, integer $len, integer $clen, string $name, boolean|null $comp = null )

Returns a local file header for the given data

Returns a local file header for the given data

Parameters

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

Returns

string
protected string
# makeCrcAndSize( integer $crc, integer $len, integer $clen )

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.

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.

Parameters

$crc
CRC32 checksum of the uncompressed data
$len
length of the uncompressed data
$clen
length of the compressed data

Returns

string
protected array($filename,
# encodeFilename( $original )

Returns an allowed filename and an extra field header

Returns an allowed filename and an extra field header

When encoding stuff outside the 7bit ASCII range it needs to be placed in a separate extra field

Parameters

$original

Returns

array($filename,
$filename, $extra)
Methods inherited from splitbrain\PHPArchive\Archive
setCallback()
Constants summary
integer LOCAL_FILE_HEADER_CRC_OFFSET
# 14
Constants inherited from splitbrain\PHPArchive\Archive
COMPRESS_AUTO, COMPRESS_BZIP, COMPRESS_GZIP, COMPRESS_NONE
Properties summary
protected string $file
# ''
protected $fh
#
protected string $memory
# ''
protected boolean $closed
# true
protected boolean $writeaccess
# false
protected $ctrl_dir
#
protected integer $complevel
# 9
Properties inherited from splitbrain\PHPArchive\Archive
$callback
API documentation generated by ApiGen