TbsZip is a PHP class which can read a zip archive, and even edit sub-files in the archive and then create a new archive. It works without any temporary file. While this class is independent from other libraries, it has been first created for the OpenTBS project which is a plugin for the TinyButStrong Template Engine. OpenTBS makes TinyButStrong able to build OpenOffice and Ms Office documents with the technique of templates.
TbsZip is developed under PHP5 but should be compatible with PHP4.
$zip = new clsTbsZip(); // instantiate the class $zip->Open('archive1.zip'); // open an existing zip archive $ok = $zip->FileExists('innerfolder/subfile1.txt'); // check if a sub-file exist in the archive $txt = $zip->FileRead('subfile2.txt'); // retrieve the content of a sub-file ... // some work on the $txt contents $zip->FileReplace('subfile2.txt', $txt, TBSZIP_STRING); // replace the existing sub-file $zip->FileReplace('subfile3.txt', false); // delete the existing sub-file $zip->FileAdd('subfile4.txt', $txt3, TBSZIP_STRING); // add a new sub-file $zip->Flush(TBSZIP_FILE, 'archive1_new.zip'); // flush the modifications as a new archive $zip->Close(); // close the current archive
Class name: clsTbsZip
|Method or Property||Description|
|Open($ArchFile, $UseIncludePath=false)|| Open an original Zip archive (it can be an LibreOffice document, an MS Office document or any other Zip archive).
- since TbsZip version 2.14, $ArchFile can be a PHP file handle
- argument $UseIncludePath is supported since TbsZip version 2.12
|CreateNew()|| Create a new empty Zip archive. The new archive is virtually prepared in the PHP memory, nothing is written at this point. Methods FileExists(), FileRead() and FileReplace() cannot be used when you work on an new archive created by CreateNew(), simply because the archive is empty.
Versioning: method CreateNew() is supported since TbsZip version 2.1
|FileExists($NameOrIdx)||Return true if the file exists in the original archive. $NameOrIdx must be the full name (including folder name inside the archive) or the index of an existing file in the archive.|
|FileRead($NameOrIdx, $Uncompress=true)||Return the contents of the file stored in the original archive. If $Uncompress is true, then TbsZip tryies to uncompressed the contents if needed. If the file is compressed but TbsZip cannot uncompress it then an error is raised. You can check if the result is compressed using property LastReadComp.|
|FileReplace($NameOrIdx, $Data, $DataType=TBSZIP_STRING, $Compress=true)||
Replace or delete an existing file in the archive. Set $Data=false in order to delete the file. $DataType accepts TBSZIP_STRING and TBSZIP_FILE ($Data must then be the path of the external file to insert). $Compress can be true, false or an array with keys ('meth','len_u','crc32') which means that the data is already previously compressed. Note that the original archive is not modified, modifications will be provided as a new archive when you call the method Flush().Returned values:
It's very interesting to know that when you add or replace a file in the archive with an external file (i.e. using option TBSZIP_FILE), then the contents of the file is not loaded immediately in PHP memory. It will be loaded, compressed and output on the fly and one by one when method Flush() is called. Thus, you can add lot of files in an archive without occupying the PHP memory.
|FileAdd($Name, $Data, $DataType=TBSZIP_STRING, $Compress=true)|| Add a new file in the archive, works like FileReplace(). Note that the original archive is not modified, modifications will be provided as a new archive when you call the method Flush().
If $Data is false then the previously add file with the given name is canceled if any.
|FileCancelModif($NameOrIdx)||Cancel add, delete and replacements in the archive for the given file name. Return the number of cancels.|
Return a string that represents the state of the file in the archive:
|Flush($Render=TBSZIP_DOWNLOAD, $File='', $ContentType='')||
Actually create the new archive with all modifications. External files to be inserted as sub-files are loaded during this proces and not before. They are compressed and output on the fly and one by one without staying in the PHP memory. No temporay files are used.The $Render option:
|Debug()||Display information about the original archive.|
|Close()||Close the opened original archive.|
|ArchFile||Full path of the opened original archive.|
|CdFileLst||An array listing all existing files in the original archive with some technical information.|
Index of the last file read by FileRead().
|LastReadComp||Compression information about of the last file read by FileRead().
false means the file was not found in the archive,
-1 means the file was compressed and has been successfully uncompressed,
0 means the file was not compressed,
1 means the file was compressed but TbsZip was unable to uncompress.
|DisplayError||Default value is false until version 2.3, and true since version 2.4.
If the value is true, TbsZip error messages are displayed (using echo).
In any case, the last TbsZip error message is saved into property Error.
It can be interesting to not display error directly, for example when you flush the new archive with the Download option, the error message may be merged to the downloaded file.
|Error||Last error message, or false if no TbsZip error. Use this property in order to check errors when property DisplayError is set to false.|
Version 2.16, date: 2014-04-08
Version 2.15, date: 2013-10-15
Version 2.14, date: 2013-06-11
Version 2.13, date: 2013-04-14
Version 2.12, date: 2013-03-16
Version 2.11, date: 2011-02-14
Version 2.10, date: 2011-08-13
Version 2.9, date: 2011-07-22
Version 2.8, date: 2011-06-08
Version 2.7, date: 2011-06-07
Version 2.6, date: 2011-06-07
Version 2.5, date: 2011-05-12
Version 2.4, date: 2011-03-25
Version 2.3, date: 2010-11-29
Version 2.2, date: 2010-10-28
Version 2.1, date: 2010-07-01