TbsZip is a PHP class used to read and write Zip compressed file packages. This class does not require PHP extensions or temporary files. TbsZip allows you to read, write, modify, and delete compressed files.
<?
/* Some code examples for TbsZip
Skrol29, 2010-09-03
*/
Include_once ('tbszip. php'); // load the tbszip library
$ Zip = new clsTbsZip (); // create a new instance of the TbsZip class
$ Zip-> CreateNew (); // start a new empty archive for adding files
// Or
$ Zip-> Open('an_existing_archive.zip '); // open an existing archive for reading and/or modifying
//--------------------------------------------------
// Reading information and data in the opened archive
//--------------------------------------------------
// Check if a file is existing in the archive, the name must precise subfolders if any
$ OK = $ zip-> FileExists ('subfolder/help.html ');
// Count the files stored in the archive
$ File_nbr = count ($ zip-> CdFileLst );
// Retrieve the content of an compressed file in the archive
$ Text1 = $ zip-> FileRead('readme.txt ');
// Retrieve the content of an compressed file in a subfolder of the archive
$ Text2 = $ zip-> FileRead ('subfolder/readme.txt ');
If ($ OK) $ zip-> FileExists ('subfolder/help.html ');
//-----------------------------
// Modifying data in the archive
//-----------------------------
// Add a file in the archive
$ Zip-> FileAdd('newfile.txt ', $ data, TBSZIP_STRING); // add the file by giving the content
$ Zip-> FileAdd('newpic1.png ','./images/localpic1.png ', TBSZIP_FILE); // add the file by copying a local file
$ Zip-> FileAdd('newpic2.png ','./images/localpic2.png ', TBSZIP_FILE, false); // add the uncompressed file by copying a local file
// Delete an existing file in the archive
$ Zip-> FileReplace('newfile.txt ', $ data, TBSZIP_STRING); // replace the file by giving the content
$ Zip-> FileReplace('newpic1.png ','./images/localpic1.png ', TBSZIP_FILE); // replace the file by copying a local file
$ Zip-> FileReplace('newpic2.png ','./images/localpic2.png ', TBSZIP_FILE, false); // replace the uncompressed file by copying a local file
$ Zip-> FileReplace('newpic3.png ', false); // delete the file in the archive
// Cancel the last modification on the file (add/replace/delete)
$ Zip-> FileCancelModif('newpic2.png ');
//----------------------
// Applying modifications
//----------------------
$ Zip-> Flush (TBSZIP_FILE, './save/new_archive.zip'); // apply modifications as a new local file
// Apply modifications as an HTTP downloaded file
$ Zip-> Flush (TBSZIP_DOWNLOAD, 'download.zip ');
$ Zip-> Flush (TBSZIP_DOWNLOAD, 'download.zip ', 'application/zip'); // with a specific Content-Type
// Apply modifications as a downloaded file with your customized HTTP headers
Header ("Content-type: application/force-download ");
Header ("Content-Disposition: attachment; filename=download.zip ");
Header ("Expires: Fri, 01 Jan 2010 05:00:00 GMT ");
$ Zip-> Flush (TBSZIP_DOWNLOAD + TBSZIP_NOHEADER );
//-----------------
// Close the archive
//-----------------
$ Zip-> Close (); // stop to work with the opened archive. Modifications are not applied to the opened archive, use Flush () to commit