InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Reading and Writing Compressed Files in PHP
PHP

Reading and Writing Compressed Files in PHP

InfinityCoder December 26, 2016

You want to read or write compressed files.

Use the compress.zlib or compress.bzip2 stream wrapper with the standard file functions.
To read data from a gzip-compressed file:

1
2
3
4
5
6
7
8
$file = __DIR__ . '/lots-of-data.gz';
$fh = fopen("compress.zlib://$file",'r') or die("can't open: $php_errormsg");
if ($fh) {
   while ($line = fgets($fh)) {
      // $line is the next line of uncompressed data
   }
   fclose($fh) or die("can't close: $php_errormsg");
}

The compress.zlib stream wrapper provides access to files that have been compressed with the gzip algorithm.

The compress.bzip2 stream wrapper provides access to files that have been compressed with the bzip2 algorithm.

Both stream wrappers allow reading, writing, and appending with compressed files. To enable the zlib and bzip2 compression streams, build PHP with –with-zlib and –with-bz2, respectively.
In addition to the stream wrappers, which allow access to compressed local files, there are stream filters that compress (or uncompress) arbitrary streams on the fly.

The zlib.deflate and zlib.inflate filters compress and uncompress data according to the zlib “deflate” algorithm.

The bzip2.compress and bzip2.uncompress filters do the same for the bzip2 algorithm.
Each stream filter must be applied to a stream after it is created. This example uses the bzip2 stream filters to read compressed data from a URL:

1
2
3
4
5
6
7
8
9
$fp = fopen('http://www.example.com/something-compressed.bz2','r');
if ($fp) {
   stream_filter_append($fp, 'bzip2.uncompress');
   while (! feof($fp)) {
       $data = fread($fp);
       // do something with $data;
   }
   fclose($fp);
}

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Finding the nth Occurrence of a Match in PHP
You want to find the nth word match instead of …

Finding the nth Occurrence of a Match in PHP

Returning More Than One Value in PHP
You want to return more than one value from a …

Returning More Than One Value in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap