Ne-Lexa / php-zip

PhpZip is a php-library for extended work with ZIP-archives.
MIT License
491 stars 60 forks source link

Problem to zip folder with subfolder above 1gb #63

Closed debosset closed 4 years ago

debosset commented 4 years ago
Q A
Library version(s) affected: current
PHP version(s): 7.3
OS (with bit depth):

my serveur limitation is

300 secondes execution time 1280Mo memory limit

Description
I found this library because i was using the default ziparchive and getting trouble when i have file or folders containing more than 1gb.

When user upload is folder or files, i create a unique folder name in a tmp on the server.

Ex: mgi-xxxxxxxxxxxxxxxxxxxx

Then file / folder are uploaded one by one

So basically i have

/tmp/mgi-xxxxxxxxx1

What i have is a function when i want to generate my zip where i now pass the tmp path with the correct unique name and the destination path


Is it possible to do it with php-zip ?

function zipWithPHPZip($tmp, $destination, $nameZip){ $zip = new \ZipStream($nameZip);

==> i'm lost here :(

$zip->finalize(); exit; }

Thanks in advance for your help

Ne-Lexa commented 4 years ago

Hello.

Try

$zipFile = new \PhpZip\ZipFile();
try{
    $zipFile->addDirRecursive($dirName);
    $zipFile->saveAsFile($zipFilename);
}
catch(\PhpZip\Exception\ZipException $e){
    // handle exception
}
finally{
    $zipFile->close();
}
debosset commented 4 years ago

Hi thanks for your answer i have tried to modify my function 👍

function generateZip(){ $upload_dir = wp_upload_dir(); // zip file name $tmpName = $_POST['tmpName']; // tmp folder where folder + files have been saved $tmpFolder = $upload_dir['basedir'].'/telechargements/tmp/'.$tmpName.'/'; // Desination zip name $destinationZip = $upload_dir['basedir'].'/telechargements/zip/'.$tmpName.'.zip'; // Generate the zip file $zipFile = new \PhpZip\ZipFile(); try{ $zipFile->addDirRecursive($tmpFolder); $zipFile->saveAsFile($destinationZip); } catch(\PhpZip\Exception\ZipException $e){ // handle exception } finally{ // Generation done $zipFile->close(); // Remove tmp dir rrmdir($tmpFolder); // Create poste type download with button to download zip with header createTelechargement($tmpName); } // Die when don't need anymore wp_die(); }

It works it generate a zip with 4gb of data :)

Thanks a lot, best lib ever !!! Great work !!!