压缩内压缩 (php)

Zip inside zip (php)

大家好,这几天我一直在尝试解决一个问题,但没有得到任何回应,我并不擅长php,但我正在尽力而为。

我想自动下载,而不在服务器上保存一个名为 Bigzip 的 zip

在这个 Bigzip 中:-另一个名为 Smallzip 的 zip

但是我在打开下载的 zip 时出错,它 已损坏

<?php

//Big and Small Archives names
$BzipN='Bigzip.zip';
$SzipN='Smallzip.zip';

//Big and Small Archives
$Bzip = new ZipArchive();
$Szip = new ZipArchive();




//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';




zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path);

function zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path)
{


//create the file and throw the error if unsuccessful
if ($Bzip->open($BzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$BzipN>\n");
}
if ($Szip->open($SzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$SzipN>\n");
}



//Add Smallzip to BigZip
$Bzip->addFile($file_path.$SzipN,$Szip);


$Szip->close();
$Bzip->close();
//then send the headers to foce download the Big zip file
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$BzipN");
header("Content-length: " . filesize($BzipN));
header("Pragma: no-cache"); 
header("Expires: 0"); 
readfile("$BzipN");
exit;
}
?>

如果您有任何替代方案、想法和建议,我会很乐意采纳。

谢谢

如果您不想在服务器上保存文件,只需回显创建的文件即可。 index.php

<form action="test.php" method="post">
To file: <input type="text" name="tofile" />
<input type="submit" />
</form>

test.php

<?php
$filename = 'test-download.zip';
$htmlcode1 = "<HTML> \n <BODY>";
$htmlcode2 = "</BODY> \n <HTML>";
$somecontent = $htmlcode1.$_POST["tofile"].$htmlcode2;
!$handle = fopen($filename, 'w');
fwrite($handle, $somecontent);
fclose($handle);


header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary");

readfile($filename);

?>

第 1 期:
您不能在空的 zip 存档中创建空的 zip 存档。这会导致文件损坏。

第 2 期:
当您还没有关闭第一个 zip 存档时,不要尝试将一个 zip 存档添加到另一个 zip 存档。

第 3 期:
$Bzip->addFile($file_path.$SzipN,$Szip); 那么,您究竟为什么要尝试将该对象设置为您的文件名? => $Szip = new ZipArchive();

解法:

<?php

//Big and Small Archives names
$BzipN='Bigzip.zip';
$SzipN='Smallzip.zip';

//Big and Small Archives
$Bzip = new ZipArchive();
$Szip = new ZipArchive();

//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';


function zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path){
    // Create the file Smallzip.zip and throw the error if unsuccessful
    if ($Szip->open($SzipN, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$SzipN>\n");
    }

    // Add a txt file to Smallzip so it isn't empty
    $Szip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");

    // Close Smallzip.zip as we're done with it
    $Szip->close();

    // Create the file Bigzip.zip and throw the error if unsuccessful
    if ($Bzip->open($BzipN, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$BzipN>\n");
    }

    // Add Smallzip.zip to Bigzip.zip with a valid name
    $Bzip->addFile($file_path.$SzipN,$SzipN);

    // Close Bigzip.zip as we're done with it
    $Bzip->close();

    //then send the headers to foce download the Big zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$BzipN");
    header("Content-length: " . filesize($BzipN));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$BzipN");

    // Delete the files from the server, even if the user cancels the download
    ignore_user_abort(true);
    unlink($file_path.$SzipN);
    unlink($file_path.$SzipN);
    exit;
}

zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path);

?>