压缩-如何使用PHP创建.gz文件?
我想使用PHP gzip压缩服务器上的文件。 有谁有输入文件并输出压缩文件的示例?
AlBeebe asked 2020-01-12T15:42:38Z
8个解决方案
97 votes
这段代码可以解决问题
// 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);
AlBeebe answered 2020-01-12T15:43:18Z
87 votes
此处的其他答案是在压缩过程中将整个文件加载到内存中,这将导致大型文件出现“内存不足”错误。 下面的函数在大文件上应更可靠,因为它以512kb的块大小读写文件。
/**
* GZIPs a file on disk (appending .gz to the name)
*
* From http://stackoverflow.com/questions/6073397/how-do-you-create-a-gz-file-using-php
* 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;
}
Simon East answered 2020-01-12T15:42:58Z
20 votes
另外,您可以使用php的包装器,即压缩器。 只需对代码进行最少的更改,就可以在gzip,bzip2或zip之间切换。
$input = "test.txt";
$output = $input.".gz";
file_put_contents("compress.zlib://$output", file_get_contents($input));
将compress.zip://更改为compress.zip://以进行zip压缩(请参阅有关zip压缩的此答案的评论),或将compress.bzip2://更改为bzip2压缩。
Carlos Campderrós answered 2020-01-12T15:43:44Z
5 votes
使用gzencode()的简单一个内衬:
gzencode(file_get_contents($file_name));
dtbarne answered 2020-01-12T15:44:04Z
3 votes
如果您只想解压缩文件,则可以使用,并且不会引起内存问题:
$bytes = file_put_contents($destination, gzopen($gzip_path, r));
Frank Carey answered 2020-01-12T15:44:24Z
1 votes
这对于许多人来说可能是显而易见的,但是如果您的系统上启用了任何程序执行功能($filename、my-file.txt && anothercommand、my-file.txt; anothercommand),则可以使用它们简单地将gzip文件。
exec("gzip ".$filename);
N.B .:确保在使用2699557732998448448128变量之前对其进行正确的清理,尤其是如果它来自用户输入(但不仅是)。 它可以用于运行任意命令,例如通过包含my-file.txt && anothercommand(或my-file.txt; anothercommand)之类的命令。
Niavlys answered 2020-01-12T15:44:49Z
0 votes
复制('file.txt','compress。[zlib://']。'file.txt.gz');参阅文件
Anatoliy Melnikov answered 2020-01-12T15:45:09Z
0 votes
这是一个改进的版本。 我摆脱了所有嵌套的if / else语句,从而降低了循环复杂度,通过异常进行了更好的错误处理,而不是跟踪布尔错误状态,某种类型的提示,并且我在文件扩展名为gz的情况下提供帮助 已经。 就代码行而言,它花了更长的时间,但是它更具可读性。
/**
* Compress a file using gzip
*
* Rewritten from Simon East's version here:
* https://stackoverflow.com/a/22754032/3499843
*
* @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;
}
Gerben answered 2020-01-12T15:45:30Z