Zip

The Zip class enables the extraction of zip archives according to Wikipedia’s zip file format information (don’t you just love Wikipedia?).

Note

This class implements the Extractor interface.

Summary
ZipThe Zip class enables the extraction of zip archives according to Wikipedia’s zip file format information (don’t you just love Wikipedia?)
Variables
filename
fp
Functions
__constructInitializes all attributes to null and can also be used to open a zipped archive.
openOpens the specified zip archive for reading.
filesReturns an array containing all the files and folders contained within the current zip archive.
extractExtracts the current zip archive to the specified location.
readReads the specified file from the current zip archive and returns the contents of the file or saves it to the specified file.
closeCloses the zip archive currently being read.
__destruct
is_supportedReturns whether or not a zip archive could be extracted on the current system.

Variables

Summary

filename

private $filename

fp

private $fp

Functions

Summary
__constructInitializes all attributes to null and can also be used to open a zipped archive.
openOpens the specified zip archive for reading.
filesReturns an array containing all the files and folders contained within the current zip archive.
extractExtracts the current zip archive to the specified location.
readReads the specified file from the current zip archive and returns the contents of the file or saves it to the specified file.
closeCloses the zip archive currently being read.
__destruct
is_supportedReturns whether or not a zip archive could be extracted on the current system.

__construct

public function __construct($filename =  null)

Initializes all attributes to null and can also be used to open a zipped archive.

Parameters

string $filenameThe name of the zip to open.

open

public function open($filename)

Opens the specified zip archive for reading.

Parameters

string $filenameThe name of the zip archive to open.

Returns

boolReturns true if the zip archive was opened successfully, false if the file does not exist or is not a valid zip archive.

Note

This method may also return false if extracting zip archives are not supported on this system.  See Zip::is_supported for more details.

files

public function files()

Returns an array containing all the files and folders contained within the current zip archive.

Parameters

none

Returns

arrayReturns an array containing all the files and folders in the zip archive.

extract

public function extract($destination,  
$safe_mode =  true)

Extracts the current zip archive to the specified location.

Parameters

string $destinationWhere the contents of the zip archive will be extracted to.
bool $safe_modeIt is, of course, possible for files to have such names as ../../someImportantFile.sys which would then possibly overwrite a very important file of any kind.  By setting this to true, the name of the file will have ../ and /.. removed.

Returns

boolReturns true on success, false on failure.

read

public function read($filename,  
$destination =  null)

Reads the specified file from the current zip archive and returns the contents of the file or saves it to the specified file.

Parameters

string $filenameThe name of the file in the zip archive to read.
string $destinationThe location where $filename should be saved to, if left blank the contents will be returned.

Returns

mixedReturns a string containing the files contents if $destination is left empty but false if the file does not exist.  If a destination is supplied, true will be returned on success and false on failure (e.g. the file does not exist).

Note

File names are case-sensitive!

This does not work on directories in the archives, only files can be retrieved.

If you want to retrieve a list of all the directories (and files) within a zip archive, check out Zip::files.

close

public function close()

Closes the zip archive currently being read.

Parameters

none

Returns

voidNothing is returned by this method.

Note

This method is automatically called when the object is destroyed.

__destruct

public function __destruct()

is_supported

public function is_supported()

Returns whether or not a zip archive could be extracted on the current system.

Parameters

none

Returns

boolReturns true if a zip archive could be extracted on the current system, false if not.

Note

In order for a zip archive to be extracted the Zip class requires the use of the <www.php.net/gzinflate> function which is part of the <www.php.net/zlib> extension.

private $filename
private $fp
public function __construct($filename =  null)
Initializes all attributes to null and can also be used to open a zipped archive.
public function open($filename)
Opens the specified zip archive for reading.
public function files()
Returns an array containing all the files and folders contained within the current zip archive.
public function extract($destination,  
$safe_mode =  true)
Extracts the current zip archive to the specified location.
public function read($filename,  
$destination =  null)
Reads the specified file from the current zip archive and returns the contents of the file or saves it to the specified file.
public function close()
Closes the zip archive currently being read.
public function __destruct()
public function is_supported()
Returns whether or not a zip archive could be extracted on the current system.
The Extractor interface defines the methods which must be defined if a class is to be registered as a decompression handler in the Extraction class.
Close