\Zip

Class to create and manage a Zip file.

Inspired by CreateZipFile by Rochak Chauhan www.rochakchauhan.com (http://www.phpclasses.org/browse/package/2322.html) and http://www.pkware.com/documents/casestudies/APPNOTE.TXT Zip file specification.

License: GNU LGPL, Attribution required for commercial implementations, requested for everything else.

Summary

Methods
Properties
Constants
__construct()
__destruct()
setExtraField()
setComment()
setZipFile()
closeZipFile()
addDirectory()
addFile()
addDirectoryContent()
addLargeFile()
openStream()
addStreamData()
closeStream()
finalize()
getZipFile()
getZipData()
sendZip()
getArchiveSize()
pathJoin()
getRelativePath()
No public properties found
VERSION
ZIP_LOCAL_FILE_HEADER
ZIP_CENTRAL_FILE_HEADER
ZIP_END_OF_CENTRAL_DIRECTORY
EXT_FILE_ATTR_DIR
EXT_FILE_ATTR_FILE
ATTR_VERSION_TO_EXTRACT
ATTR_MADE_BY_VERSION
No protected methods found
No protected properties found
N/A
processFile()
getDosTime()
buildZipEntry()
zipwrite()
zipflush()
$zipMemoryThreshold
$zipData
$zipFile
$zipComment
$cdRec
$offset
$isFinalized
$addExtraField
$streamChunkSize
$streamFilePath
$streamTimeStamp
$streamComment
$streamFile
$streamData
$streamFileLength
N/A

Constants

VERSION

VERSION = 1.4

ZIP_LOCAL_FILE_HEADER

ZIP_LOCAL_FILE_HEADER = "PK\x03\x04"

ZIP_CENTRAL_FILE_HEADER

ZIP_CENTRAL_FILE_HEADER = "PK\x01\x02"

ZIP_END_OF_CENTRAL_DIRECTORY

ZIP_END_OF_CENTRAL_DIRECTORY = "PK\x05\x06\x00\x00\x00\x00"

EXT_FILE_ATTR_DIR

EXT_FILE_ATTR_DIR = "\x10\x00\xffA"

EXT_FILE_ATTR_FILE

EXT_FILE_ATTR_FILE = "\x00\x00\xff\x81"

ATTR_VERSION_TO_EXTRACT

ATTR_VERSION_TO_EXTRACT = "\x14\x00"

ATTR_MADE_BY_VERSION

ATTR_MADE_BY_VERSION = "\x1e\x03"

Properties

$zipMemoryThreshold

$zipMemoryThreshold

$zipData

$zipData

$zipFile

$zipFile

$zipComment

$zipComment

$cdRec

$cdRec

$offset

$offset

$isFinalized

$isFinalized

$addExtraField

$addExtraField

$streamChunkSize

$streamChunkSize

$streamFilePath

$streamFilePath

$streamTimeStamp

$streamTimeStamp

$streamComment

$streamComment

$streamFile

$streamFile

$streamData

$streamData

$streamFileLength

$streamFileLength

Methods

__construct()

__construct(bool  $useZipFile = true) : mixed

Constructor.

Parameters

bool $useZipFile

Write temp zip data to tempFile? Default FALSE

Returns

mixed —

__destruct()

__destruct() : mixed

Returns

mixed —

setExtraField()

setExtraField(bool  $setExtraField = TRUE) : mixed

Extra fields on the Zip directory records are Unix time codes needed for compatibility on the default Mac zip archive tool.

These are enabled as default, as they do no harm elsewhere and only add 26 bytes per file added.

Parameters

bool $setExtraField

TRUE (default) will enable adding of extra fields, anything else will disable it.

Returns

mixed —

setComment()

setComment(string  $newComment = NULL) : bool

Set Zip archive comment.

Parameters

string $newComment

New comment. NULL to clear.

Returns

bool —

$success

setZipFile()

setZipFile(string  $fileName) : bool

Set zip file to write zip data to.

This will cause all present and future data written to this class to be written to this file. This can be used at any time, even after the Zip Archive have been finalized. Any previous file will be closed. Warning: If the given file already exists, it will be overwritten.

Parameters

string $fileName

Returns

bool —

$success

closeZipFile()

closeZipFile() : mixed

Returns

mixed —

addDirectory()

addDirectory(string  $directoryPath, int  $timestamp, string  $fileComment = NULL) : bool

Add an empty directory entry to the zip archive.

Basically this is only used if an empty directory is added.

Parameters

string $directoryPath

Directory Path and name to be added to the archive.

int $timestamp

(Optional) Timestamp for the added directory, if omitted or set to 0, the current time will be used.

string $fileComment

(Optional) Comment to be added to the archive for this directory. To use fileComment, timestamp must be given.

Returns

bool —

$success

addFile()

addFile(string  $data, string  $filePath, int  $timestamp, string  $fileComment = NULL, bool  $compress = TRUE) : bool

Add a file to the archive at the specified location and file name.

Parameters

string $data

File data.

string $filePath

Filepath and name to be used in the archive.

int $timestamp

(Optional) Timestamp for the added file, if omitted or set to 0, the current time will be used.

string $fileComment

(Optional) Comment to be added to the archive for this file. To use fileComment, timestamp must be given.

bool $compress

(Optional) Compress file, if set to FALSE the file will only be stored. Default TRUE.

Returns

bool —

$success

addDirectoryContent()

addDirectoryContent(string  $realPath, string  $zipPath, bool  $recursive = TRUE, bool  $followSymlinks = TRUE, array  $addedFiles = array()) : mixed

Add the content to a directory.

Parameters

string $realPath

Path on the file system.

string $zipPath

Filepath and name to be used in the archive.

bool $recursive

Add content recursively, default is TRUE.

bool $followSymlinks

Follow and add symbolic links, if they are accessible, default is TRUE.

array $addedFiles

Reference to the added files, this is used to prevent duplicates, efault is an empty array. If you start the function by parsing an array, the array will be populated with the realPath and zipPath kay/value pairs added to the archive by the function.

Returns

mixed —

addLargeFile()

addLargeFile(string  $dataFile, string  $filePath, int  $timestamp, string  $fileComment = NULL) : bool

Add a file to the archive at the specified location and file name.

Parameters

string $dataFile

File name/path.

string $filePath

Filepath and name to be used in the archive.

int $timestamp

(Optional) Timestamp for the added file, if omitted or set to 0, the current time will be used.

string $fileComment

(Optional) Comment to be added to the archive for this file. To use fileComment, timestamp must be given.

Returns

bool —

$success

openStream()

openStream(string  $filePath, int  $timestamp, string  $fileComment = null) : bool

Create a stream to be used for large entries.

Parameters

string $filePath

Filepath and name to be used in the archive.

int $timestamp

(Optional) Timestamp for the added file, if omitted or set to 0, the current time will be used.

string $fileComment

(Optional) Comment to be added to the archive for this file. To use fileComment, timestamp must be given.

Returns

bool —

$success

addStreamData()

addStreamData(string  $data) : mixed

Add data to the open stream.

Parameters

string $data

Returns

mixed —

closeStream()

closeStream() : bool

Close the current stream.

Returns

bool —

$success

finalize()

finalize() : bool

Close the archive.

A closed archive can no longer have new files added to it.

Returns

bool —

$success

getZipFile()

getZipFile() : \zip

Get the handle ressource for the archive zip file.

If the zip haven't been finalized yet, this will cause it to become finalized

Returns

\zip —

file handle

getZipData()

getZipData() : \zip

Get the zip file contents If the zip haven't been finalized yet, this will cause it to become finalized

Returns

\zip —

data

sendZip()

sendZip(string  $fileName, string  $contentType = "application/zip") : bool

Send the archive as a zip download

Parameters

string $fileName

The name of the Zip archive, ie. "archive.zip".

string $contentType

Content mime type. Optional, defaults to "application/zip".

Returns

bool —

$success

getArchiveSize()

getArchiveSize() : mixed

Return the current size of the archive

Returns

mixed —

pathJoin()

pathJoin(string  $dir, string  $file) : mixed

Join $file to $dir path, and clean up any excess slashes.

Parameters

string $dir
string $file

Returns

mixed —

getRelativePath()

getRelativePath(string  $path) : string

Clean up a path, removing any unnecessary elements such as /./, // or redundant ../ segments.

If the path starts with a "/", it is deemed an absolute path and any /../ in the beginning is stripped off. The returned path will not end in a "/".

Parameters

string $path

The path to clean up

Returns

string —

the clean path

processFile()

processFile(mixed  $dataFile, mixed  $filePath, mixed  $timestamp, mixed  $fileComment = null) : mixed

Parameters

mixed $dataFile
mixed $filePath
mixed $timestamp
mixed $fileComment

Returns

mixed —

getDosTime()

getDosTime(int  $timestamp) : mixed

Calculate the 2 byte dostime used in the zip entries.

Parameters

int $timestamp

Returns

mixed —

buildZipEntry()

buildZipEntry(string  $filePath, string  $fileComment, string  $gpFlags, string  $gzType, int  $timestamp, string  $fileCRC32, int  $gzLength, int  $dataLength, int  $extFileAttr) : mixed

Build the Zip file structures

Parameters

string $filePath
string $fileComment
string $gpFlags
string $gzType
int $timestamp
string $fileCRC32
int $gzLength
int $dataLength
int $extFileAttr

Use self::EXT_FILE_ATTR_FILE for files, self::EXT_FILE_ATTR_DIR for Directories.

Returns

mixed —

zipwrite()

zipwrite(mixed  $data) : mixed

Parameters

mixed $data

Returns

mixed —

zipflush()

zipflush() : mixed

Returns

mixed —