Create simple and compatible ZIP-archives
With this class it is possible to create ZIP-archives that are compatible with the original PKZip 2.04g. This class does not provide a way to read ZIP-archives.
There are three possible options for the output:
Special features:
This class might use a lot of memory when creating ZIP-archives, especially the ZIP_TYPE_BUFFER variant which eventually requires the size of the resulting ZIP-archive plus (worst case) the size of the largest file plus the size of the largest compressed file. This might be a problem with large files or many, many smaller files. A workaround could be to either stream the ZIP-archive directly (ZIP_TYPE_STREAM) or write to a file (ZIP_TYPE_FILE) because those variants only require the size of the largest file, the largest compressed file and the size of the central directory.
This class is not able to read ZIP-archives.
This class either stores a file as-is using the PKZIP 'Store' method or compresses the file using the 'Deflate' method. There is no support for other (more advanced) compression algoritms and no encryption is used.
References
I implemented this class using the following references.
[1] The ultimate definition of the ZIP-archive format as published by PKWare, Inc. See: http://www.pkware.com/appnote.txt or http://www.pkware.com/support/zip-application-note. I used version 6.3.2 which was published on 28 September 2007.
[2] RFC1950 - ZLIB Compressed Data Format Specification version 3.3, P. Deutsch, J-L. Gailly (May 1996), http://www.faqs.org/rfcs/rfc1950
[3] RFC1951 - DEFLATE Compressed Data Format Specification version 1.3, P. Deutsch (May 1996), http://www.faqs.org/rfcs/rfc1951
[4] Disk Operating System Technical Reference, IBM Corporation 1985, Chapter 5 (DOS Disk Directory).
[5] Official registration of the application/zip MIME-type: http://www.iana.org/assignments/media-types/application/zip
Examples
Typical usage of this class is as follows.
Example 1 - store 3 existing files in a ZIP-archive
$zip = new Zip; $zip->OpenFile("/tmp/test.zip"); $zip->AddFile("/tmp/foo.txt"); $zip->AddFile("/tmp/bar.txt"); $zip->AddFile("/tmp/baz.txt"); $zip->CloseZip();
Example 2 - store a chunk of data in a ZIP-archive in memory
$zip_archive = ''; $data = "This is example-data that ends up in file QUUX.TXT"; $zip = new Zip; $zip->OpenZipbuffer($zip_archive); $zip->AddData($data,'QUUX.TXT'); $zip->CloseZip();
Example 3 - directly stream a file in a ZIP-archive and rename on the fly
$zip = new Zip; $zip->OpenStream('htdocs.zip'); $zip->AddFile("/var/www/index.html",'INDEX.HTM'); $zip->CloseZip();
All methods return TRUE on success or FALSE on failure. If the method failed, an (English) error message can be found in $zip->Error.
Located in /program/lib/zip.class.php (line 139)
constructor initialises all variables
add data to the current ZIP-archive
This adds the data to the current ZIP-archive.
add the contents of an existing file to the current ZIP-archive
this reads the file $path into a buffer, and subsequently adds the data to the ZIP-archive.
finish the ZIP-archive by outputting the central directory and closing output
this finishes the ZIP-archive by constructing and outputting the Central Directory and subsequently closing the output file (in case of ZIP_TYPE_FILE). The call to CloseZip() is necessary to create a complete ZIP-archive, including the Central Directory.
construct an MS-DOS time and date based on unix timestamp
this routine constructs a string of 2 x 2 bytes with the time and the date in the following format.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 h h h h h m m m m m m x x x x x hhhhh = hour, from 0 - 23 (5 bits) mmmmmm = minute, from 0 to 59 (6 bits) xxxxx = duoseconds, from 0 to 29 (5 bits) 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 y y y y y y y m m m m d d d d d yyyyyyy = year offset from 1980, from 0 - 119 (7 bits) mmmm = month, from 1 to 12 (4 bits) ddddd = day, from 1 to 31 (5 bits)
Note that the time resolution is 2 seconds whereas the unix timestamp has a 1 second resolution. This means that the seconds are rounded down. Also note that the specification [4] indicates that the maximum value for year offset is 119 which corresponds with 2099 rather than the maximum of 127 which would yield the year 2107.
construct a suitable filename for use in ZIP-archive
this analyses and edits the string $filename in such a way that a suitable name for use in a ZIP-archive remains. This means that:
prepare the user supplied buffer for subsequent ZIP-archive data
open a file for subsequent output of ZIP-archive
this opens the file $path for writing and also sets the zip_type to ZIP_TYPE_FILE. The optional $comment is stored for future reference. The file must be closed afterwards via CloseZip().
start with a stream (direct output) indicating an application/zip type of content
this starts the output of the ZIP-archive directly to the browser. The Content-Type and the Content-Disposition are set by sending headers. The stream must be closed afterwards via CloseZip().
workhorse function to add data to the current ZIP-archive
This actually adds the data to the current ZIP-archive.
Note that we try to make a wise decision about compressed data: the compressed data should be smaller than the uncompressed data. If not, we don't bother and simply store the data as-is.
We also try to keep the number of copies of the data down to a minimum by not copying the $data but selecting between $data and $zdata only when we are really ready to write output the data.
workhorse function to add 0, 1 or more directories to the current ZIP-archive
this routine works from top to bottom through the specified path, adding directories to the archive. If a particular directory was already added before, it is not added again. This information is based on the existence of the corresponding key in the central_directory array.
add an error message to the list of error messages
Documentation generated on Tue, 28 Jun 2016 19:12:49 +0200 by phpDocumentor 1.4.0