Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a .gz file using PHP?

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?

like image 882
AlBeebe Avatar asked May 20 '11 14:05

AlBeebe


People also ask

How do I create a gzip file?

gz file is a Tar archive compressed with Gzip. To create a tar. gz file, use the tar -czf command, followed by the archive name and files you want to add.

What is the difference between gzip and gz?

GZ compressed files features low compression ratio. GNUZip employs a classic Deflate file compressor, comparable to ZIP and PEA, but inferior to BZip2 or modern compressors as 7Z and RAR format. GZip standard is meant only to provide single file compression, other features including archival are out of its scope.

Can you gzip a gz file?

Using Gzip/Gunzip to Create or Unzip GZ FIlesGzip and Gunzip commands can be used to unzip GZ files in Linux, except for compressed Tar archives. Although a TAR. GZ file is a TAR archive compressed by Gzip, only the Tar command will allow you to uncompress and extract files from it.


1 Answers

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); 
like image 163
AlBeebe Avatar answered Oct 09 '22 21:10

AlBeebe