Create a ZIP archive from a directory (and its subdirectories) with the option to exclude specific directories. This web-based tool utilizes PHP and the ZipArchive class to generate the ZIP archive.
function createZipFromDirectory ($sourceDir , $outputZipFile , $excludeDir = array ( ) )
{
$zip = new ZipArchive ();
if ($zip ->open ($outputZipFile , ZipArchive ::CREATE | ZipArchive ::OVERWRITE ) === true ) {
$dirIterator = new RecursiveIteratorIterator (
new RecursiveDirectoryIterator ($sourceDir ),
RecursiveIteratorIterator ::LEAVES_ONLY
);
foreach ($dirIterator as $file ) {
$relativePath = substr ($file ->getPathname (), strlen ($sourceDir ) + 1 );
if ($file ->isFile ()) {
$zip ->addFile ($file ->getPathname (), $relativePath );
} elseif ($file ->isDir ()) {
if (in_array ($file ->getFilename (), $excludeDir )) {
continue ;
}
$zip ->addEmptyDir ($relativePath );
}
}
$zip ->close ();
echo "ZIP archive created successfully at $outputZipFile " ;
} else {
echo "Failed to create ZIP archive at $outputZipFile " ;
}
}
$sourceDir = dirname (__DIR__ );
$outputZipFile = basename ($sourceDir ) . '_' . date ('Y-m-d_H-i-s' ) . '.zip' ;
$excludeDir = array ('node_modules' , 'vendor' , 'sumonst21-temp' );
createZipFromDirectory ($sourceDir , __DIR__ . '/' . $outputZipFile , $excludeDir );
Copy to Clipboard