我想使用PHP在我的服务器上对文件进行gzip压缩。是否有人有一个例子,可以输入一个文件并输出一个压缩文件?
我想使用PHP在我的服务器上对文件进行gzip压缩。是否有人有一个例子,可以输入一个文件并输出一个压缩文件?
这段代码很有效
// Name of the file we're compressing
$file = "test.txt";
// Name of the gz file we're creating
$gzfile = "test.gz";
// Open the gz file (w9 is the highest compression)
$fp = gzopen ($gzfile, 'w9');
// Compress the file
gzwrite ($fp, file_get_contents($file));
// Close the gz file and we're done
gzclose($fp);
其他答案在压缩期间将整个文件加载到内存中,这会导致在大文件上出现“内存不足”错误。下面的函数应该更可靠,因为它以512kb的块读取和写入文件。
/**
* GZIPs a file on disk (appending .gz to the name)
*
* From https://dev59.com/LW025IYBdhLWcg3wVkYR
* Based on function by Kioob at:
* http://www.php.net/manual/en/function.gzwrite.php#34955
*
* @param string $source Path to file that should be compressed
* @param integer $level GZIP compression level (default: 9)
* @return string New filename (with .gz appended) if success, or false if operation fails
*/
function gzCompressFile($source, $level = 9){
$dest = $source . '.gz';
$mode = 'wb' . $level;
$error = false;
if ($fp_out = gzopen($dest, $mode)) {
if ($fp_in = fopen($source,'rb')) {
while (!feof($fp_in))
gzwrite($fp_out, fread($fp_in, 1024 * 512));
fclose($fp_in);
} else {
$error = true;
}
gzclose($fp_out);
} else {
$error = true;
}
if ($error)
return false;
else
return $dest;
}
更新: Gerben发布了一个改进版本的函数,它更加清晰,并且在出错时使用异常而不是返回false。请参见https://dev59.com/LW025IYBdhLWcg3wVkYR#56140427
compress.zip://
不支持压缩。请参见http://www.php.net/manual/en/wrappers.compression.php上的说明。 - Alex使用gzencode()的简单一行代码:
gzencode(file_get_contents($file_name));
这是一个改进版。我摆脱了所有嵌套的if/else语句,从而降低了圈复杂度,通过异常处理来实现更好的错误处理,而不是跟踪布尔错误状态,还有一些类型提示,并且如果文件已经具有gz扩展名,则退出。虽然代码行数有点增加,但可读性大大提高。
/**
* Compress a file using gzip
*
* Rewritten from Simon East's version here:
* https://dev59.com/LW025IYBdhLWcg3wVkYR#22754032
*
* @param string $inFilename Input filename
* @param int $level Compression level (default: 9)
*
* @throws Exception if the input or output file can not be opened
*
* @return string Output filename
*/
function gzcompressfile(string $inFilename, int $level = 9): string
{
// Is the file gzipped already?
$extension = pathinfo($inFilename, PATHINFO_EXTENSION);
if ($extension == "gz") {
return $inFilename;
}
// Open input file
$inFile = fopen($inFilename, "rb");
if ($inFile === false) {
throw new \Exception("Unable to open input file: $inFilename");
}
// Open output file
$gzFilename = $inFilename.".gz";
$mode = "wb".$level;
$gzFile = gzopen($gzFilename, $mode);
if ($gzFile === false) {
fclose($inFile);
throw new \Exception("Unable to open output file: $gzFilename");
}
// Stream copy
$length = 512 * 1024; // 512 kB
while (!feof($inFile)) {
gzwrite($gzFile, fread($inFile, $length));
}
// Close files
fclose($inFile);
gzclose($gzFile);
// Return the new filename
return $gzFilename;
}
$bytes = file_put_contents($destination, gzopen($gzip_path, r));
对许多人来说可能很明显,但如果你的系统启用了任何程序执行函数 (exec
、system
、shell_exec
) 中的一个,你可以使用它们来简单地 gzip
文件。
exec("gzip ".$filename);
注意:一定要正确地对$filename
变量进行清理,特别是如果它来自用户输入(不仅仅如此)。它可能被用于运行任意命令,例如包含类似my-file.txt && anothercommand
(或者my-file.txt; anothercommand
)的内容。
escapeshellarg()
或escapeshellcmd()
。 - sylbru为需要的任何人压缩文件夹
function gzCompressFile($source, $level = 9)
{
$tarFile = $source . '.tar';
if (is_dir($source)) {
$tar = new PharData($tarFile);
$files = scandir($source);
foreach ($files as $file) {
if (is_file($source . '/' . $file)) {
$tar->addFile($source . '/' . $file, $file);
}
}
}
$dest = $tarFile . '.gz';
$mode = 'wb' . $level;
$error = false;
if ($fp_out = gzopen($dest, $mode)) {
if ($fp_in = fopen($tarFile, 'rb')) {
while (!feof($fp_in))
gzwrite($fp_out, fread($fp_in, 1024 * 512));
fclose($fp_in);
} else {
$error = true;
}
gzclose($fp_out);
unlink($tarFile);
} else {
$error = true;
}
if ($error)
return false;
else
return $dest;
}