How to create a zip archive using PHP
The following is a library that allows you to generate zip file archives using php.
<?php
include('ziplib.php');
$zipfile = new Ziplib;
$zipfile->zl_add_file('This is a test file','path/to/file','g9');
//You can stream the ZIP file or write it in a file on your server
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename=\'testfile.zip\'');
echo $zipfile->zl_pack('zip file comments');
?>
include('ziplib.php');
$zipfile = new Ziplib;
$zipfile->zl_add_file('This is a test file','path/to/file','g9');
//You can stream the ZIP file or write it in a file on your server
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename=\'testfile.zip\'');
echo $zipfile->zl_pack('zip file comments');
?>
This script will dynamically create a zip archive using the files specified with zl_add_file and output it to the browser (the final zip file will be named: testfile.zip).
Options
zl_add_file allows you to specify the compression level of the file file that will be added to the archive.
- n (none)
- b (bzip)
- g (gzip)
Download
The php zip library can be downloaded Here
9 comments
can i use this code to achieve multiple files to a one zip file?
please. can u explain me how ?
I believe you can just run the following command for each file you want to include:
$zipfile->zl_add_file(’This is a test file’,'path/to/file’,'g9′);
James
Do not waste your time on this class. use the all powerful PHP bundled extension ZipArchive. it can do anything you want. very easily
@jibKAS:
You are right,this is a much faster extension that you can use to create zip archives. However, some hosting providers don’t have it installed. Also, if you are releasing a program and want to make it as painless as possible for your end user, you can use this library.
[...] zip????????????? [...]
it is possible to save zip file to some folder ?
@chirag,
You can output it to a file or send it to the browser.
@Roy ,
Noob question : how to write it to file ?
can i use this class to code to achieve some directories together with some files also. lets say we have a mixture of files and directories.
Leave a Comment