Archive

Posts Tagged ‘zip’

Reading And Writing To Compressed gzip Files

April 23rd, 2009 No comments

Reading and writing to compressed gzip files can be done in much the same way as reading and writing normal files. The main difference is the use of some special functions that compress and uncompress the data. Rather then use fopen() to open a file, you open a compressed file with gzopen(). This is the case for many of the file access functions, although you should be aware that they don’t all work exactly the same as each other. Here are some examples of the compression functions in use.

To read from a compressed file use the following script.

// read from file
$inputfile = 'text.gz';
 
$filecontents = '';
 
$ifh = gzopen($inputfile, 'r');
 
while ( $line = gzgets($ifh, 1024) ) {
    $filecontents .= $line;
}
echo $filecontents;
gzclose($ifh);

To write to a compressed file use the following script.

// writing to a complessed file
$inputfile = 'text.gz';
 
$string = 'lalalala';
$filecontents = '';
 
$ifh = gzopen($inputfile, 'r');
 
while ( $line = gzgets($ifh, 1024) ) {
    $filecontents .= $line;
}
gzclose($ifh);
 
$ifh = gzopen($inputfile, 'w');
if ( !gzwrite($ifh, $filecontents.$string) ) {
    // write failed
}
 
gzclose($ifh);
 

To compress a normal file use the following script.

// compress
$inputfile = 'text.txt';
$outputfile = 'text.gz';
 
// open files
$ifh = fopen($inputfile, 'rb');
$ofh =  fopen($outputfile, 'wb');
 
// encode string
$encoded = gzencode(fread($ifh, filesize($inputfile)));
 
if ( !fwrite($ofh, $encoded) ) {
    // write failed
}
 
// close files
fclose($ifh);
fclose($ofh);

To uncompress a gzip file use the following script.

// uncompress
$inputfile = 'text.gz';
$outputfile = 'text.txt';
 
$ofh =  fopen($outputfile, 'wb');
 
$string = '';
 
$ifh = gzopen($inputfile, 'r');
while ( $line = gzgets($ifh, 1024) ) {
    $string .= $line;
}
 
if ( !fwrite($ofh, $string) ) {
    // write failed
}
 
echo $string;
 
gzclose($ifh);
fclose($ofh);

Notice from these examples that you can open a compressed file using fopen(), but you need to use the 'b' flag to indicate to fopen that you want the file to be opened in a binary format. Basically, the two following calls are very much the same.

$ifh = gzopen($inputfile, 'r');
$ifh = fopen($inputfile, 'rb');

The gzopen() function can also take some other parameters as the mode. These include the compression level (eg. wb1 or wb9) or a strategy. The strategy can be either f for filtered (used as wb9f) or h for Huffman only compression (used as wb1h).

Categories: PHP Tags: , , , , , , ,

Create Compressed Files With Phing

January 15th, 2009 1 comment

Building your projects into directories is nice, but distributing these projects is difficult if you have to build the compressed files yourself. Phing has the ability to create zip and tar files using simple commands.

The most convenient way of using the tar and zip commands is by using a fileset. But rather than use the fileset that was used to copy the files into the build directory it is best to create a separate fileset that is used to compress the contents of the build directory.

<fileset dir="./" id="zipFiles">
<include name="myProject_build/**" />
</fileset>

To compress the files into a tar use the tar element. This accepts two parameters, the destination file (called destfile) and the compression to use (called compression). The compression attribute can take one of three values, these are gzip, bzip2 and none. Using none will not run any compression algorithms on the files. The following example will create a file called myProject.tar in the current directory, which contains all of the files and directories from the myProject_build directory.

<tar destfile="myProject.tar" compression="gzip">
<fileset refid="zipFiles" />
</tar>

To compress the files into a zip file you need to use the zip element. This works in exactly the same way as the tar element, except it doesn’t have a compression attribute.

<zip destfile="myProject.zip">
<fileset refid="zipFiles" />
</zip>

The tar element works very well and the file created was as expected, but I had a little bit of trouble with the zip element. I am using Windows XP to run phing and the file that was created was blocked by Windows for having odd content. After I figured out how to open it I realized that phing had used the full path to the build folder. So rather than the zip file containing a folder called myProject_build, it contained the following folder structure, with the correct files included in the end directory.

C:\dev\tests\phing\myProject_build

Has anyone else seen this? I would be grateful for any help on the subject.

You can also use the zip and tar elements without the fileset element by using the basedir attribute. By setting the basedir you are telling phing to use everything in that directory and every sub directory.

<tar destfile="phing.tar" basedir="." compression="gzip"/>

There is a warning on the phing website that states using basedir and fileset simultaneously can result in strange contents in the archive. So I would suggest using one or the other.

Categories: PHP Tags: , , , , , ,