For a future project I needed these days some easy to use zip or gzip class to create a zip file from files / folders inside a specified directory. A short search on Google has lead me to the Create ZIP File PHP class from Rochak Chauhan.
This class can create ZIP archives from lists of files.
The class provides means to add individual files or whole directories to the list of files packed into a ZIP archive.
The class can generate the packed archive as a string value.
The class can also output the necessary request response headers to serve the generated ZIP archive for download.
The supplied example demonstrates how to use the class to store the ZIP archive in a file, serve it for download and delete the file after it is served.
Example of usage
$createZip = new createZip;
$createZip -> addDirectory(“dir/”);
$fileContents = file_get_contents(“img.jpg”);
$createZip -> addFile($fileContents, “dir/img.jpg”);
$fileName = “archive.zip”;
$fd = fopen ($fileName, “wb”);
$out = fwrite ($fd, $createZip -> getZippedfile());
$createZip -> forceDownload($fileName);