PHP Classes

How can I zip specific folder ?

Recommend this page to a friend!

      Zip  >  All threads  >  How can I zip specific folder ?  >  (Un) Subscribe thread alerts  
Subject:How can I zip specific folder ?
Summary:Modified the script to get specific folder contents
Author:Sumith Harshan
Date:2012-10-16 15:54:43
Update:2012-10-16 18:23:48

  1. How can I zip specific folder ?   Reply   Report abuse  
Sumith Harshan - 2012-10-16 15:54:43
I modified the script as follows.

$zip = new Zip();

$fileDirectory = 'Folder_name_to_zip/';

foreach (new DirectoryIterator($fileDirectory) as $file) {
if($file->isDot()) continue;
$zip->addFile( file_get_contents($fileDirectory.$file->getFilename(),$fileDirectory),$file->getFilename() );


More info:

  2. Re: How can I zip specific folder ?   Reply   Report abuse  
Asbjorn Grandt - 2012-10-16 18:23:48 - In reply to message 1 from Sumith Harshan
First, you should avoid ob_start()

Next, have you tried the addDirectoryContent function in Zip?

$zip->addDirectoryContent($realPath, $zipPath, $recursive, $followSymlinks, &$addedFiles);

Only the first two arguments are required.

So from your example I'd say the command is:
$zip->addDirectoryContent($fileDirectory, "");


String $realPath Path on the file system.

String $zipPath Filepath and name to be used in the archive.

bool $recursive Add content recursively, default is TRUE.

bool $followSymlinks Follow and add symbolic links, if they are accessible, default is TRUE.

array &$addedFiles Reference to the added files, this is used to prevent duplicates, default is an empty array.
If you start the function by parsing an array, the array will be populated with the realPath and zipPath kay/value pairs added to the archive by the function.

  3. Re: How can I zip specific folder ?   Reply   Report abuse  
dang thi ngoc - 2014-11-12 11:08:39 - In reply to message 2 from Asbjorn Grandt
Hi Asbjorn Grandt,
i can zip Directory as your way.
But i doesn't support utf8 file names as $zip->addFile.
i need to zip folder include some Chinese File.
could you help me?