Merge pull request #18997 from hregis/fix_add_zstd_compression

NEW add support for zstd compression
This commit is contained in:
Laurent Destailleur 2021-10-17 13:49:06 +02:00 committed by GitHub
commit 0727e182ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 50 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2006-2018 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2006-2018 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2006-2018 Regis Houssin <regis.houssin@inodbox.com> * Copyright (C) 2006-2021 Regis Houssin <regis.houssin@inodbox.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * 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', 'id' => 'radio_compression_bzip',
'label' => $langs->trans("Bzip2") 'label' => $langs->trans("Bzip2")
); );
$compression['zstd'] = array(
'function' => 'zstd_compress',
'id' => 'radio_compression_zstd',
'label' => $langs->trans("Zstd")
);
$compression['none'] = array( $compression['none'] = array(
'function' => '', 'function' => '',
'id' => 'radio_compression_none', 'id' => 'radio_compression_none',

View File

@ -2,20 +2,21 @@
/* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr> * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
* Copyright (C) 2021 Regis Houssin <regis.houssin@inodbox.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/** /**
* \file htdocs/admin/tools/export.php * \file htdocs/admin/tools/export.php

View File

@ -2,20 +2,21 @@
/* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr> * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
* Copyright (C) 2021 Regis Houssin <regis.houssin@inodbox.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/** /**
* \file htdocs/admin/tools/export_files.php * \file htdocs/admin/tools/export_files.php
@ -41,7 +42,7 @@ $file = trim(GETPOST('zipfilename_template', 'alpha'));
$compression = GETPOST('compression', 'aZ09'); $compression = GETPOST('compression', 'aZ09');
$file = dol_sanitizeFileName($file); $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'); $sortfield = GETPOST('sortfield', 'aZ09comma');
$sortorder = GETPOST('sortorder', 'aZ09comma'); $sortorder = GETPOST('sortorder', 'aZ09comma');
@ -137,7 +138,7 @@ if ($compression == 'zip') {
$errormsg = $langs->trans("ErrorFailedToWriteInDir", $outputdir); $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'); $userlogin = ($user->login ? $user->login : 'unknown');
$outputfile = $conf->admin->dir_temp.'/export_files.'.$userlogin.'.out'; // File used with popen method $outputfile = $conf->admin->dir_temp.'/export_files.'.$userlogin.'.out'; // File used with popen method
@ -157,9 +158,12 @@ if ($compression == 'zip') {
if ($compression == 'gz') { if ($compression == 'gz') {
$cmd = "gzip -f ".$outputdir."/".$file; $cmd = "gzip -f ".$outputdir."/".$file;
} }
if ($compression == 'bz') { elseif ($compression == 'bz') {
$cmd = "bzip2 -f ".$outputdir."/".$file; $cmd = "bzip2 -f ".$outputdir."/".$file;
} }
elseif ($compression == 'zstd') {
$cmd = "zstd -z -9 -q --rm ".$outputdir."/".$file;
}
$result = $utils->executeCLI($cmd, $outputfile); $result = $utils->executeCLI($cmd, $outputfile);

View File

@ -1,5 +1,6 @@
<?php <?php
/* Copyright (C) 2016 Destailleur Laurent <eldy@users.sourceforge.net> /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2021 Regis Houssin <regis.houssin@inodbox.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * 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'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
// Check compression parameter // 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"); $langs->load("errors");
$this->error = $langs->transnoentitiesnoconv("ErrorBadValueForParameter", $compression, "Compression"); $this->error = $langs->transnoentitiesnoconv("ErrorBadValueForParameter", $compression, "Compression");
return -1; return -1;
@ -249,9 +250,12 @@ class Utils
if ($compression == 'gz') { if ($compression == 'gz') {
$outputfile .= '.gz'; $outputfile .= '.gz';
} }
if ($compression == 'bz') { elseif ($compression == 'bz') {
$outputfile .= '.bz2'; $outputfile .= '.bz2';
} }
elseif ($compression == 'zstd') {
$outputfile .= '.zst';
}
$outputerror = $outputfile.'.err'; $outputerror = $outputfile.'.err';
dol_mkdir($conf->admin->dir_output.'/backup'); dol_mkdir($conf->admin->dir_output.'/backup');
@ -337,12 +341,12 @@ class Utils
$fullcommandclear = $command." ".$paramclear." 2>&1"; $fullcommandclear = $command." ".$paramclear." 2>&1";
if ($compression == 'none') { if ($compression == 'none') {
$handle = fopen($outputfile, 'w'); $handle = fopen($outputfile, 'w');
} } elseif ($compression == 'gz') {
if ($compression == 'gz') {
$handle = gzopen($outputfile, 'w'); $handle = gzopen($outputfile, 'w');
} } elseif ($compression == 'bz') {
if ($compression == 'bz') {
$handle = bzopen($outputfile, 'w'); $handle = bzopen($outputfile, 'w');
} elseif ($compression == 'zstd') {
$handle = fopen($outputfile, 'w');
} }
$ok = 0; $ok = 0;
@ -408,12 +412,12 @@ class Utils
if ($compression == 'none') { if ($compression == 'none') {
fclose($handle); fclose($handle);
} } elseif ($compression == 'gz') {
if ($compression == 'gz') {
gzclose($handle); gzclose($handle);
} } elseif ($compression == 'bz') {
if ($compression == 'bz') {
bzclose($handle); bzclose($handle);
} elseif ($compression == 'zstd') {
fclose($handle);
} }
if (!empty($conf->global->MAIN_UMASK)) { if (!empty($conf->global->MAIN_UMASK)) {
@ -428,12 +432,12 @@ class Utils
// Get errorstring // Get errorstring
if ($compression == 'none') { if ($compression == 'none') {
$handle = fopen($outputfile, 'r'); $handle = fopen($outputfile, 'r');
} } elseif ($compression == 'gz') {
if ($compression == 'gz') {
$handle = gzopen($outputfile, 'r'); $handle = gzopen($outputfile, 'r');
} } elseif ($compression == 'bz') {
if ($compression == 'bz') {
$handle = bzopen($outputfile, 'r'); $handle = bzopen($outputfile, 'r');
} elseif ($compression == 'zstd') {
$handle = fopen($outputfile, 'r');
} }
if ($handle) { if ($handle) {
// Get 2048 first chars of error message. // Get 2048 first chars of error message.
@ -443,12 +447,12 @@ class Utils
// Close file // Close file
if ($compression == 'none') { if ($compression == 'none') {
fclose($handle); fclose($handle);
} } elseif ($compression == 'gz') {
if ($compression == 'gz') {
gzclose($handle); gzclose($handle);
} } elseif ($compression == 'bz') {
if ($compression == 'bz') {
bzclose($handle); bzclose($handle);
} elseif ($compression == 'zstd') {
fclose($handle);
} }
if ($ok && preg_match('/^-- (MySql|MariaDB)/i', $errormsg)) { // No error if ($ok && preg_match('/^-- (MySql|MariaDB)/i', $errormsg)) { // No error
$errormsg = ''; $errormsg = '';

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2012-2015 Regis Houssin <regis.houssin@inodbox.com> * Copyright (C) 2012-2021 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2012-2016 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2012-2016 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015 Marcos García <marcosgdf@gmail.com> * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr> * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
@ -1971,6 +1971,9 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz", &$errorstring
} elseif ($mode == 'bz') { } elseif ($mode == 'bz') {
$foundhandler = 1; $foundhandler = 1;
$compressdata = bzcompress($data, 9); $compressdata = bzcompress($data, 9);
} elseif ($mode == 'zstd') {
$foundhandler = 1;
$compressdata = zstd_compress($data, 9);
} elseif ($mode == 'zip') { } elseif ($mode == 'zip') {
if (class_exists('ZipArchive') && !empty($conf->global->MAIN_USE_ZIPARCHIVE_FOR_ZIP_COMPRESS)) { if (class_exists('ZipArchive') && !empty($conf->global->MAIN_USE_ZIPARCHIVE_FOR_ZIP_COMPRESS)) {
$foundhandler = 1; $foundhandler = 1;