首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用PHP压缩整个文件夹

如何使用PHP压缩整个文件夹
EN

Stack Overflow用户
提问于 2011-02-07 00:59:35
回答 13查看 259.9K关注 0票数 154

我在stackoveflow找到了一些关于如何压缩特定文件的代码,但是如何压缩特定的文件夹呢?

Folder/
  index.html
  picture.jpg
  important.txt

My Folder内部,有一些文件。在压缩My Folder之后,我还想删除文件夹中除important.txt之外的所有内容。

stack找到了这个

我需要你的帮助。谢谢。

EN

回答 13

Stack Overflow用户

回答已采纳

发布于 2011-02-07 01:10:19

代码更新2015/04/22。

压缩整个文件夹:

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

压缩整个文件夹+删除除“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);
}
票数 372
EN

Stack Overflow用户

发布于 2011-02-07 01:25:41

在ZipArchive类中有一个有用的未记录的方法: addGlob();

$zipFile = "./testZip.zip";
$zipArchive = new ZipArchive();

if ($zipArchive->open($zipFile, (ZipArchive::CREATE | ZipArchive::OVERWRITE)) !== true)
    die("Failed to create archive\n");

$zipArchive->addGlob("./*.txt");
if ($zipArchive->status != ZIPARCHIVE::ER_OK)
    echo "Failed to write files to zip\n";

$zipArchive->close();

现在文档记录在:www.php.net/manual/en/ziparchive.addglob.php

票数 57
EN

Stack Overflow用户

发布于 2011-02-07 01:06:26

我假设这是在服务器上运行的,其中zip应用程序位于搜索路径中。对于所有基于unix的服务器,我想大多数基于windows的服务器都应该是这样的。

exec('zip -r archive.zip "My folder"');
unlink('My\ folder/index.html');
unlink('My\ folder/picture.jpg');

之后,归档文件将驻留在archive.zip中。请记住,文件或文件夹名称中的空格是导致错误的常见原因,应尽可能避免。

票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4914750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档