From fbf6cbe091be3ff4fe1fb688e5b5ee8525afaed2 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 14 Aug 2019 03:43:15 +0200 Subject: [PATCH] FIX Option to use ZipArchive instead of PclZip bugged with large files. --- htdocs/core/lib/files.lib.php | 45 +++++++++++++++++++++++++++++++++++ htdocs/master.inc.php | 1 + 2 files changed, 46 insertions(+) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index 3ef82f9f3b6..5889c8270ca 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -1888,6 +1888,8 @@ function dol_convert_file($fileinput, $ext = 'png', $fileoutput = '', $page = '' */ function dol_compress_file($inputfile, $outputfile, $mode = "gz") { + global $conf; + $foundhandler=0; try @@ -1899,6 +1901,49 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz") elseif ($mode == 'bz') { $foundhandler=1; $compressdata = bzcompress($data, 9); } elseif ($mode == 'zip') { + if (class_exists('ZipArchive') && ! empty($conf->global->MAIN_USE_ZIPARCHIVE_FOR_ZIP_COMPRESS)) + { + $foundhandler=1; + + $rootPath = realpath($inputfile); + + dol_syslog("Class ZipArchive is set so we zip using ZipArchive to zip into ".$outputfile.' rootPath='.$rootPath); + $zip = new ZipArchive; + + if ($zip->open($outputfile, ZipArchive::CREATE)!==TRUE) { + $errormsg="Failed to open file ".$outputfile."\n"; + dol_syslog("dol_compress_file failure - ".$errormsg, LOG_ERR); + return -6; + } + + // Create recursive directory iterator + /** @var SplFileInfo[] $files */ + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($rootPath), + 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($rootPath) + 1); + + // Add current file to archive + $zip->addFile($filePath, $relativePath); + } + } + + // Zip archive will be created only after closing object + $zip->close(); + + dol_syslog("dol_compress_file success - ".count($zip->numFiles)." files"); + return 1; + } + if (defined('ODTPHP_PATHTOPCLZIP')) { $foundhandler=1; diff --git a/htdocs/master.inc.php b/htdocs/master.inc.php index 6ddb439f074..c09a04e8709 100644 --- a/htdocs/master.inc.php +++ b/htdocs/master.inc.php @@ -245,3 +245,4 @@ $hookmanager=new HookManager($db); if (! defined('MAIN_LABEL_MENTION_NPR') ) define('MAIN_LABEL_MENTION_NPR', 'NPR'); +//if (! defined('PCLZIP_TEMPORARY_DIR')) define('PCLZIP_TEMPORARY_DIR', $conf->user->dir_temp);