如何使用PHP创建.gz文件?

时间:2022-09-07 15:46:08

I would like to gzip compress a file on my server using PHP. Does anyone have an example that would input a file and output a compressed file?

我想使用PHP在我的服务器上压缩文件。有没有人有一个输入文件并输出压缩文件的例子?

6 个解决方案

#1


72  

The other answers here load the entire file into memory during compression, which will cause 'out of memory' errors on large files. The function below should be more reliable on large files as it reads and writes files in 512kb chunks.

这里的其他答案在压缩期间将整个文件加载到内存中,这将导致大文件上的“内存不足”错误。下面的函数在大文件上应该更可靠,因为它以512kb块读取和写入文件。

/**
 * GZIPs a file on disk (appending .gz to the name)
 *
 * From http://*.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; 
} 

#2


92  

This code does the trick

这段代码可以解决问题

// 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);

#3


19  

Also, you could use php's wrappers, the compression ones. With a minimal change in the code you would be able to switch between gzip, bzip2 or zip.

另外,你可以使用php的包装器,压缩包装器。只需对代码进行最小的更改,您就可以在gzip,bzip2或zip之间切换。

$input = "test.txt";
$output = $input.".gz";

file_put_contents("compress.zlib://$output", file_get_contents($input));

change compress.zlib:// to compress.zip:// for zip compression (see comment to this answer about zip compression), or to compress.bzip2:// to bzip2 compression.

将compress.zlib://更改为compress.zip:进行zip压缩(请参阅有关zip压缩的回答的评论),或者压缩.bzip2://到bzip2压缩。

#4


5  

Simple one liner with gzencode():

使用gzencode()的简单单线程:

gzencode(file_get_contents($file_name));

#5


3  

If you are looking to just unzip a file, this works and doesn't cause issues with memory:

如果您只想解压缩文件,这可以工作,不会导致内存问题:

$bytes = file_put_contents($destination, gzopen($gzip_path, r));

#6


1  

It's probably obvious to many, but if any of the program execution functions is enabled on your system (exec, system, shell_exec), you can use them to simply gzip the file.

这对许多人来说可能是显而易见的,但是如果在你的系统上启用了任何程序执行功能(exec,system,shell_exec),你可以使用它们来简单地gzip文件。

exec("gzip ".$filename);

#1


72  

The other answers here load the entire file into memory during compression, which will cause 'out of memory' errors on large files. The function below should be more reliable on large files as it reads and writes files in 512kb chunks.

这里的其他答案在压缩期间将整个文件加载到内存中,这将导致大文件上的“内存不足”错误。下面的函数在大文件上应该更可靠,因为它以512kb块读取和写入文件。

/**
 * GZIPs a file on disk (appending .gz to the name)
 *
 * From http://*.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; 
} 

#2


92  

This code does the trick

这段代码可以解决问题

// 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);

#3


19  

Also, you could use php's wrappers, the compression ones. With a minimal change in the code you would be able to switch between gzip, bzip2 or zip.

另外,你可以使用php的包装器,压缩包装器。只需对代码进行最小的更改,您就可以在gzip,bzip2或zip之间切换。

$input = "test.txt";
$output = $input.".gz";

file_put_contents("compress.zlib://$output", file_get_contents($input));

change compress.zlib:// to compress.zip:// for zip compression (see comment to this answer about zip compression), or to compress.bzip2:// to bzip2 compression.

将compress.zlib://更改为compress.zip:进行zip压缩(请参阅有关zip压缩的回答的评论),或者压缩.bzip2://到bzip2压缩。

#4


5  

Simple one liner with gzencode():

使用gzencode()的简单单线程:

gzencode(file_get_contents($file_name));

#5


3  

If you are looking to just unzip a file, this works and doesn't cause issues with memory:

如果您只想解压缩文件,这可以工作,不会导致内存问题:

$bytes = file_put_contents($destination, gzopen($gzip_path, r));

#6


1  

It's probably obvious to many, but if any of the program execution functions is enabled on your system (exec, system, shell_exec), you can use them to simply gzip the file.

这对许多人来说可能是显而易见的,但是如果在你的系统上启用了任何程序执行功能(exec,system,shell_exec),你可以使用它们来简单地gzip文件。

exec("gzip ".$filename);