gpt4 book ai didi

php - 压缩主文件夹,里面有子文件夹

转载 作者:可可西里 更新时间:2023-11-01 13:50:32 27 4
gpt4 key购买 nike

我有一个文件夹,里面有一些文件和子文件夹。我将如何读取目录并压缩主文件夹?

例如:

maindirectory
--- file 1
--- file 2
--- subdirectory 1
------ file 3
------ file 4
--- subdirectory 2
------ file 5
------ file 6

我正在使用这个脚本:

function Zip($source, $destination, $include_dir = false)
{

if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}

if (file_exists($destination)) {
unlink ($destination);
}

$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));

if (is_dir($source) === true)
{

$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_CHILD);

if ($include_dir) {

$arr = explode("/",$source);
$maindir = $arr[count($arr)- 1];

$source = "";
for ($i=0; $i < count($arr) - 1; $i++) {
$source .= '/' . $arr[$i];
}

$source = substr($source, 1);

$zip->addEmptyDir($maindir);

}

foreach ($files as $file)
{
$file = str_replace('\\', '/', $file);

// Ignore "." and ".." folders
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;

$file = realpath($file);

if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}

return $zip->close();
}

我这样调用这个函数:

Zip('image/data/','aaa.zip',false);

但我得到的是它压缩了整个 C: 文件夹。我想要的是仅将文档压缩到 image/data/ 文件夹中。

如何格式化正确的目录及其子目录?

最佳答案

试试这个。

zipFile('image/data/','aaa.zip', true);

/**
* function zipFile. Creates a zip file from source to destination
*
* @param string $source Source path for zip
* @param string $destination Destination path for zip
* @param string|boolean $flag OPTIONAL If true includes the folder also
* @return boolean
*/
function zipFile($source, $destination, $flag = '')
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}

$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}

$source = str_replace('\\', '/', realpath($source));
if($flag)
{
$flag = basename($source) . '/';
//$zip->addEmptyDir(basename($source) . '/');
}

if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$file = str_replace('\\', '/', realpath($file));

if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $flag.$file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $flag.$file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString($flag.basename($source), file_get_contents($source));
}

return $zip->close();
}

关于php - 压缩主文件夹,里面有子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17584869/

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