如何在 PHP 中[递归地]压缩目录?
目录类似于:
home/
file1.html
file2.html
Another_Dir/
file8.html
Sub_Dir/
file19.html
我正在使用 PHPMyAdmin http://trac.seagullproject.org/browser/branches/0.6-bugfix/lib/other/Zip.php 。我不确定如何压缩目录而不仅仅是文件。这是我到目前为止所得到的:
$aFiles = $this->da->getDirTree($target);
/* $aFiles is something like, path => filetime
Array
(
[home] =>
[home/file1.html] => 1251280379
[home/file2.html] => 1251280377
etc...
)
*/
$zip = & new Zip();
foreach( $aFiles as $fileLocation => $time ){
$file = $target . "/" . $fileLocation;
if ( is_file($file) ){
$buffer = file_get_contents($file);
$zip->addFile($buffer, $fileLocation);
}
}
THEN_SOME_PHP_CLASS::toDownloadData($zip); // this bit works ok
但是当我尝试解压缩相应的下载的 zip 文件时,我收到“不允许操作”
只有当我尝试在我的 mac 上解压缩时才会发生此错误,当我通过命令行解压缩时,文件解压正常。我是否需要在下载时发送特定的内容类型,目前为“application/zip”
Directory is something like:
home/
file1.html
file2.html
Another_Dir/
file8.html
Sub_Dir/
file19.html
I am using the same PHP Zip class used in PHPMyAdmin http://trac.seagullproject.org/browser/branches/0.6-bugfix/lib/other/Zip.php . I'm not sure how to zip a directory rather than just a file. Here's what I have so far:
$aFiles = $this->da->getDirTree($target);
/* $aFiles is something like, path => filetime
Array
(
[home] =>
[home/file1.html] => 1251280379
[home/file2.html] => 1251280377
etc...
)
*/
$zip = & new Zip();
foreach( $aFiles as $fileLocation => $time ){
$file = $target . "/" . $fileLocation;
if ( is_file($file) ){
$buffer = file_get_contents($file);
$zip->addFile($buffer, $fileLocation);
}
}
THEN_SOME_PHP_CLASS::toDownloadData($zip); // this bit works ok
but when I try to unzip the corresponding downloaded zip file I get "operation not permitted"
This error only happens when I try to unzip on my mac, when I unzip through the command line the file unzips ok. Do I need to send a specific content type on download, currently 'application/zip'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这是一个简单的函数,可以递归压缩任何文件或目录,只需要加载 zip 扩展名。
像这样称呼它:
Here is a simple function that can compress any file or directory recursively, only needs the zip extension to be loaded.
Call it like this:
另一种递归目录树归档,作为 ZipArchive 的扩展实现。作为奖励,还包括单语句树压缩辅助函数。与其他 ZipArchive 函数一样,支持可选的本地名称。待添加错误处理代码...
Yet another recursive directory tree archiving, implemented as an extension to ZipArchive. As a bonus, a single-statement tree compression helper function is included. Optional localname is supported, as in other ZipArchive functions. Error handling code to be added...
当将第三个参数设置为
时,我编辑了 Alix Axel 的答案以采用第三个参数true
所有文件都将添加到主目录下,而不是直接添加到 zip 文件夹中。如果 zip 文件存在,该文件也将被删除。
示例:
第三个参数
true
zip 结构:第三个参数
false
或缺少 zip 结构:编辑后的代码:
I've edited Alix Axel's answer to take a third argrument, when setting this third argrument to
true
all the files will be added under the main directory rather than directly in the zip folder.If the zip file exists the file will be deleted as well.
Example:
Third argrument
true
zip structure:Third argrument
false
or missing zip structure:Edited code:
用法: thisfile.php?dir=./path/to/folder (压缩后,它也会开始下载:)
USAGE: thisfile.php?dir=./path/to/folder (After zipping, it starts download too:)
尝试此链接 <-- 更多源代码在这里
Try this link <-- MORE SOURCE CODE HERE
这是我的代码,用于 Zip 文件夹及其子文件夹及其文件,并使其可以 zip 格式下载,
如果代码有任何问题,请告诉我。
Here Is my code For Zip the folders and its sub folders and its files and make it downloadable in zip Format
If Any Issue With the Code Let Me know.
我需要在 Mac OSX 中运行这个 Zip 函数,
这样我总是会压缩那个烦人的 .DS_Store。
我通过包含额外的忽略文件来改编https://stackoverflow.com/users/2019515/user2019515。
因此,要忽略 zip 中的 .DS_Store,请运行
zipIt('/path/to/folder', '/path/to/compressed.zip', false, array('.DS_Store'));
I needed to run this Zip function in Mac OSX
so I would always zip that annoying .DS_Store.
I adapted https://stackoverflow.com/users/2019515/user2019515 by including additionalIgnore files.
SO to ignore the .DS_Store from zip, you run
zipIt('/path/to/folder', '/path/to/compressed.zip', false, array('.DS_Store'));
很好的解决方案,但对于我的 Windows,我需要进行修改。修改代码如下
Great solution but for my Windows I need make a modifications. Below the modify code
此代码适用于 Windows 和 Linux。
This code works for both windows and linux.
这是我基于 Alix 的版本,适用于 Windows,希望也适用于 *nix:
Here's my version base on Alix's, works on Windows and hopefully on *nix too:
这是一个简单、易于阅读、运行良好的递归函数:
Here is the simple, easy to read, recursive function that works very well:
根据 @user2019515 的回答,我需要处理我的存档的排除项。这是带有示例的结果函数。
Zip 功能:
如何使用:
Following @user2019515 answer, I needed to handle exclusions to my archive. here is the resulting function with an example.
Zip Function :
How to use it :