gpt4 book ai didi

php - Windows 上的 Zip 目录和 Linux 上的解压缩

转载 作者:可可西里 更新时间:2023-11-01 11:44:56 25 4
gpt4 key购买 nike

我使用这段代码在 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();

enter image description hereZip 文件是正确的...(上图)

现在我想用这段代码在 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 文件,但不创建目录和子目录。此代码在文件名中设置目录名!

(在windows中提取代码是正确的,创建目录和子目录并在目录中设置文件) enter image description here

最佳答案

我在 windows 上使用这个函数来压缩,在 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);

}
}
}
}
}

关于php - Windows 上的 Zip 目录和 Linux 上的解压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47845025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com