使用PHP将目录转换为Zip

使用PHP将目录转换为Zip,php,compression,filesystems,directory,zip,Php,Compression,Filesystems,Directory,Zip,如何使用PHP将目录转换为zip文件 谢谢。来自“” 这里有一个简单的函数,可以递归压缩任何文件或目录,只需要加载zip扩展名 function Zip($source, $destination) { if (extension_loaded('zip') === true) { if (file_exists($source) === true) { $zip = new ZipArchive();

如何使用PHP将目录转换为zip文件

谢谢。

来自“”


这里有一个简单的函数,可以递归压缩任何文件或目录,只需要加载zip扩展名

function Zip($source, $destination)
{
    if (extension_loaded('zip') === true)
    {
        if (file_exists($source) === true)
        {
            $zip = new ZipArchive();

            if ($zip->open($destination, ZIPARCHIVE::CREATE) === true)
            {
                $source = realpath($source);

                if (is_dir($source) === true)
                {
                    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

                    foreach ($files as $file)
                    {
                        $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();
        }
    }

    return false;
}
可以这样称呼:

Zip('/folder/to/compress/', './compressed.zip');

编辑-此文件夹不会保留文件夹结构(请查看我的评论):

function Zip($source, $destination)
{
    if (extension_loaded('zip') === true)
    {
        if (file_exists($source) === true)
        {
            $zip = new ZipArchive();

            if ($zip->open($destination, ZIPARCHIVE::CREATE) === true)
            {
                if (is_dir($source) === true)
                {
                    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

                    foreach ($files as $file)
                    {
                        if (is_file($file) === true)
                        {
                            $zip->addFromString(basename($file), file_get_contents($file));
                        }
                    }
                }

                else if (is_file($source) === true)
                {
                    $zip->addFromString(basename($source), file_get_contents($source));
                }
            }

            return $zip->close();
        }
    }

    return false;
}

这一条似乎短一些

<?php

$sourcePath = realpath('../../');

$archiv = new ZipArchive();
$archiv->open('archiv.zip', ZipArchive::CREATE);
$dirIter = new RecursiveDirectoryIterator($sourcePath);
$iter = new RecursiveIteratorIterator($dirIter);

foreach($iter as $element) {
    /* @var $element SplFileInfo */
    $dir = str_replace($sourcePath, '', $element->getPath()) . '/';
    if ($element->isDir()) {
        $archiv->addEmptyDir($dir);
    } elseif ($element->isFile()) {
        $file         = $element->getPath() .
                        '/' . $element->getFilename();
        $fileInArchiv = $dir . $element->getFilename();
        // add file to archive 
        $archiv->addFile($file, $fileInArchiv);
    }
}

// Save a comment
$archiv->setArchiveComment('Backup ' . $absolutePath);
// save and close 
$archiv->close();

// to extract

$destinationPath = realpath('tmp/');
$archiv = new ZipArchive();
$archiv->open('archiv.zip');
$archiv->extractTo($destinationPath);

对于windows服务器,您需要更改它,或者从C获得整个目录层次结构:

    foreach ($files as $file)
    {
        $currentfile = substr($file, strpos($file, "\\") + 1);
        $file = str_replace('\\', '/', $file);

        if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
            continue;

        $file = realpath($file);

        if (is_dir($file) === true)
        {
            $dirloc = $currentfile;
            $zip->addEmptyDir($dirloc); 
        }
        elseif(is_file($file) === true)
        {
            $fileloc = $currentfile;
            $zip->addFromString($fileloc,file_get_contents($file)); 
       }
    } 

这将保持目录结构。有没有办法不保留结构?所以所有文件都在一个文件夹中。@unknown(谷歌):不可靠,因为一个文件可以覆盖一个已经压缩的文件。@unknown(谷歌):我已将函数更新为您想要的内容。如果有人包含更大的压缩文件(或其他文件),我建议更改:$zip->addFromString(str_replace($source./'),'$file),file_get_contents($file)); --- 到--$zip->addFile($file,$file);--否则,此函数将自动失败。jp[]@Jeffz:使用
addFile()
方法也有其怪癖,请参见和。事实并非如此,这似乎是因为编码风格。闻起来像
请将代码发送给我这可以帮助你