Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Create/Extract Zip Archives

by #include (Curate)
on Jun 03, 2003 at 04:22 UTC ( #262564=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info #include (include@riotmod.com)
http://linux.riotmod.com
Description: Using this script, you can create and extract zip archives. Not only is this script functional as a utility, it also demonstrates usage of the Archive::Zip module.
# zip.pl
# Author: Include
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

if (scalar(@ARGV) < 2){die "Zip.pl\nAuthor:Include\nUsage: zip.pl <c=c
+reate, x=extract> <filename> <files>\n";};

my $opt = shift(@ARGV);
my $ofile = shift(@ARGV);

# extract a zip file
if($opt=~/x/i){
print "Zip.pl\nAuthor:Include\n";
print "Extracting $ofile...\n";
$zip = Archive::Zip->new();
die 'Error reading zip file.' if $zip->read( $ofile ) != AZ_OK;
my @members = $zip->members();
foreach $element(@members)
{
  print "$element\n";
  $zip->extractMember($element);
}
print "Done!\n";
}

# create a zip file
if($opt=~/c/i){
print "Zip.pl\nAuthor:Include\n";
print "Creating $ofile...\n";
my $zip = Archive::Zip->new();
foreach my $memberName (map { glob } @ARGV)
{
    if (-d $memberName )
    {
    print "Adding $memberName\n";
        warn "Error adding directory $memberName\n"
            if $zip->addTree( $memberName, $memberName ) != AZ_OK;
    }
    else
    {
    print "Adding $memberName\n";
        $zip->addFile( $memberName )
            or warn "Error adding file $memberName\n";
    }
}
die 'Write error.' if $zip->writeToFileNamed( $ofile ) != AZ_OK;
print "Done!\n";
}
Replies are listed 'Best First'.
Re: Create/Extract Zip Archives
by msemtd (Scribe) on Jun 03, 2003 at 13:58 UTC
    Very useful -- minor changes are required for use strict and note that on Win32, first filename arg may need to be quoted dependent on shell used...
    # zip.pl # Author: Include use strict; use warnings; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); if ( scalar(@ARGV) < 2 ) { die "Zip.pl\nAuthor:Include\nUsage: zip.pl <c=create, x=extract> <filename +> <files>\n"; } my $opt = shift (@ARGV); my $ofile = shift (@ARGV); # extract a zip file if ( $opt =~ /x/i ) { print "Zip.pl\nAuthor:Include\n"; print "Extracting $ofile...\n"; my $zip = Archive::Zip->new(); die 'Error reading zip file.' if $zip->read($ofile) != AZ_OK; my @members = $zip->members(); foreach my $element (@members) { print "$element\n"; $zip->extractMember($element); } print "Done!\n"; } # create a zip file if ( $opt =~ /c/i ) { print "Zip.pl\nAuthor:Include\n"; print "Creating $ofile...\n"; my $zip = Archive::Zip->new(); foreach my $memberName ( map { glob } @ARGV ) { if ( -d $memberName ) { print "Adding $memberName\n"; warn "Error adding directory $memberName\n" if $zip->addTree( $memberName, $memberName ) != AZ_OK; } else { print "Adding $memberName\n"; $zip->addFile($memberName) or warn "Error adding file $memberName\n"; } } die 'Write error.' if $zip->writeToFileNamed($ofile) != AZ_OK; print "Done!\n"; }
Re: Create/Extract Zip Archives
by webchalkboard (Scribe) on Apr 08, 2005 at 11:50 UTC

    Thanks, that's really cool, i'm just trying to use the code in a program i'm writing and I get the following errors when I try and unzip my file, is there a problem with the zip file do you think?

    format error: can't find EOCD signature Archive::Zip::Archive::_findEndOfCentralDirectory('Archive::Zip::Arch +ive=HASH(0x8e87c00)','IO::File=GLOB(0x9196204)') called at /usr/lib/p +erl5/site_perl/5.8.3/Archive/Zip.pm line 955 Archive::Zip::Archive::readFromFileHandle('Archive::Zip::Archive=HASH +(0x8e87c00)','IO::File=GLOB(0x9196204)','tooled-up.gz') called at /us +r/lib/perl5/site_perl/5.8.3/Archive/Zip.pm line 929 Archive::Zip::Archive::read('Archive::Zip::Archive=HASH(0x8e87c00)',' +tooled-up.gz') called at zip.pl line 13 Error reading zip file. at zip.pl line 13.
    Learning without thought is labor lost; thought without learning is perilous. - Confucius
    WebChalkboard.com | For the love of art...
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://262564]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-06-04 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (35 votes). Check out past polls.

    Notices?