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?
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With