Reading and Writing .gz files in PHP
By: David Sklar in PHP Tutorials on 2008-12-01
You want to read or write compressed files. Use PHP's zlib extension to read or write gzip'ed files. To read a compressed file:
$zh = gzopen('file.gz','r') or die("can't open: $php_errormsg"); while ($line = gzgets($zh,1024)) { // $line is the next line of uncompressed data, up to 1024 bytes } gzclose($zh) or die("can't close: $php_errormsg");
Here's how to write a compressed file:
$zh = gzopen('file.gz','w') or die("can't open: $php_errormsg"); if (-1 == gzwrite($zh,$s)) { die("can't write: $php_errormsg"); } gzclose($zh) or die("can't close: $php_errormsg");
The zlib extension contains versions of many file-access functions, such as fopen(), fread(), and fwrite() (called gzopen(), gzread(), gzwrite(), etc.) that transparently compress data when writing and uncompress data when reading. The compression algorithm that zlib uses is compatible with the gzip and gunzip utilities.
For example, gzgets($zp,1024) works like fgets($fh,1024). It reads up to 1023 bytes, stopping earlier if it reaches EOF or a newline. For gzgets( ), this means 1023 uncompressed bytes.
However, gzseek() works differently than fseek(). It only supports seeking a specified number of bytes from the beginning of the file stream (the SEEK_SET argument to fseek()). Seeking forward (from the current position) is only supported in files opened for writing (the file is padded with a sequence of compressed zeroes). Seeking backwards is supported in files opened for reading, but it is very slow.
The zlib extension also has some functions to create compressed strings. The function gzencode()compresses a string and gives it the correct headers and formatting to be compatible with gunzip. Here's a simple gzip program:
$in_file = $_SERVER['argv'][1]; $out_file = $_SERVER['argv'][1].'.gz'; $ifh = fopen($in_file,'rb') or die("can't open $in_file: $php_errormsg"); $ofh = fopen($out_file,'wb') or die("can't open $out_file: $php_errormsg"); $encoded = gzencode(fread($ifh,filesize($in_file))) or die("can't encode data: $php_errormsg"); if (-1 == fwrite($ofh,$encoded)) { die("can't write: $php_errormsg"); } fclose($ofh) or die("can't close $out_file: $php_errormsg"); fclose($ifh) or die("can't close $in_file: $php_errormsg");
The guts of this program are the lines:
$encoded = gzencode(fread($ifh,filesize($in_file))) or die("can't encode data: $php_errormsg); if (-1 == fwrite($ofh,$encoded)) { die("can't write: $php_errormsg"); }
The compressed contents of $in_file are stored in $encoded and then written to $out_file with fwrite().
You can pass a second argument to gzencode() that indicates compression level. Set no compression with 0 and maximum compression with 9. The default level is 1. To adjust the simple gzip program for maximum compression, the encoding line becomes:
$encoded = gzencode(fread($ifh,filesize($in_file)),9) or die("can't encode data: $php_errormsg);
You can also compress and uncompress strings without the gzip-compatibility headers by using gzcompress()and gzuncompress().
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Send push notifications using Expo tokens in PHP
PHP convert string to lower case
A Basic Example using PHP in AWS (Amazon Web Services)
Different versions of PHP - History and evolution of PHP
PHP code to write to a CSV file for Microsoft Applications
PHP code to write to a CSV file from MySQL query
PHP code to import from CSV file to MySQL
Password must include both numeric and alphabetic characters - Magento
Resume or Pause File Uploads in PHP
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Comments