// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
压缩整个文件夹 + 删除所有文件,除了“ impant.txt”:
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Initialize empty "delete list"
$filesToDelete = array();
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
// Add current file to "delete list"
// delete it later cause ZipArchive create archive only after calling close function and ZipArchive lock files until archive created)
if ($file->getFilename() != 'important.txt')
{
$filesToDelete[] = $filePath;
}
}
}
// Zip archive will be created only after closing object
$zip->close();
// Delete all files from "delete list"
foreach ($filesToDelete as $file)
{
unlink($file);
}
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
echo $path = "uploadpdf/".$file;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));---This is main function
}
else{
echo"file does not exist";
}
}
$zip->close();
class compressor {
/**
* public static $NOT_COMPRESS
* use: compressor::$NOT_COMPRESS
* no compress thoses files for upload
*/
public static $NOT_COMPRESS = array(
'error_log',
'cgi-bin',
'whatever/whatever'
);
/**
* end public static $NOT_COMPRESS
*/
/**
* public function compress_folder( $dir, $version, $archive_dest );
* @param {string} $dir | absolute path to the directory
* @param {string} $version_number | ex: 0.1.1
* @param {string} $archive_dest | absolute path to the future compressed file
* @return {void} DO A COMPRESSION OF A FOLDER
*/
public function compress_folder( $dir, $version, $archive_dest ){
// name of FUTURE .zip file
$archive_name = $version_number.'.zip';
// test dir exits
if( !is_dir($dir) ){ exit('No temp directory ...'); }
// Iterate and archive API DIRECTORIES AND FOLDERS
// create zip archive + manager
$zip = new ZipArchive;
$zip->open( $archive_dest,
ZipArchive::CREATE | ZipArchive::OVERWRITE );
// iterator / SKIP_DOTS -> ignore '..' and '.'
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $dir,
RecursiveDirectoryIterator::SKIP_DOTS )
);
// loop iterator
foreach( $it as $file ){
// check files not to add for compress
// loop list for not add to upload .zip
foreach( compressor::$NOT_COMPRESS as $k => $v) {
if( preg_match( '/^('.preg_quote($v,'/').')/', $it->getSubPathName() ) == true ){
// break this loop and parent loop
continue 2;
}
}
// end loop list
// for Test
// echo $it->getSubPathName()."\r\n";
// no need to check if is a DIRECTORY with $it->getSubPathName()
// DIRECTORIES are added automatically
$zip->addFile( $it->getPathname(), $it->getSubPathName() );
}
// end loop
$zip->close();
// END Iterate and archive API DIRECTORIES AND FOLDERS
}
/**
* public function compress_folder( $version_number );
*/
}
// end class compressor
用途:
// future name of the archive
$version = '0.0.1';
// path of directory to compress
$dir = $_SERVER['DOCUMENT_ROOT'].'/SOURCES';
// real path to FUTURE ARCHIVE
$archive_dest = $_SERVER['DOCUMENT_ROOT'].'/COMPRESSED/'.$version.'.zip';
$Compress = new compressor();
$Compress->compress_folder( $dir, $version, $archive_dest );
// this create a .zip file like :
$_SERVER['DOCUMENT_ROOT'].'/COMPRESSED/0.0.1.zip