From 84e4bd8820ed110dcbf8c771e9d445f88c8b50bf Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Sun, 17 Oct 2021 11:59:25 +0200 Subject: [PATCH 1/2] NEW add zstd compression --- htdocs/admin/tools/dolibarr_export.php | 7 ++++- htdocs/admin/tools/export.php | 27 +++++++++--------- htdocs/admin/tools/export_files.php | 36 +++++++++++++----------- htdocs/core/class/utils.class.php | 38 ++++++++++++++++++-------- htdocs/core/lib/files.lib.php | 5 +++- 5 files changed, 71 insertions(+), 42 deletions(-) diff --git a/htdocs/admin/tools/dolibarr_export.php b/htdocs/admin/tools/dolibarr_export.php index 77200404a03..6215de6396a 100644 --- a/htdocs/admin/tools/dolibarr_export.php +++ b/htdocs/admin/tools/dolibarr_export.php @@ -1,6 +1,6 @@ - * Copyright (C) 2006-2018 Regis Houssin + * Copyright (C) 2006-2021 Regis Houssin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -441,6 +441,11 @@ if (in_array($type, array('mysql', 'mysqli'))) { 'id' => 'radio_compression_bzip', 'label' => $langs->trans("Bzip2") ); + $compression['zstd'] = array( + 'function' => 'zstd_compress', + 'id' => 'radio_compression_zstd', + 'label' => $langs->trans("Zstd") + ); $compression['none'] = array( 'function' => '', 'id' => 'radio_compression_none', diff --git a/htdocs/admin/tools/export.php b/htdocs/admin/tools/export.php index 0fe8be7d50c..4dd2fbad58e 100644 --- a/htdocs/admin/tools/export.php +++ b/htdocs/admin/tools/export.php @@ -2,20 +2,21 @@ /* Copyright (C) 2006-2014 Laurent Destailleur * Copyright (C) 2011 Juanjo Menent * Copyright (C) 2015 Raphaël Doursenaud + * Copyright (C) 2021 Regis Houssin * -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -*/ + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * \file htdocs/admin/tools/export.php diff --git a/htdocs/admin/tools/export_files.php b/htdocs/admin/tools/export_files.php index 384b08e19cf..9fabaa3b0b2 100644 --- a/htdocs/admin/tools/export_files.php +++ b/htdocs/admin/tools/export_files.php @@ -2,20 +2,21 @@ /* Copyright (C) 2006-2014 Laurent Destailleur * Copyright (C) 2011 Juanjo Menent * Copyright (C) 2015 Raphaël Doursenaud + * Copyright (C) 2021 Regis Houssin * -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -*/ + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ /** * \file htdocs/admin/tools/export_files.php @@ -41,7 +42,7 @@ $file = trim(GETPOST('zipfilename_template', 'alpha')); $compression = GETPOST('compression', 'aZ09'); $file = dol_sanitizeFileName($file); -$file = preg_replace('/(\.zip|\.tar|\.tgz|\.gz|\.tar\.gz|\.bz2)$/i', '', $file); +$file = preg_replace('/(\.zip|\.tar|\.tgz|\.gz|\.tar\.gz|\.bz2|\.zst)$/i', '', $file); $sortfield = GETPOST('sortfield', 'aZ09comma'); $sortorder = GETPOST('sortorder', 'aZ09comma'); @@ -137,7 +138,7 @@ if ($compression == 'zip') { $errormsg = $langs->trans("ErrorFailedToWriteInDir", $outputdir); } } -} elseif (in_array($compression, array('gz', 'bz'))) { +} elseif (in_array($compression, array('gz', 'bz', 'zstd'))) { $userlogin = ($user->login ? $user->login : 'unknown'); $outputfile = $conf->admin->dir_temp.'/export_files.'.$userlogin.'.out'; // File used with popen method @@ -157,9 +158,12 @@ if ($compression == 'zip') { if ($compression == 'gz') { $cmd = "gzip -f ".$outputdir."/".$file; } - if ($compression == 'bz') { + elseif ($compression == 'bz') { $cmd = "bzip2 -f ".$outputdir."/".$file; } + elseif ($compression == 'zstd') { + $cmd = "zstd -z -9 -q --rm ".$outputdir."/".$file; + } $result = $utils->executeCLI($cmd, $outputfile); diff --git a/htdocs/core/class/utils.class.php b/htdocs/core/class/utils.class.php index fd89a00a810..aff18cd4506 100644 --- a/htdocs/core/class/utils.class.php +++ b/htdocs/core/class/utils.class.php @@ -1,5 +1,6 @@ +/* Copyright (C) 2016 Laurent Destailleur + * Copyright (C) 2021 Regis Houssin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -202,7 +203,7 @@ class Utils require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; // Check compression parameter - if (!in_array($compression, array('none', 'gz', 'bz', 'zip'))) { + if (!in_array($compression, array('none', 'gz', 'bz', 'zip', 'zstd'))) { $langs->load("errors"); $this->error = $langs->transnoentitiesnoconv("ErrorBadValueForParameter", $compression, "Compression"); return -1; @@ -249,9 +250,12 @@ class Utils if ($compression == 'gz') { $outputfile .= '.gz'; } - if ($compression == 'bz') { + elseif ($compression == 'bz') { $outputfile .= '.bz2'; } + elseif ($compression == 'zstd') { + $outputfile .= '.zst'; + } $outputerror = $outputfile.'.err'; dol_mkdir($conf->admin->dir_output.'/backup'); @@ -338,12 +342,15 @@ class Utils if ($compression == 'none') { $handle = fopen($outputfile, 'w'); } - if ($compression == 'gz') { + elseif ($compression == 'gz') { $handle = gzopen($outputfile, 'w'); } - if ($compression == 'bz') { + elseif ($compression == 'bz') { $handle = bzopen($outputfile, 'w'); } + elseif ($compression == 'zstd') { + $handle = fopen($outputfile, 'w'); + } $ok = 0; if ($handle) { @@ -409,12 +416,15 @@ class Utils if ($compression == 'none') { fclose($handle); } - if ($compression == 'gz') { + elseif ($compression == 'gz') { gzclose($handle); } - if ($compression == 'bz') { + elseif ($compression == 'bz') { bzclose($handle); } + elseif ($compression == 'zstd') { + fclose($handle); + } if (!empty($conf->global->MAIN_UMASK)) { @chmod($outputfile, octdec($conf->global->MAIN_UMASK)); @@ -429,12 +439,15 @@ class Utils if ($compression == 'none') { $handle = fopen($outputfile, 'r'); } - if ($compression == 'gz') { + elseif ($compression == 'gz') { $handle = gzopen($outputfile, 'r'); } - if ($compression == 'bz') { + elseif ($compression == 'bz') { $handle = bzopen($outputfile, 'r'); } + elseif ($compression == 'zstd') { + $handle = fopen($outputfile, 'r'); + } if ($handle) { // Get 2048 first chars of error message. $errormsg = fgets($handle, 2048); @@ -444,12 +457,15 @@ class Utils if ($compression == 'none') { fclose($handle); } - if ($compression == 'gz') { + elseif ($compression == 'gz') { gzclose($handle); } - if ($compression == 'bz') { + elseif ($compression == 'bz') { bzclose($handle); } + elseif ($compression == 'zstd') { + fclose($handle); + } if ($ok && preg_match('/^-- (MySql|MariaDB)/i', $errormsg)) { // No error $errormsg = ''; } else { diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index d34a8e5ee30..2175de373ef 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -1,6 +1,6 @@ - * Copyright (C) 2012-2015 Regis Houssin + * Copyright (C) 2012-2021 Regis Houssin * Copyright (C) 2012-2016 Juanjo Menent * Copyright (C) 2015 Marcos García * Copyright (C) 2016 Raphaël Doursenaud @@ -1971,6 +1971,9 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz", &$errorstring } elseif ($mode == 'bz') { $foundhandler = 1; $compressdata = bzcompress($data, 9); + } elseif ($mode == 'zstd') { + $foundhandler = 1; + $compressdata = zstd_compress($data, 9); } elseif ($mode == 'zip') { if (class_exists('ZipArchive') && !empty($conf->global->MAIN_USE_ZIPARCHIVE_FOR_ZIP_COMPRESS)) { $foundhandler = 1; From 9fa6a8daa7dfbed476f949abaa0a8f41ded8969e Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sun, 17 Oct 2021 10:05:58 +0000 Subject: [PATCH 2/2] Fixing style errors. --- htdocs/core/class/utils.class.php | 36 +++++++++++-------------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/htdocs/core/class/utils.class.php b/htdocs/core/class/utils.class.php index aff18cd4506..6bfbbdb98ad 100644 --- a/htdocs/core/class/utils.class.php +++ b/htdocs/core/class/utils.class.php @@ -341,14 +341,11 @@ class Utils $fullcommandclear = $command." ".$paramclear." 2>&1"; if ($compression == 'none') { $handle = fopen($outputfile, 'w'); - } - elseif ($compression == 'gz') { + } elseif ($compression == 'gz') { $handle = gzopen($outputfile, 'w'); - } - elseif ($compression == 'bz') { + } elseif ($compression == 'bz') { $handle = bzopen($outputfile, 'w'); - } - elseif ($compression == 'zstd') { + } elseif ($compression == 'zstd') { $handle = fopen($outputfile, 'w'); } @@ -415,14 +412,11 @@ class Utils if ($compression == 'none') { fclose($handle); - } - elseif ($compression == 'gz') { + } elseif ($compression == 'gz') { gzclose($handle); - } - elseif ($compression == 'bz') { + } elseif ($compression == 'bz') { bzclose($handle); - } - elseif ($compression == 'zstd') { + } elseif ($compression == 'zstd') { fclose($handle); } @@ -438,14 +432,11 @@ class Utils // Get errorstring if ($compression == 'none') { $handle = fopen($outputfile, 'r'); - } - elseif ($compression == 'gz') { + } elseif ($compression == 'gz') { $handle = gzopen($outputfile, 'r'); - } - elseif ($compression == 'bz') { + } elseif ($compression == 'bz') { $handle = bzopen($outputfile, 'r'); - } - elseif ($compression == 'zstd') { + } elseif ($compression == 'zstd') { $handle = fopen($outputfile, 'r'); } if ($handle) { @@ -456,14 +447,11 @@ class Utils // Close file if ($compression == 'none') { fclose($handle); - } - elseif ($compression == 'gz') { + } elseif ($compression == 'gz') { gzclose($handle); - } - elseif ($compression == 'bz') { + } elseif ($compression == 'bz') { bzclose($handle); - } - elseif ($compression == 'zstd') { + } elseif ($compression == 'zstd') { fclose($handle); } if ($ok && preg_match('/^-- (MySql|MariaDB)/i', $errormsg)) { // No error