Welcome GuestLogin

A Drunken Wiki


A Drunken Wiki is the companion wiki to the blog "A Drunken Madman", written by Jason Brown.


Search the wiki

Vaccination Saves Lives: Stop The Australian Vaccination Network

Find me on Twitter, Facebook, Google+, Pinterest and LinkedIn

Pin It

Alpha Archive Atheism Blue Mountains Climbing Cycling Development DotNet GPS Guitar HMHB JavaScript JQuery LINQ Meta MTB Music Perl Powershell RNP SharePoint Skepticism Social Media Training Ukulele Vaccines Visual Studio WebParts Woodford Festival 2011 XML


Using Archive::Zip (archive article)

Modified on 2012/01/11 18:43 by Administrator Categorized as Development, Perl

Using Archive::Zip

Creating zipfiles from Perl is easy if you've got the Archive::Zip module, so fire up PPM and install it why dontcha?

ActivePerl comes with Archive::Tar as standard, which is useful for creating TAR and TAR.GZ archives, but the prevalent compression format under windows is Zip. Sure, Winzip can handle TAR and TAR.GZ files, but we want to make ZipFiles, right?

Luckily for us there's an excellent and comprehensive Zip module available through CPAN called Archive::Zip by Ned Konz, which is quick to install and gives good results. So here we go from scratch

Fire yourself up a command line, and type 'ppm'. You'll get dropped into the Perl Package Manager, allowing you to install Archive::Zip (or any other module). Now type install Archive::Zip and sit back. On a quick connection, it'll install in a blink. On a dial-up it still takes under a minute, and PPM does all your configuration for you.

Now, Archive::Zip is a pretty comprehensive module, so I won't be running you through every single method - you can get a full listing of those from perldoc anyway. I'll just be giving you a few examples of common operations.

First, importing the module. It's recommended that in addition to the default, you also import constants and error codes, like so :

use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); Now, let's create a new zipfile. In the same folder as my new script, I have three other files, which I'd like to zip. They are 'popdemo.html', 'querystringclass.html' and 'perltar.pl'. So I write this little sub to add these to a zip archive

sub createFile	{
	my $zip = Archive::Zip->new();
	for('popdemo.html', 'querystringclass.html', 'perltar.pl')	{
		my $memberFile = $zip->addFile($_);
	}	$zip->zipfileComment('just a test zipfile');
	($zip->writeToFileNamed('perltest.zip') eq 0) 
				or die "couldn\'t write";	

So, we create a new Zip object, then loop through each file, adding it to the archive. The module allows you to individually set compression options on each member file, which we've done here for completeness (both are the defaults, but the docs explain more on these methods). Finally, we write a comment to the zipfile, and save it out to a file called 'perltest.zip'.

Of course, to be sure this worked first time round I opened the file in WinZip, and voila, there was my complete zipfile in front of me.

Let's list back the contents

sub listContents	{
	my $zip = Archive::Zip->new();
	die "couldn\'t read" unless $zip->read('test.zip') eq 0;
	for($zip->memberNames())	{
		print $_ . "\n";

Fairly simple, we create a new object and read a file into it. If the read fails, we die with a message. If not, we loop through the ->memberNames() array, printing each filename as we go.

What about extracting the contents of a zipfile? Well, that's the thrid and final operation in our short introduction to Archive::Zip and here it is in all it's simple glory

sub extractFile	{
	my $zip = Archive::Zip->new();
	for($zip->members())	{

Yes, it really is that simple, at least for a basic zipfile. Of course, the Zip format allows for far more than this, including multiple directories within a zip file, differing compression operations, extracting individual files and so on. The module provides a comprehensive interface to the entire gamut of the format, allowing you to create some pretty nifty zipfiles. Feel free, and explore.

Jason Brown


ScrewTurn Wiki version Some of the icons created by FamFamFam.