PHP ZipArchive不工作,没有错误

时间:2015-07-21 13:41:10

标签: php zip ziparchive

我试图压缩(非常大)一组文件,但没有任何反应,我似乎无法让任何错误返回。

以下每次都会返回一个错误:#6,最后。

每个文件都存在,它是可读的,ZipArchive :: addFile返回true,$ zipArchive close()s没有失败,但最终的zip文件没有被创建。

是什么给出了?

**如果重要的话,在Windows Server 2012 R2上作为本地SAN运行。

//exceptions, throws ErrorException
set_error_handler('exception_error');

try {
    // Copy directory to temporary location
    rcopy($src_location,$tmp_location);

    // Initialize archive object
    $zipArchive = new ZipArchive();
    $zipArchive->open($archive_filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);

    // Create recursive directory iterator
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($tmp_location),
        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($tmp_location) + 1);

            if (!file_exists($filePath)) {
                _log(1,'Uploading Archives [Compression Error #1]','File Not Found ['.$filePath.']');
                throw new Exception('Giving Up');
            }
            elseif (!is_readable($filePath)) {
                _log(1,'Uploading Archives [Compression Error #2]','File Not Readable ['.$filePath.']');
                throw new Exception('Giving Up');
            }
            else {
                // Add current file to archive
                if (!$zipArchive->addFile($filePath, $relativePath)) {
                    _log(1,'Uploading Archives [Compression Error #7]','File Not Added ['.$filePath.' | '.$relativePath.']');
                    throw new Exception('Giving Up');
                }
            }
        }
    }

    // Zip archive will be created only after closing object
    if (!$zipArchive->close()) {
        _log(1,'Uploading Archives [Compression Error #3]','Zip Not Created ['.$zipArchive->getStatusString().']');
    }
}
catch (ErrorException $e) {
    _log(1,'Uploading Archives [Copy/Compression Error #4]',(string)$e);
}
catch (Exception $e) {
    _log(1,'Uploading Archives [Copy/Compression Error #5]',(string)$e);
}
finally {
    // Clean Up Memory
    unset($files);
    unset($zipArchive);
}

// Final Check
if (!file_exists($archive_filename)) {
    _log(1,'Uploading Archives [Compression Error #6]','Error Creating Archive ['.$archive_filename.'] ('.$src_location.')');
}

0 个答案:

没有答案