ZipArchive - zip 没有被创建,但没有抛出错误

ZipArchive - zip is not getting created, but throws no errors

我正在努力使用 ZipArchive 创建 zip。我修改了一个函数,让我从一组文件路径创建一个 zip。

您能发现此代码中的任何错误吗?是否有任何阻止创建 zip 文件的错误?

// Function to create a zip archive for the low res images
function create_zip_lres($files = array(), $destination = '', $overwrite = false) {
    // If the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }

    $valid_files = array();

    // If files were passed in
    if(is_array($files)) {
        foreach($files as $file) {
            // Make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }

    // If we have good files
    if(count($valid_files)) {

        // Create the archive
        $zip = new ZipArchive();

        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }

        // Add the files
        foreach($valid_files as $file) {
            $basename = basename($file);
            $zip->addFile($file, $basename);
        }

        // DEBUG
        echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

        if ($zip->close()) {
            echo ' - Success!';
            echo '<br>';
            echo 'Your zip path is: ' . $destination;
        } else {
            echo ' - Failed!';
        }

        // DEBUG
        echo '<br>';
        print_r($zip);

        // Return filepath for zip
        return $destination;
    }
    else {
        return false;
    }
}



// getting the images array
$resize_images = resize_images();
// destination path
$lres_directory = get_lres_full_upload_dir() . get_the_title() . '-low-res.zip';

if (function_exists('create_zip_lres')) {
    create_zip_lres($resize_images, $lres_directory);
} else {

}

我已经添加了一些调试行来找出发生了什么,虽然它告诉我一切看起来都很好,但没有创建文件。

The zip archive contains 3 files with a status of 0 - Success!
Your zip path is: http://media.mysite.com/wp-content/uploads/lres-download-manager-files/Knus 1400-low-res.zip
ZipArchive Object ( [status] => 0 [statusSys] => 0 [numFiles] => 0 [filename] => [comment] => )

正在将多个文件压缩到一个 zip 文件中。

对于多个文件,您可以使用此代码压缩特定文件夹。

我试过这段代码并成功地将多个文件压缩到一个 zip 文件中。

我认为这对您的要求有所帮助。

<?php

function Zip($source, $destination)
{

echo "called function";
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }

    $zip = new ZipArchive();
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
        return false;
    }

    $source = str_replace('\', '/', realpath($source));

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

        foreach ($files as $file)
        {
            $file = str_replace('\', '/', $file);

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

            $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();
}

Zip('D:/tmp','D:/tmp/compressed.zip',true);


?> 

希望对您有所帮助

我有另一个动态数组值输入到 zip 的解决方案 files.I 已经测试它工作正常。

希望对您有所帮助:

<?php
//Our Dynamic array
$array = array( "name" => "raaaaaa",
    "test" => "/sites/chessboard.jpg"
);

//After zip files are stored in this folder
$file= "/sites/master.zip";

$zip = new ZipArchive;
echo "zip started.\n";
if ($zip->open($file, ZipArchive::CREATE) === TRUE) {
    foreach($array as $path){
        $zip->addFile($path, basename($path));
        echo "$path was added.\n";
    }
    $zip->close();
    echo 'done';
} else {
    echo 'failed';
}

?>