我使用此代码在Windows中创建zip文件
$plugin_address="D:/processmaker-3.2.1-x/apps/processmaker/htdocs/cakephp/plugins";
$rootPath = $plugin_address."/".$R;
$zipFileName = $rootPath.'.zip';
$zip = new ZipArchive();
$zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
if (!$file->isDir())
{
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
$filePath=str_replace("\\","/",$filePath);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
现在我想用这段代码在Ubuntu中提取这个文件
$zipAdress="/var/www/cakephp/plugins/backup/EstelamBasic.zip";
$plugin_address="/var/www/cakephp/plugins/EstelamBasic/";
$zip = new ZipArchive;
$res = $zip->open($zipAdress);
if ($res === TRUE) {
$zip->extractTo($plugin_address);
$zip->close();
}
此代码工作并解压缩zip文件,但不创建目录和子目录。此代码在文件名中设置目录名!
我在linux上使用这个函数用于拉链工作
$rootPath = "/var/www/cakephp/plugins/EstelamBasic";
$zipFileName = $rootPath.'.zip';
$zip = new ZipArchive();
$zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
addFolderToZip($rootPath."/", $zip, $zipdir = '');
$zip->close();
function addFolderToZip($dir, $zipArchive, $zipdir = ''){
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//Add the directory
if(!empty($zipdir)) $zipArchive->addEmptyDir($zipdir);
// Loop through all the files
while (($file = readdir($dh)) !== false) {
//If it's a folder, run the function again!
if(!is_file($dir . $file)){
// Skip parent and root directories
if( ($file !== ".") && ($file !== "..")){
addFolderToZip($dir . $file . "/", $zipArchive, $zipdir . $file . "/");
}
}else{
// Add the files
$zipArchive->addFile($dir . $file, $zipdir . $file);
}
}
}
}
}