\PhpZipZipFile

Create, open .ZIP files, modify, get info and extract files.

Implemented support traditional PKWARE encryption and WinZip AES encryption. Implemented support ZIP64. Support ZipAlign functional.

Summary

Methods
Properties
Constants
__construct()
openFile()
openFromString()
openFromStream()
getListFiles()
count()
getArchiveComment()
setArchiveComment()
hasEntry()
getEntry()
isDirectory()
getEntryComment()
setEntryComment()
getEntryContents()
getEntryStream()
getEntryInfo()
getAllInfo()
matcher()
getEntries()
extractTo()
addFromString()
addFromFinder()
addSplFile()
addFile()
addFromStream()
addEmptyDir()
addDir()
addDirRecursive()
addFilesFromIterator()
addFilesFromGlob()
addFilesFromGlobRecursive()
addAll()
rename()
deleteFromName()
deleteFromGlob()
deleteFromRegex()
deleteAll()
setCompressionLevel()
setCompressionLevelEntry()
setCompressionMethodEntry()
setZipAlign()
setReadPassword()
setReadPasswordEntry()
setPassword()
setPasswordEntry()
disableEncryption()
disableEncryptionEntry()
unchangeAll()
unchangeArchiveComment()
unchangeEntry()
saveAsFile()
saveAsStream()
outputAsAttachment()
outputAsResponse()
outputAsString()
close()
rewrite()
__destruct()
offsetSet()
offsetUnset()
current()
offsetGet()
key()
next()
valid()
offsetExists()
rewind()
No public properties found
No constants found
createZipReader()
createZipWriter()
createZipContainer()
normalizeEntryName()
addZipEntry()
getMimeTypeByFilename()
writeZipToStream()
onBeforeSave()
$zipContainer
N/A
addGlob()
addRegex()
doAddFiles()
$defaultMimeTypes
$reader
N/A

Properties

$zipContainer

$zipContainer : \PhpZip\Model\ZipContainer

Type

ZipContainer

$defaultMimeTypes

$defaultMimeTypes : array

Type

array — default mime types

Methods

__construct()

__construct() : mixed

ZipFile constructor.

Returns

mixed —

openFile()

openFile(string  $filename, array  $options = []) : \PhpZip\ZipFile

Open zip archive from file.

Parameters

string $filename
array $options

Throws

\PhpZip\Exception\ZipException

if can't open file

Returns

\PhpZip\ZipFile —

openFromString()

openFromString(string  $data, array  $options = []) : \PhpZip\ZipFile

Open zip archive from raw string data.

Parameters

string $data
array $options

Throws

\PhpZip\Exception\ZipException

if can't open temp stream

Returns

\PhpZip\ZipFile —

openFromStream()

openFromStream(resource  $handle, array  $options = []) : \PhpZip\ZipFile

Open zip archive from stream resource.

Parameters

resource $handle
array $options

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

getListFiles()

getListFiles() : string[]

Returns

string[] —

returns the list files

count()

count() : int

Returns

int —

returns the number of entries in this ZIP file

getArchiveComment()

getArchiveComment() : string|null

Returns the file comment.

Returns

string|null —

the file comment

setArchiveComment()

setArchiveComment(string|null  $comment = null) : \PhpZip\ZipFile

Set archive comment.

Parameters

string|null $comment

Returns

\PhpZip\ZipFile —

hasEntry()

hasEntry(string  $entryName) : bool

Checks if there is an entry in the archive.

Parameters

string $entryName

Returns

bool —

getEntry()

getEntry(string  $entryName) : \PhpZip\Model\ZipEntry

Returns ZipEntry object.

Parameters

string $entryName

Throws

\PhpZip\Exception\ZipEntryNotFoundException

Returns

\PhpZip\Model\ZipEntry —

isDirectory()

isDirectory(string  $entryName) : bool

Checks that the entry in the archive is a directory.

Returns true if and only if this ZIP entry represents a directory entry (i.e. end with '/').

Parameters

string $entryName

Throws

\PhpZip\Exception\ZipEntryNotFoundException

Returns

bool —

getEntryComment()

getEntryComment(string  $entryName) : string

Returns entry comment.

Parameters

string $entryName

Throws

\PhpZip\Exception\ZipEntryNotFoundException
\PhpZip\Exception\ZipException

Returns

string —

setEntryComment()

setEntryComment(string  $entryName, string|null  $comment = null) : \PhpZip\ZipFile

Set entry comment.

Parameters

string $entryName
string|null $comment

Throws

\PhpZip\Exception\ZipException
\PhpZip\Exception\ZipEntryNotFoundException

Returns

\PhpZip\ZipFile —

getEntryContents()

getEntryContents(string  $entryName) : string

Returns the entry contents.

Parameters

string $entryName

Throws

\PhpZip\Exception\ZipException
\PhpZip\Exception\ZipEntryNotFoundException

Returns

string —

getEntryStream()

getEntryStream(string  $entryName) : resource

Parameters

string $entryName

Throws

\PhpZip\Exception\ZipException
\PhpZip\Exception\ZipEntryNotFoundException

Returns

resource —

getEntryInfo()

getEntryInfo(string|\PhpZip\Model\ZipEntry  $entryName) : \PhpZip\Model\ZipInfo

Get info by entry.

Parameters

string|\PhpZip\Model\ZipEntry $entryName

Throws

\PhpZip\Exception\ZipEntryNotFoundException
\PhpZip\Exception\ZipException

Returns

\PhpZip\Model\ZipInfo —

getAllInfo()

getAllInfo() : \PhpZip\Model\ZipInfo[]

Get info by all entries.

Returns

\PhpZip\Model\ZipInfo[] —

matcher()

matcher() : \PhpZip\Model\ZipEntryMatcher

Returns

\PhpZip\Model\ZipEntryMatcher —

getEntries()

getEntries() : \PhpZip\Model\ZipEntry[]

Returns an array of zip records (ex. for modify time).

Returns

\PhpZip\Model\ZipEntry[] —

array of raw zip entries

extractTo()

extractTo(string  $destDir, array|string|null  $entries = null, array  $options = [], array  $extractedEntries = []) : \PhpZip\ZipFile

Extract the archive contents (unzip).

Extract the complete archive or the given files to the specified destination.

Parameters

string $destDir

location where to extract the files

array|string|null $entries

entries to extract

array $options

extract options

array $extractedEntries

if the extractedEntries argument is present, then the specified array will be filled with information about the extracted entries

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addFromString()

addFromString(string  $entryName, string  $contents, int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add entry from the string.

Parameters

string $entryName

zip entry name

string $contents

string contents

int|null $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addFromFinder()

addFromFinder(\Symfony\Component\Finder\Finder  $finder, array  $options = []) : \PhpZip\Model\ZipEntry[]

Parameters

\Symfony\Component\Finder\Finder $finder
array $options

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\Model\ZipEntry[] —

addSplFile()

addSplFile(\SplFileInfo  $file, string|null  $entryName = null, array  $options = []) : \PhpZip\Model\ZipEntry

Parameters

\SplFileInfo $file
string|null $entryName
array $options

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\Model\ZipEntry —

addFile()

addFile(string  $filename, string|null  $entryName = null, int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add entry from the file.

Parameters

string $filename

destination file

string|null $entryName

zip Entry name

int|null $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addFromStream()

addFromStream(resource  $stream, string  $entryName, int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add entry from the stream.

Parameters

resource $stream

stream resource

string $entryName

zip Entry name

int|null $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addEmptyDir()

addEmptyDir(string  $dirName) : \PhpZip\ZipFile

Add an empty directory in the zip archive.

Parameters

string $dirName

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addDir()

addDir(string  $inputDir, string  $localPath = '/', int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add directory not recursively to the zip archive.

Parameters

string $inputDir

Input directory

string $localPath

add files to this directory, or the root

int|null $compressionMethod

Compression method.

                           Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or
                           {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addDirRecursive()

addDirRecursive(string  $inputDir, string  $localPath = '/', int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add recursive directory to the zip archive.

Parameters

string $inputDir

Input directory

string $localPath

add files to this directory, or the root

int|null $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addFilesFromIterator()

addFilesFromIterator(\Iterator  $iterator, string  $localPath = '/', int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add directories from directory iterator.

Parameters

\Iterator $iterator

directory iterator

string $localPath

add files to this directory, or the root

int|null $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addFilesFromGlob()

addFilesFromGlob(string  $inputDir, string  $globPattern, string  $localPath = '/', int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add files from glob pattern.

Parameters

string $inputDir

Input directory

string $globPattern

glob pattern

string $localPath

add files to this directory, or the root

int|null $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addFilesFromGlobRecursive()

addFilesFromGlobRecursive(string  $inputDir, string  $globPattern, string  $localPath = '/', int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add files recursively from glob pattern.

Parameters

string $inputDir

Input directory

string $globPattern

glob pattern

string $localPath

add files to this directory, or the root

int|null $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addAll()

addAll(array  $mapData) : mixed

Add array data to archive.

Keys is local names. Values is contents.

Parameters

array $mapData

associative array for added to zip

Returns

mixed —

rename()

rename(string  $oldName, string  $newName) : \PhpZip\ZipFile

Rename the entry.

Parameters

string $oldName

old entry name

string $newName

new entry name

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

deleteFromName()

deleteFromName(string  $entryName) : \PhpZip\ZipFile

Delete entry by name.

Parameters

string $entryName

zip Entry name

Throws

\PhpZip\Exception\ZipEntryNotFoundException

if entry not found

Returns

\PhpZip\ZipFile —

deleteFromGlob()

deleteFromGlob(string  $globPattern) : \PhpZip\ZipFile

Delete entries by glob pattern.

Parameters

string $globPattern

Glob pattern

Returns

\PhpZip\ZipFile —

deleteFromRegex()

deleteFromRegex(string  $regexPattern) : \PhpZip\ZipFile

Delete entries by regex pattern.

Parameters

string $regexPattern

Regex pattern

Returns

\PhpZip\ZipFile —

deleteAll()

deleteAll() : \PhpZip\ZipFile

Delete all entries.

Returns

\PhpZip\ZipFile —

setCompressionLevel()

setCompressionLevel(int  $compressionLevel = ZipCompressionLevel::NORMAL) : \PhpZip\ZipFile

Set compression level for new entries.

Parameters

int $compressionLevel

Returns

\PhpZip\ZipFile —

setCompressionLevelEntry()

setCompressionLevelEntry(string  $entryName, int  $compressionLevel) : \PhpZip\ZipFile

Parameters

string $entryName
int $compressionLevel

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

setCompressionMethodEntry()

setCompressionMethodEntry(string  $entryName, int  $compressionMethod) : \PhpZip\ZipFile

Parameters

string $entryName
int $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

setZipAlign()

setZipAlign(int|null  $align = null) : \PhpZip\ZipFile

zipalign is optimization to Android application (APK) files.

Parameters

int|null $align

Returns

\PhpZip\ZipFile —

setReadPassword()

setReadPassword(string  $password) : \PhpZip\ZipFile

Set password to all input encrypted entries.

Parameters

string $password

Password

Returns

\PhpZip\ZipFile —

setReadPasswordEntry()

setReadPasswordEntry(string  $entryName, string  $password) : \PhpZip\ZipFile

Set password to concrete input entry.

Parameters

string $entryName
string $password

Password

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

setPassword()

setPassword(string  $password, int|null  $encryptionMethod = ZipEncryptionMethod::WINZIP_AES_256) : \PhpZip\ZipFile

Sets a new password for all files in the archive.

Parameters

string $password

Password

int|null $encryptionMethod

Encryption method

Returns

\PhpZip\ZipFile —

setPasswordEntry()

setPasswordEntry(string  $entryName, string  $password, int|null  $encryptionMethod = null) : \PhpZip\ZipFile

Sets a new password of an entry defined by its name.

Parameters

string $entryName
string $password
int|null $encryptionMethod

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

disableEncryption()

disableEncryption() : \PhpZip\ZipFile

Disable encryption for all entries that are already in the archive.

Returns

\PhpZip\ZipFile —

disableEncryptionEntry()

disableEncryptionEntry(string  $entryName) : \PhpZip\ZipFile

Disable encryption of an entry defined by its name.

Parameters

string $entryName

Returns

\PhpZip\ZipFile —

unchangeAll()

unchangeAll() : \PhpZip\ZipFile

Undo all changes done in the archive.

Returns

\PhpZip\ZipFile —

unchangeArchiveComment()

unchangeArchiveComment() : \PhpZip\ZipFile

Undo change archive comment.

Returns

\PhpZip\ZipFile —

unchangeEntry()

unchangeEntry(string|\PhpZip\Model\ZipEntry  $entry) : \PhpZip\ZipFile

Revert all changes done to an entry with the given name.

Parameters

string|\PhpZip\Model\ZipEntry $entry

Entry name or ZipEntry

Returns

\PhpZip\ZipFile —

saveAsFile()

saveAsFile(string  $filename) : \PhpZip\ZipFile

Save as file.

Parameters

string $filename

Output filename

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

saveAsStream()

saveAsStream(resource  $handle) : \PhpZip\ZipFile

Save as stream.

Parameters

resource $handle

Output stream resource

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

outputAsAttachment()

outputAsAttachment(string  $outputFilename, string|null  $mimeType = null, bool  $attachment = true) : mixed

Output .ZIP archive as attachment.

Die after output.

Parameters

string $outputFilename

Output filename

string|null $mimeType

Mime-Type

bool $attachment

Http Header 'Content-Disposition' if true then attachment otherwise inline

Throws

\PhpZip\Exception\ZipException

Returns

mixed —

outputAsResponse()

outputAsResponse(\Psr\Http\Message\ResponseInterface  $response, string  $outputFilename, string|null  $mimeType = null, bool  $attachment = true) : \Psr\Http\Message\ResponseInterface

Output .ZIP archive as PSR-7 Response.

Parameters

\Psr\Http\Message\ResponseInterface $response

Instance PSR-7 Response

string $outputFilename

Output filename

string|null $mimeType

Mime-Type

bool $attachment

Http Header 'Content-Disposition' if true then attachment otherwise inline

Throws

\PhpZip\Exception\ZipException

Returns

\Psr\Http\Message\ResponseInterface —

outputAsString()

outputAsString() : string

Returns the zip archive as a string.

Throws

\PhpZip\Exception\ZipException

Returns

string —

close()

close() : mixed

Close zip archive and release input stream.

Returns

mixed —

rewrite()

rewrite() : \PhpZip\ZipFile

Save and reopen zip archive.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

__destruct()

__destruct() : mixed

Release all resources.

Returns

mixed —

offsetSet()

offsetSet(string  $entryName, string|\DirectoryIterator|\SplFileInfo|resource  $contents) : mixed

Offset to set.

Parameters

string $entryName

the offset to assign the value to

string|\DirectoryIterator|\SplFileInfo|resource $contents

the value to set

Throws

\PhpZip\Exception\ZipException

Returns

mixed —

offsetUnset()

offsetUnset(string  $entryName) : mixed

Offset to unset.

Parameters

string $entryName

the offset to unset

Throws

\PhpZip\Exception\ZipEntryNotFoundException

Returns

mixed —

current()

current() : mixed

Return the current element.

Throws

\PhpZip\Exception\ZipException

Returns

mixed —

can return any type

offsetGet()

offsetGet(string  $entryName) : string|null

Offset to retrieve.

Parameters

string $entryName

the offset to retrieve

Throws

\PhpZip\Exception\ZipException

Returns

string|null —

key()

key() : mixed

Return the key of the current element.

Returns

mixed —

scalar on success, or null on failure

next()

next() : mixed

Move forward to next element.

Returns

mixed —

valid()

valid() : bool

Checks if current position is valid.

Returns

bool —

The return value will be casted to boolean and then evaluated. Returns true on success or false on failure.

offsetExists()

offsetExists(string  $entryName) : bool

Whether a offset exists.

Parameters

string $entryName

an offset to check for

Returns

bool —

true on success or false on failure. The return value will be casted to boolean if non-boolean was returned.

rewind()

rewind() : mixed

Rewind the Iterator to the first element.

Returns

mixed —

createZipReader()

createZipReader(resource  $inputStream, array  $options = []) : \PhpZip\IO\ZipReader

Parameters

resource $inputStream
array $options

Returns

\PhpZip\IO\ZipReader —

createZipWriter()

createZipWriter() : \PhpZip\IO\ZipWriter

Returns

\PhpZip\IO\ZipWriter —

createZipContainer()

createZipContainer(\PhpZip\Model\ImmutableZipContainer|null  $sourceContainer = null) : \PhpZip\Model\ZipContainer

Parameters

\PhpZip\Model\ImmutableZipContainer|null $sourceContainer

Returns

\PhpZip\Model\ZipContainer —

normalizeEntryName()

normalizeEntryName(string  $entryName) : string

Parameters

string $entryName

Returns

string —

addZipEntry()

addZipEntry(\PhpZip\Model\ZipEntry  $zipEntry) : mixed

Parameters

\PhpZip\Model\ZipEntry $zipEntry

Returns

mixed —

getMimeTypeByFilename()

getMimeTypeByFilename(string  $outputFilename) : string

Parameters

string $outputFilename

Returns

string —

writeZipToStream()

writeZipToStream(resource  $handle) : mixed

Parameters

resource $handle

Throws

\PhpZip\Exception\ZipException

Returns

mixed —

onBeforeSave()

onBeforeSave() : mixed

Event before save or output.

Returns

mixed —

addGlob()

addGlob(string  $inputDir, string  $globPattern, string  $localPath = '/', bool  $recursive = true, int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add files from glob pattern.

Parameters

string $inputDir

Input directory

string $globPattern

glob pattern

string $localPath

add files to this directory, or the root

bool $recursive

recursive search

int|null $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

addRegex()

addRegex(string  $inputDir, string  $regexPattern, string  $localPath = '/', bool  $recursive = true, int|null  $compressionMethod = null) : \PhpZip\ZipFile

Add files from regex pattern.

Parameters

string $inputDir

search files in this directory

string $regexPattern

regex pattern

string $localPath

add files to this directory, or the root

bool $recursive

recursive search

int|null $compressionMethod

Compression method. Use {@see \PhpZip\Constants\ZipCompressionMethod::STORED}, {@see \PhpZip\Constants\ZipCompressionMethod::DEFLATED} or {@see \PhpZip\Constants\ZipCompressionMethod::BZIP2}. If null, then auto choosing method.

Throws

\PhpZip\Exception\ZipException

Returns

\PhpZip\ZipFile —

doAddFiles()

doAddFiles(string  $fileSystemDir, array  $files, string  $zipPath, int|null  $compressionMethod = null) : mixed

Parameters

string $fileSystemDir
array $files
string $zipPath
int|null $compressionMethod

Throws

\PhpZip\Exception\ZipException

Returns

mixed —