PHP Classes

Zip: Create archives of compressed files in ZIP format

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (26)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 66%Total: 5,901 All time: 392 This week: 89Up
Version License PHP version Categories
zip 1.64GNU Lesser Genera...5.2PHP 5, Files and Folders, Compression
Description 

Author

This class can be used to create archives of compressed files in ZIP format.

It is based on the Create Zip File class originally written by Rochak Chauhan.

This version writes the compressed archive to a file instead of a variable when the archive size exceeds a given memory usage threshold.

It can also add comments to the ZIP archives.

Picture of Asbjorn Grandt
Name: Asbjorn Grandt <contact>
Classes: 10 packages by
Country: Denmark Denmark
Age: 52
All time rank: 1711 in Denmark Denmark
Week rank: 264 Down2 in Denmark Denmark Down
Innovation award
Innovation award
Nominee: 4x

Example

<?php
// Example. Zip all .html files in the current directory and send the file for Download.
// Also adds a static text "Hello World!" to the file Hello.txt
$fileDir = './';
ob_start(); // This is only to show that ob_start can be called, however the buffer must be empty when sending.

include_once("Zip.php");
$fileTime = date("D, d M Y H:i:s T");

$zip = new Zip();
// Archive comments don't really support utf-8. Some tools detect and read it though.
$zip->setComment("Example Zip file.\nАрхив Комментарий\nCreated on " . date('l jS \of F Y h:i:s A'));
// A bit of russian (I hope), to test UTF-8 file names.
$zip->addFile("Привет мир!", "Кириллица имя файла.txt");
$zip->addFile("Привет мир!", "Привет мир. С комментарий к файлу.txt", 0, "Кириллица файл комментарий");
$zip->addFile("Hello World!", "hello.txt");

@
$handle = opendir($fileDir);
if (
$handle) {
   
/* This is the correct way to loop over the directory. */
   
while (false !== ($file = readdir($handle))) {
        if (
strpos($file, ".php") !== false) {
           
$pathData = pathinfo($fileDir . $file);
           
$fileName = $pathData['filename'];

           
$zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file), NULL, TRUE, Zip::getFileExtAttr($file));
        }
    }
}

// Add a directory, first recursively, then the same directory, but without recursion.
// Naturally this requires you to change the path to ../test to point to a directory of your own.
// $zip->addDirectoryContent("testData/test", "recursiveDir/test");
// $zip->addDirectoryContent("testData/test", "recursiveDir/testFlat", FALSE);

//$zip->sendZip("ZipExample1.zip");
//$zip->sendZip("ZipExample1_€2,000.zip");
$zip->sendZip("ZipExample1_€2,000.zip", "application/zip", "ZipExample1_€2,000_utf8.zip");
?>


Details

A pair of PHP classes to generate zip files. The projects that started these two classes are hosted on PHPClasses.org at the addresses: Zip : http://www.phpclasses.org/package/6110 ZipStream: http://www.phpclasses.org/package/6616 ***************************************************************************************************************** WARNING: THE CURRENT VERSION OF PHPZip *WILL* FAIL IF THE SERVER HAS mbstring.func_overload INSTALLED AND ACTIVE! ***************************************************************************************************************** Zip.php generates the Zip file in memory (or tempfile) allowing the parent script to save the final Zip file elsewhere, and/or send it to the user. ZipStream has much of the same features and functions of Zip.php, with a few notable differences, it does not cache and build the zip file on the server, instead it'll send the file to the user as a stream. See the examples for example usage. The php files have "some" doumentation in them in the form of Javadoc style function headers. NOTE: Please ensure that output buffering is disabled when using especially ZipStream. It defeats the purpose of the class, and large zip files may cause a memory exceeded exception. NOTE2: THe Zip and ZipStream classes support UTF-8 in file paths and file comments, and will autodetect UTF-8 strings to that end, however it is up to the user to ensure that other Multibyte chracter sets aren't sent to the class. TODO: * Add compression level (at least "compress or store") to ZipStream, and to the Large file option on Zip.php * Documentation, no one reads it, but everyone complains if it is missing. * Better examples to fully cover the capabilities of the Zip classes. * more TODO's.

  Files folder image Files  
File Role Description
Plain text file Zip.php Class Zip class
Accessible without login Plain text file Zip.Example1.php Example Example file for generating a download file.
Plain text file Zip.Example1a.php Example Example file for generating a download file, using the new sendZip method.
Accessible without login Plain text file Zip.Example2.php Example Example file for generating a zip file and save it on the server.
Plain text file Zip.Example3 Example Example showing the stream function on Zip.php
Plain text file README Data Auxiliary data
Plain text file Zip.Example3.php Example Step version to 1.62 Allow customizing temporary file names Let's allow users to customize temporary files, either by setting Zip::$temp to a custom temporary folder or by setting Zip::$temp to a callable that returns a custom temporary file name.

 Version Control Reuses Unique User Downloads Download Rankings  
 71%1
Total:5,901
This week:0
All time:392
This week:89Up
User Ratings User Comments (10)
 All time
Utility:96%StarStarStarStarStar
Consistency:88%StarStarStarStarStar
Documentation:-
Examples:92%StarStarStarStarStar
Tests:-
Videos:-
Overall:66%StarStarStarStar
Rank:557
 
good one saved my lots of time
7 years ago (Hardik IIHGlobal)
70%StarStarStarStar
good one saved my lots of time
7 years ago (Hardik IIHGlobal)
70%StarStarStarStar
good one saved my lots of time
7 years ago (Hardik IIHGlobal)
70%StarStarStarStar
good one saved my lots of time
7 years ago (Hardik IIHGlobal)
70%StarStarStarStar
good one saved my lots of time
7 years ago (Hardik IIHGlobal)
70%StarStarStarStar
good one saved my lots of time
7 years ago (Hardik IIHGlobal)
70%StarStarStarStar
muy buena clase!, en solo 2 minutos ya estaba funcionando!.
11 years ago (SR Soluciones)
70%StarStarStarStar
Finally I found it, thank you.
12 years ago (jaume mila)
70%StarStarStarStar
really good class
13 years ago (gabriel souza)
70%StarStarStarStar
really useful with big files (250 mb).
13 years ago (Francisco Velazquez)
65%StarStarStarStar