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
5 changed files with 67 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
<?php
/* 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
* 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',

View File

@@ -2,20 +2,21 @@
/* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* 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
* 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 <https://www.gnu.org/licenses/>.
*/
* 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 <https://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/admin/tools/export.php

View File

@@ -2,20 +2,21 @@
/* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* 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
* 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 <https://www.gnu.org/licenses/>.
*/
* 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 <https://www.gnu.org/licenses/>.
*/
/**
* \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);

View File

@@ -1,5 +1,6 @@
<?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
* 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');
@@ -337,12 +341,12 @@ class Utils
$fullcommandclear = $command." ".$paramclear." 2>&1";
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;
@@ -408,12 +412,12 @@ 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)) {
@@ -428,12 +432,12 @@ class Utils
// Get errorstring
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.
@@ -443,12 +447,12 @@ class Utils
// Close file
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 = '';

View File

@@ -1,6 +1,6 @@
<?php
/* 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) 2015 Marcos García <marcosgdf@gmail.com>
* 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') {
$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;