如何使用 PHP 压缩整个文件夹 [英] How to zip a whole folder using PHP
本文介绍了如何使用 PHP 压缩整个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 stackoveflow 上找到了一些关于如何压缩特定文件的代码,但是特定文件夹呢?
文件夹/索引.html图片.jpg重要.txt
在我的文件夹
里面,有文件.压缩My Folder
后,我还想删除文件夹中除important.txt
之外的全部内容.
在 stack 中找到了这个>
我需要你的帮助.谢谢.
解决方案
代码更新 2015/04/22.
压缩整个文件夹:
//获取我们文件夹的真实路径$rootPath = realpath('folder-to-zip');//初始化归档对象$zip = 新的 ZipArchive();$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);//创建递归目录迭代器/** @var SplFileInfo[] $files */$files = new RecursiveIteratorIterator(新的递归目录迭代器($rootPath),RecursiveIteratorIterator::LEAVES_ONLY);foreach ($files as $name => $file){//跳过目录(它们会被自动添加)if (!$file->isDir()){//获取当前文件的真实路径和相对路径$filePath = $file->getRealPath();$relativePath = substr($filePath, strlen($rootPath) + 1);//将当前文件添加到存档$zip->addFile($filePath, $relativePath);}}//只有在关闭对象后才会创建 Zip 存档$zip->close();
压缩整个文件夹+删除除important.txt"之外的所有文件:
//获取我们文件夹的真实路径$rootPath = realpath('folder-to-zip');//初始化归档对象$zip = 新的 ZipArchive();$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);//初始化空的删除列表"$filesToDelete = array();//创建递归目录迭代器/** @var SplFileInfo[] $files */$files = new RecursiveIteratorIterator(新的递归目录迭代器($rootPath),RecursiveIteratorIterator::LEAVES_ONLY);foreach ($files as $name => $file){//跳过目录(它们会被自动添加)if (!$file->isDir()){//获取当前文件的真实路径和相对路径$filePath = $file->getRealPath();$relativePath = substr($filePath, strlen($rootPath) + 1);//将当前文件添加到存档$zip->addFile($filePath, $relativePath);//将当前文件添加到删除列表"//稍后删除它会导致 ZipArchive 仅在调用 close 函数后创建存档,并且 ZipArchive 锁定文件直到创建存档)if ($file->getFilename() != 'important.txt'){$filesToDelete[] = $filePath;}}}//只有在关闭对象后才会创建 Zip 存档$zip->close();//从删除列表"中删除所有文件foreach ($filesToDelete as $file){取消链接($文件);}
I have found here at stackoveflow some codes on how to ZIP a specific file, but how about a specific folder?
Folder/
index.html
picture.jpg
important.txt
inside in My Folder
, there are files. after zipping the My Folder
, i also want to delete the whole content of the folder except important.txt
.
Found this here at stack
I need your help. thanks.
解决方案
Code updated 2015/04/22.
Zip a whole folder:
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
Zip a whole folder + delete all files except "important.txt":
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Initialize empty "delete list"
$filesToDelete = array();
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
// Add current file to "delete list"
// delete it later cause ZipArchive create archive only after calling close function and ZipArchive lock files until archive created)
if ($file->getFilename() != 'important.txt')
{
$filesToDelete[] = $filePath;
}
}
}
// Zip archive will be created only after closing object
$zip->close();
// Delete all files from "delete list"
foreach ($filesToDelete as $file)
{
unlink($file);
}
这篇关于如何使用 PHP 压缩整个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文