PHP Classes

TAR/GZIP/BZIP2/ZIP Archives: Create tar/gzip/bzip2/zip, extract tar/gzip/bzip2.

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (29)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 68%Total: 27,867 All time: 18 This week: 349Down
Version Licenses PHP version Categories
archive 2.1BSD License, GNU ...5.0Files and Folders, Compression


Allows the creation of tar, gzip, bzip2, and zip archives, and the extraction of tar, gzip, and bzip2. Supports relative paths/no paths, comments, and recursing through subdirectories. Can write file to disk, allow user to download directly, or return file contents in a string or an array. Does not require any external programs to run. PHP must be compiled with '--with-zlib' for gzip and zip and '--with-bzip2' for bzip2. Supports creation of self-extracting zip archives. See readme for full details.

Picture of Devin Doucette
Name: Devin Doucette <contact>
Classes: 3 packages by
Country: Canada Canada
Age: 37
All time rank: 221 in Canada Canada
Week rank: 367 Down8 in Canada Canada Down



Examples of Compression:

The following example creates a gzipped tar file:
// Assume the following script is executing in /var/www/htdocs/test
// Create a new gzip file test.tgz in htdocs/test
$test = new gzip_file("htdocs/test/test.tgz");
// Set basedir to "../..", which translates to /var/www
// Overwrite /var/www/htdocs/test/test.tgz if it already exists
// Set compression level to 1 (lowest)
$test->set_options(array('basedir' => "../..", 'overwrite' => 1, 'level' => 1));
// Add entire htdocs directory and all subdirectories
// Add all php files in htsdocs and its subdirectories
$test->add_files(array("htdocs", "htsdocs/*.php"));
// Exclude all jpg files in htdocs and its subdirectories
// Create /var/www/htdocs/test/test.tgz
// Check for errors (you can check for errors at any point)
if (count($test->errors) > 0)
    print ("Errors occurred."); // Process errors here

The following example creates a zip file:
// Create new zip file in the directory below the current one
$test = new zip_file("../");
// All files added will be relative to the directory in which the script is
// executing since no basedir is set.
// Create archive in memory
// Do not recurse through subdirectories
// Do not store file paths in archive
$test->set_options(array('inmemory' => 1, 'recurse' => 0, 'storepaths' => 0));
// Add lib/archive.php to archive
// Add all jpegs and gifs in the images directory to archive
$test->add_files(array("images/*.jp*g", "images/*.gif"));
// Store all exe files in bin without compression
// Create archive in memory
// Send archive to user for download

Examples of Decompression:

The following example extracts a bzipped tar file:
// Open test.tbz2
$test = new bzip_file("test.tbz2");
// Overwrite existing files
$test->set_options(array('overwrite' => 1));
// Extract contents of archive to disk

The following example extracts a tar file:
// Open archives/test.tar
$test = new tar_file("archives/test.tar");
// Extract in memory
$test->set_options(array('inmemory' => 0));
// Extract archive to memory
// Write out the name and size of each file extracted
foreach ($test->files as $file)
    print ("File " + $file['name'] + " is " + $file['stat'][7] + " bytes\n");


TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES 2.1 By Devin Doucette Copyright (c) 2005 Devin Doucette Email: Requirements: PHP 4 or greater (there is a chance tar and zip archives will work with PHP 3) Compiled using --with-bz2 for bzip2 support. Compiled using --with-zlib for gzip and zip support. (Zip archives created using method 0 do not require zlib) Features: - Can create tar, gzip, bzip2, and zip archives. - Can create self-extracting zip archives. - Can recurse through and store directories. - Can create archives in memory or on disk. - Can allow client to download file directly from memory. - Errors are placed in an array named "errors" in the object. - Supports in zip file comments. - Files are automatically sorted within archive for greater compression in gzip and bzip2 archives. - Supports archiving and extraction of symbolic links (not applicable for zip files). Note: Bzip2 and gzip archives are always created as tar files and then compressed, so the recommended file extensions are .tbz/.tbz2 and .tgz respectively. Limitations: - Currently only USTAR archives are supported for tar file extraction. - Only bzip2 and gzip files that contain a tar file can be extracted. (Opening these files otherwise is already supported by PHP) - Cannot extract zip files. (This feature is already supported by PHP) Extraction of bzip2 and gzip archives is limited to compatible tar files that have been compressed by either bzip2 or gzip. For greater support, use the functions bzopen and gzopen respectively for bzip2 and gzip extraction. Zip extraction is not supported due to the wide variety of algorithms that may be used for compression and newer features such as encryption. If you need to extract zip files, use the functions detailed at Downloading Files: The download_file function only works for files that are stored in memory. To have users download files that are on disk redirect to the file, or use the following method: - Send the appropriate Content-Type header for the file being sent. - Send a "Content-Disposition: attachment; filename=[insert filename]" header. - Write out the file contents. Usage: For tar use tar_file (eg. $example = new tar_file("example.tar");) For gzip use gzip_file (eg. $example = new gzip_file("example.tgz");) For bzip2 use bzip_file (eg. $example = new bzip_file("example.tbz");) For zip use zip_file (eg. $example = new zip_file("");) Options: To set options, send an array containing the options that you wish to set to the function set_options. (eg. $example->setoptions($options);) The options array can include any of the following: basedir (default ".") Sets the that all filenames are taken as being relative to (except optional sfx header). Used both when creating and when extracting (will extract to basedir if inmemory == 0). name (no default) The name (and path, if necessary) of the archive, relative to basedir. Should be set when creating object (eg. $example = new zip_file("test/");). prepend (no default) The path that is added to the beginning of every filename in the archive. Example: If prepend is set to "src/zlib/" then the file "docs/readme.txt" will be stored in the archive as "src/zlib/docs/readme.txt". inmemory (default 0) Set to 1 to create/extract archive in memory, set to 0 to write to disk. overwrite (default 0) Set to 1 to overwrite existing files when creating/extracting archives. If set to 0, will give error message if file already exists. recurse (default 1) Set to 1 to recurse through subdirectories, 0 to not recurse. storepaths (default 1) Set to 1 to store paths in the archive, 0 to strip paths from the filenames. followlinks (default 0) Set to 1 to store the file that the symbolic link points to, 0 to store the link itself. level (default 3, zip and gzip only) [1-9] Level of compression for zip and gzip files, 0 is none. method (default 1, zip only) Set to 1 to compress files in the zip archive, 0 to store files only (no compression). sfx (no default, zip only) Filename of a valid sfx header for a zip archive, NOT relative to basedir. SFX capability is added to a zip file by simply prepending a valid executable, so this options takes the path of such a file. An example of a valid file for this would be "Zip.SFX" that is included in WinRAR. comment (no default) The comment added to a zip archive; may be used to set options for some sfx modules. Example options array: $options = array('basedir' => "../example", 'overwrite' => 1); Adding Files and Directories: To add files use the add_files function, which takes either an array or a single file/path. The * character can be used but be careful, as it is the equivalent of placing .* in a regular expression. Examples: $example->add_files("htdocs"); $example->add_files(array("test.php", "htdocs/*.txt")); $example->add_files("../*.gif"); To exclude files or directories from the archive use the exclude_files function, which works the same as the add_files function, except it excludes any files or directories would otherwise be added to the archive. (eg. $example->exclude_files("*.html");) To store files without compression (zip only), use the store_files function. This function works the same as add_files and exclude_files. (eg. $example->store_files("htdocs/test.txt");) Creating and Extracting Archives: To create an archive, call the create_archive function. (eg. $example->create_archive();) The archive created is the one passed when creating the object. If the file is downloaded, the filename sent for the download is the name passed when creating the object. To extract an archive, call the extract_files function. (eg. $example->extract_files();) The archive extracted is the one passed when creating the object. If the file is extracted to memory, the file information is located in an array called files (eg. $example->files) The structure of the array into which files are extracted in memory is as follows: $files = array( 'name'=>filename, 'stat'=>array( 2=>mode 4=>uid 5=>gid 7=>size 9=>mtime), 'type'=>0 for file, 2 for symbolic link, 5 for directory, 'data'=>file contents); Errors: Any errors that occur during any process will be logged in the errors array (eg. $example->errors). If any serious errors occur they will report errors as usual; only errors directly related to the creation or extraction of the archive will be suppressed any stored in the errors array.

  Files folder image Files  
File Role Description
Plain text file archive.php Class Contains the complete script.
Accessible without login Plain text file changelog Doc. Change Log
Accessible without login Plain text file examples Example Examples
Accessible without login Plain text file readme Doc. Readme

 Version Control Reuses Unique User Downloads Download Rankings  
This week:0
All time:18
This week:349Down
User Ratings User Comments (7)
 All time
For *tar.
9 years ago (Dovidl)
needs a lot of memory usage, does not work on freehoster wher...
13 years ago (Dan GeCk0)
Very usefull
13 years ago (Arvid de Jong)
not at all a good class i think.
14 years ago (Ridvan KARATAS)
Great script, this is just what I was looking for, has everyt...
14 years ago (Ivo)
Some function documentation would be more usefull but very go...
15 years ago (Stash X)
15 years ago (fizikopat)