9
A
回答
14
下面是一个例子:
<?php
// Adding files to a .zip file, no zip file exists it creates a new ZIP file
// increase script timeout value
ini_set('max_execution_time', 5000);
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open('my-archive.zip', ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("themes/"));
// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
// close and save archive
$zip->close();
echo "Archive created successfully.";
?>
2
PHP自带ZipArchive扩展,这是为你的权利。
3
当心阿德南的例子可能出现的问题:如果目标myarchive.zip是源文件夹中,那么你需要排除的循环,或在创建存档文件之前运行迭代器(如果它不存在)。这是一个修改后的脚本,它使用后一个选项,并将一些配置变量添加到顶部。不应该使用这个添加到现有的存档。
<?php
// Config Vars
$sourcefolder = "./" ; // Default: "./"
$zipfilename = "myarchive.zip"; // Default: "myarchive.zip"
$timeout = 5000 ; // Default: 5000
// instantate an iterator (before creating the zip archive, just
// in case the zip file is created inside the source folder)
// and traverse the directory to get the file list.
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);
// set script timeout value
ini_set('max_execution_time', $timeout);
// instantate object
$zip = new ZipArchive();
// create and open the archive
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
die ("Could not open archive");
}
// add each file in the file list to the archive
foreach ($filelist as $key=>$value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
// close the archive
$zip->close();
echo "Archive ". $zipfilename . " created successfully.";
// And provide download link ?>
<a href="http:<?php echo $zipfilename;?>" target="_blank">
Download <?php echo $zipfilename?></a>
+0
它不解压缩。在解压缩时显示错误。 – 2015-10-21 17:25:33
相关问题
- 1. 使用Powershell脚本压缩文件夹
- 2. Node.js - 压缩/解压缩文件夹
- 3. 压缩/压缩文件夹,同时排除特定文件夹
- 4. 如何使用rubyzip解压缩压缩文件夹
- 5. 使用qt压缩文件夹/文件
- 6. 如何将未压缩文件夹视为压缩文件?
- 7. 压缩/解压缩文件夹和文件
- 8. 压缩和解压缩文件和文件夹
- 9. Android - 解压缩文件夹?
- 10. Windows.Storage.Compression UnZip压缩文件夹
- 11. 解压缩文件用PHP
- 12. 使用powershell脚本解压缩文件夹。
- 13. PHP压缩文件夹没有循环
- 14. 如何使用ZipKit压缩文件夹?
- 15. 压缩解压缩的exe文件
- 16. 压缩未压缩的MSI文件
- 17. 解压缩压缩富文本字段
- 18. 解压缩(解压缩)由.net压缩(解压缩)的文件.net system.io.compression.gzipstream
- 19. javascript文本压缩/解压
- 20. 使用PHP压缩文件的目录
- 21. 与django_compressor压缩文件以gzip压缩
- 22. Magick ++压缩和解压缩文件
- 23. 解压缩压缩文件夹,ASP.NET MVC 5
- 24. 在C#中压缩和解压缩文件夹#
- 25. 从NSIS发送到压缩(压缩)文件夹
- 26. Windows压缩文件夹没有完全解压缩zip
- 27. 如何在asp.net框架4.0中压缩/解压缩文件夹
- 28. 创建一个压缩(或压缩)文件夹
- 29. 压缩文件
- 30. 后postrotate脚本后logrotate压缩文件
伟大的代码...我如何编辑这个ZIP压缩嵌套的文件夹,排除所有的文件夹导致它?我所需的文件夹位于“/var/www/vhosts/mysite.com/dev/wp-content/themes/mytheme/”。当我运行这个脚本时,我得到一个var的开始文件夹然后www然后vhosts然后mysite.com等我缺少什么? – 2013-04-11 15:51:44
我是否理解,代码也是通过“。”进行迭代的。和“..”目录里面的“主题/”?它看起来像这样会导致问题,当您尝试解压缩存档。 – Tamara 2015-06-12 16:52:14
此代码在解压文件时会导致问题。 – 2015-10-21 17:22:45