PHP zip压缩留下未完成的zip文件


php ziparchive leave unfinished zip files

我试图用php创建三个zip包。我正在运行一个函数,该函数复制目录并查看内容,以将每个带有循环的文件添加到zip中。

问题是,ZipArchive似乎不关闭文件时完成。我最后想要得到的是三个像这样的zip文件:

Package1.zip
Package2.zip
Package3.zip

而不是像这样结束:

Package1.zip.a012341
Package1.zip.b012342
Package2.zip.a023451
Package2.zip.b023452
Package3.zip.a034561
Package3.zip.b034562

这就像zip文件没有关闭,最后删除临时扩展名,并试图为每个文件创建另一个扩展名,但也失败了。我没有得到任何错误,至少我所看到的没有。

我猜的是,我需要在php.ini或我的webserver/IIS中更改某种设置。我尝试创建的文件非常大,大约在450 - 600mb之间。

这是我最好的猜测,因为我有两个几乎相同的服务器设置,我已经尝试了相同的代码。它对其中一个很有效,但对另一个不起作用。这证明我的ZipArchive代码不能解决这里的问题(顺便说一句,我没有设置服务器,所以我不确定有什么不同)。

我已经尝试在php.ini中增加memory_limit和执行时间。后者是一个较早的问题,我收到一个消息告诉我它已超时。

提前感谢!

编辑:增加功能

function package_site($filepath, &$context) {
    $context['message'] = 'Creating package';
    // Mac app
    $mac_zipfile = 'public://site_export/mac_package.zip';
    copy_directory($mac_zipfile, $filepath . '/mac_package.zip', '/.*/');
    $files = file_scan_directory($filepath . '/files', '/.*/');
    $mac_zip = new ZipArchive();
    if($mac_zip->open(drupal_realpath($filepath . '/mac_package.zip')) === true) {
        foreach($files as $key => $value) {
            $mac_zip->addFile(drupal_realpath($value->uri), 'mac_package.app/Contents/Resources/' . substr(drupal_realpath($value->uri), strlen(drupal_realpath($filepath . '/files')) + 1));
        }
    } else {
        drupal_set_message("Could NOT open mac_package.zip");
    }
    $mac_zip->close();
    // PC app
    drupal_unlink($filepath . '/package.zip');
    $start_files_path_pc = 'public://site_export/pc_files';
    copy_directory($start_files_path_pc, $filepath . '/files', '/.*/');
    $pc_files = file_scan_directory($filepath . '/files', '/.*/');
    $zip = new ZipArchive();
    $res = $zip->open(drupal_realpath($filepath . '/package.zip'), ZipArchive::CREATE);
    if ($res === TRUE) {
        foreach ($pc_files as $key => $value) {
            $zip->addFile(drupal_realpath($value->uri), substr(drupal_realpath($value->uri), strlen(drupal_realpath($filepath . '/files')) + 1));
        }
        $zip->close();
    }
    // Create a html package
    create_pure_html_package($filepath);
    // HTML package
    drupal_unlink($filepath . '/html.zip');
    $html_files = file_scan_directory($filepath . '/files/site', '/.*/');
    $zip_html = new ZipArchive();
    $res = $zip_html->open(drupal_realpath($filepath . '/html.zip'), ZipArchive::CREATE);
    if ($res === TRUE) {
        foreach ($html_files as $key => $value) {
            $zip_html->addFile(drupal_realpath($value->uri), substr(drupal_realpath($value->uri), strlen(drupal_realpath($filepath . '/files')) + 1));
        }
        $zip_html->addFile(drupal_realpath($filepath . '/start.html'), 'start.html');
        $zip_html->close();
    }
    rrmdir(drupal_realpath($filepath . '/files'));
    unlink(drupal_realpath($filepath . '/start.html'));
}

我刚才遇到了同样的问题。我通过更改文件目录权限(为非只读)解决了这个问题。这就是我的答案,我希望它能帮助你!