zip主文件夹,内部有子文件夹

编程入门 行业动态 更新时间:2024-10-08 19:50:56
本文介绍了zip主文件夹,内部有子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have a folder with some files and sub folder inside. How im going to read the directory and zip the main folder?

例如:

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 / 文件夹内。

But what I get is it zip the whole C: folder. What I want is to only zip document inside the image/data/ folder.

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

How can I format the correct directory and it's subdirectories?

推荐答案

尝试一下。

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(); }

更多推荐

zip主文件夹,内部有子文件夹

本文发布于:2023-11-28 10:40:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主文   文件夹   zip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!