Spiga

How to Create a zip file with php on the fly

by Gabi Solomon

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

[php]

include_once("createZip.inc.php");
$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());
fclose ($fd);

$createZip -> forceDownload($fileName);
@unlink($fileName);
?>
[/php]

  • http://toolnext.com amit shah

    hello,
    i used the same flow for creating zip file that contains some files. but when extracting those, with winrar no error occurs. but with winzip the error comes is

    error [C:\Documents and Settings\Amit\Desktop\recharge.zip]: start of central directory not found; Zip file corrupt.
    Possible cause: file transfer error

  • http://www.gsdesign.ro/ Gabi Solomon

    @amit shah
    I dont know what to tell you .. i used it and it worked for me.

  • http://toolnext.com amit shah

    GABI:

    do u get any error while extracting it with winzip?

  • http://www.gsdesign.ro/ Gabi Solomon

    no it worked on the files i tested

  • http://toolnext.com amit shah

    if u can try to see what happens when u add mutiple file in a zip folder.

  • Tripp

    I tried this out and it works great with winzip. However, one of my buddy’s can’t open the zip archive on his Mac. Any ideas?

  • http://www.gsdesign.ro/ Gabi Solomon

    @tripp
    i think there might be an incompatibility with the mac, havent tested it out.

  • http://toolnext.com amit shah

    it’s working now. i just added
    ob_end_clean();
    before echo the zip file content.

  • http://www.sewsolutions.com PHP Expert
  • http://www.webaddict.info/ IT addict

    One more function to create zip:
    http://www.phpinform.com/2009/06/27/php-classfu

  • http://www.jasonstockton.com.au/ Jason Stockton

    Unfortunately like a lot of the PHP Zip classes this one doesn't unarchive on mac.

    For Mac friendly version you can try this one!

    http://thewebdevelopmentblog.com/2009/08/script

  • Fred Bloggs

    ob_end_clean();

    Worked a treat, files zipped ok without including directories but as soon as I added one I got the same problem as amit shah. 2 days of searching google and you have the solution, nice one amit shah.

    • Barki

      Great, Thank you very much , Its the right answer

  • tsardkyut

    thanks for the fixed solution given by amit shah really helps me a lot, …. :)

  • Ultrasahand

    many many many thanks
    —-==== BUT ====——
    the file(createZip.inc.php) by Rochak Chauhan has been changed …
    so here it goes :
    change line 3 to : include_once(“createZipFile.inc.php”);
    change line 4 to : $createZip = new CreateZipFile;
    and there you have it

  • Jay

    file_get_contents making it so it doesn't display realtime. it has to get contents and make it into zip before download (wait 1 min for page to load first) can you just stream the current get contents and download it realtime? :S

  • Rajeshvs_119

    how to get createZip.inc.php

    • Rajeshvs_119

      Please any body tell me how to download createZip.inc.php file

  • http://pulse.yahoo.com/_ECCH2TOIP3TS7GHJEIYGXVH7TY alex

    I keep getting a  ”ERROR: File not found.” message when I put the file on the server but if I tested it out locally it works fine. Anyone have any idea why that is? Not sure if its some setting I have to set on the server or ?