Skip to content

Instantly share code, notes, and snippets.

@rtio
Created January 30, 2017 17:33
Show Gist options
  • Select an option

  • Save rtio/d913ca9080c96af7e8f0dd05c3a75e6c to your computer and use it in GitHub Desktop.

Select an option

Save rtio/d913ca9080c96af7e8f0dd05c3a75e6c to your computer and use it in GitHub Desktop.
/**
* Zip a folder (include itself).
* Usage:
* HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
*
* @param string $sourcePath Path of directory to be zip.
* @param string $outZipPath Path of output zip file.
*/
public static function zipDir($sourcePath, $outZipPath)
{
$pathInfo = pathInfo($sourcePath);
$parentPath = $pathInfo['dirname'];
$dirName = $pathInfo['basename'];
$z = new ZipArchive();
$z->open($outZipPath, ZIPARCHIVE::CREATE);
$z->addEmptyDir($dirName);
self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
$z->close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment