Zip-It or DIY Tar-balls.
p. Who hasn’t sat in front of their computer night after night wondering aloud – to only the monitor and the voices in their head – how they are going to build a zip file or tar ball dynamically using nothing more than PHP. I know that if I had actually thought this, it would have kept me up at nights. Thankfully, before it got that far, those wacky code monkeys over at PHPit have come up with a “tutorial”:http://www.phpit.net/article/creating-zip-tar-archives-dynamically-php/ that walks you through just this very conundrum.
p. First, they start you off with a pair of PEARs. Specifically, the PEAR “archive_zip”:http://pear.php.net/package/Archive_Zip and “archive_tar”:http://pear.php.net/package/Archive_Tar packages. Checking my kit to make sure it was complete before starting I noticed that my 6 month old CentOS install included Archive_Tar but not Archive_Zip. This is probably because Archive_Zip is still marked as stability “beta” (Hey, if the entire Web 2.0 can be beta, why can’t Archive_zip?) So I had to drop to a command line and install it with the magic incantation:
pear install channel://pear.php.net/Archive_Zip-0.1.1
That did the trick. Now I was ready to rumble.
p. The first page walks you through a quick, 3 line example of how to compress a single text file. (Hey, the tutorial is useful even if the examples aren’t always). It’s a great way to get your feet wet and sets up the concepts used later on.
p. From there they take you through all the major areas that you need to know and understand. TMP directories, fixing relative paths and so on. They even end it with a nice section on how to stream your newly created archive out to your user.
p. The only noggin-scratcher is the section on creating a reusable function. Since Archive_Zip and Archive_Tar are classes, wouldn’t it have been better to create a wrapper class or a helper class? Oh well, if I didn’t find fault with it, how would I feel good about myself today?
p. Overall, it’s a very good tutorial and it liberally sprinkled with code for your cut-n-paste pleasure. If you’ve ever wanted to be able to compress things on the fly and ship them out via a browser, this is a great tutorial for you. If you are interested in selling items for download but don’t want to put your goods in a directory where Apache can get to them, this is a great article for you. If it’s 2:00 AM and you just can’t sleep, this is an OK article for you. (It’s low on the snooze factor because it contains lots of vitamin ohhh and ahhhh.)
Until next time,
“=C=”:http://blog.calevans.com
Categories: News
Tags: Intermediate, Novice, PEAR, PHP4, PHP5, Samples, TAR Archives, ZIP Archives

