Creating a tar gz on the fly using PHP
A while ago, I thought about creating a tar.gz file for every download, so that if someone runs a search, he/she then can download all the images in the results. After a little bit of research, I found that PHP has a function for gzip. I also knew that the tar format just sticks files after one another, so if I can implement the tar format in PHP then I can gzip all images in the results.
I found this LGPL code that implemented the tar format. I used it (and modified it a little bit) to produce the online tar.gz functions:
-
// Computes the unsigned Checksum of a file’s header
-
// to try to ensure valid file
-
// PRIVATE ACCESS FUNCTION
-
function __computeUnsignedChecksum($bytestring)
-
{
-
for($i=0; $i<512; $i++)
-
for($i=0; $i<8; $i++)
-
-
return $unsigned_chksum;
-
}
-
-
// Generates a TAR file from the processed data
-
// PRIVATE ACCESS FUNCTION
-
function tarSection($Name, $Data, $information=NULL)
-
{
-
// Generate the TAR header for this file
-
-
$header .= "0";
-
-
// Compute header checksum
-
for($i=0; $i<6; $i++) {
-
}
-
-
// Pad file contents to byte count divisible by 512
-
-
// Add new tar formatted data to tar file contents
-
$tar_file = $header . $file_contents;
-
-
return $tar_file;
-
}
-
-
function targz($Name, $Data)
-
{
-
}
-
To use those functions all you have to do is send a header with the mime type for the tar gz ( application/x-gzip ) using the php header function. To add a tar/gz section for a file, read the file in an array using filegetcontents and pass the filename and data to the targz function. Echo what is returned. That’s it!
So why is it not active on clker.com website? I actually tried it and found that compression consumes a lot of CPU. In the first 20 minute I had more than one hundred connections for different users downloading their results and the CPU was saturated. This basically left no CPU for searching. So use it carefully, and only if you really need that functionality.
Technorati Tags: tar, gz, compress, online, php
Tags: compress, gz, online, php, tar