Categories
PHP
Javascript
MySQL
C#
VB
VB.NET
ASP.NET
Regex
Packaging & compression
General Web Tech
Tech Speak


Google


This website looks best on firefox.
 
Resource Center : PHP : <PHP and compression/archiving, with examples>

PHP and compression/archiving, with examples

Posted by: Floresense Team
Pages: 1  2  3  4  

This article contains:
1. Introduction to compression with PHP.. extending PHP with extensions.
2. Using ZIP
3. Using gZip
4. Using bZip2 and LZF
5. Using 'File_Archive' PEAR package


File compression is slowly becoming the most used programs in computers and servers, because there's so much information to save, backup, transfer, or secure.

Everyone knows that compressing files reduces their size on the computer and allows more space for more information. Also, decompression makes files larger sometimes expanding them to whole directories or drives of information.

File compression is the mode of software packaging, format for backing up old/archived data etc., But compressing your email attachments or any file transfer between one computer to another makes it secure compared to transfering as it is.

Compressing a file is very often considered one of the steps to protecting data making it secure.. though there are many ways to push trojans and viruses into compressed data. We will take up the idea of encrypting-decrypting data through php in another article...where again we will use file compression programs discussed in this article to finally compress the encrypted data for another layer of protection.

PHP has some very easy, simple yet powerful, extensions for file compression support. zip, gZip, bZip2, LZF libraries are popular extensions with php. Each one is actually a different compression format... all of them have their own algorithms to compress files, and some of them support mentioning which compression algorithm you want to use, while creating/opening a compressed file. We will look at some of the features of these libraries with reference to PHP.

Note that whatever features or functions you do with all these compression libraries, are actually not made available by PHP but the actual open source format of the library which guys in PHP wrap-up into extensions. That simply means, no matter whether you bzip a file using Microsoft C# or using PHP, the underlying library that does the job is the same open source library.. and C# or PHP people only package it suitable to their environments.

Using ZIP:
This library's core comes from: http://zziplib.sourceforge.net/ To know more about how it compresses files/other information, refer to this website.

Zip is an archiving and compression format. It means you can use it for compressing a single file, and also to compress a folder of files. All compression formats that support archiving like zip, rar, tar, etc., are mostly refered to as archiving formats, rather than compression formats.

This library is not enabled by default in PHP. If you get errors like "undefined function" or "undefined call" while trying to use this library's methods, it just means this library has to be installed to be used.

Library file: php_zip.dll
Install Type: PHP extension.

You should be having php_zip.dll in your php's 'ext' or 'extensions' folder. If not, you should try downloading the php package again and use the dll. If you try to copy that dll from say a php4 folder to a php5 folder, apache will fail when you restart.. because php_zip.dll that you copied was specifically created for php4.

This is a PECL extension.. which means you will find latest versions and updates at http://pecl.php.net For windows users, http://pecl4win.php.net

Once you have the dll, Install it as perinstructions here http://php.net/install.windows.extensions. If you get errors like "unable to load library", troubleshoot as mentioned here http://www.floresense.com/resc_center/?c=3&art=83.

If you are looking for more such php extensions.. you can find an updated list here http://pecl.php.net/packages.php

Creating a ZIP compressed archive:

<?
$outFile = "testBundle.zip";
// make zip archive

$zip = new ZipArchive();
$zip->open($outFile, ZIPARCHIVE::CREATE);
//add Files
$zip->addFile("testFile.txt");
$zip->addFile("testFile2.txt");
$zip->addFile("testFile3.txt");
$zip->addFile("testFile4.txt");
$zip->close();
?>


Steps in above code:
- We create an instance of ZipArchive class.
- We open (create if not present) a zip archive file by the name testBundle.zip
- We add files into our archive.. and close.
- That's it, this would create a .zip compressed archive with the four files.

We can also add a new file to the archive with data at runtime as below.

//add File from string.. without actual file
$zip->addFromString("t1.txt", "this is a stest");

The above code creates a file t1.txt directly inside the archive with the content passed as the second parameter.

Extracting a zip compressed archive into a folder:

Below code sample is for extracting the contents of a .zip file into a folder given by '/test/'.
Note that if the folder 'test' is not there, it is created.
$zip = new ZipArchive;
if ($zip->open($outFile) === TRUE) {
$zip->extractTo('/test/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}

The method extractTo does the process of reading all files and filenames in the archive and writing new equivalent files in the output folder.

We can also selectively extract files.. for examples we want to modify the above example such that only testFile2.txt and testFile4.txt is extracted.
    $zip->extractTo('/test/',array('testFile2.txt','testFile4.txt'));

Read files/content/file details from a .zip archive, without extracting them.

$zip = zip_open("D:tests".$outFile);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
echo "Name: " . zip_entry_name($zip_entry) . "n";
echo "Actual Filesize: " . zip_entry_filesize($zip_entry) . "n";
echo "Compressed Size: " . zip_entry_compressedsize($zip_entry) . "n";
echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "n";
if (zip_entry_open($zip, $zip_entry, "r")) {
echo "File Contents:n";
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
echo "$bufn";
zip_entry_close($zip_entry);
}
echo "n";
}
zip_close($zip);
}

The above code, gives an example of the methods to use for extracting the contents of files, or status and size of files, in a .zip archive.

Pages: 1  2  3  4  

Advertisement

2005 - 2008 © Floresense.com