How to extract files with Archive::Zip

From espinola.net

Jump to: navigation, search

Import semantics of the Archive::Zip module so its subroutine functions can be used in the script:

use Archive::Zip

Declare and/or define variables to be used later in the script:

my $zipError = 0;
my ( $arrayItem, $zipFileListing );

Read/open the archive into memory:

my $zipFile = Archive::Zip->new();
unless ( $zipFile->read(<zipFileName>) == AZ_OK ) {
    print "Unable to open $zipFile\n";
}
else {
    <next script actions go here>;
}

Extract files based on a regexp match:

    foreach $zipFileListing ( $zipFile->membersMatching("<regexp>") ) {
        unless ( $zipFile->extractMemberWithoutPaths( $zipFileListing->fileName(), ) == AZ_OK ) {
            print "Unable to extract files from $zipFile\n";
            $zipError = 1;
        }
    }

Extract files based on a list array:

    foreach $arrayItem (@Array) {
        unless ( $zipFile->extractTree( "$arrayItem", "<savedItemName>" ) == AZ_OK ) {
            print "Unable to extract list from $zipFile\n";
            $zipError = 2;
        }
    }

Extract all files in the archive:

    unless ( $zipFile->extractTree( "", "<destinationDir>/" ) == AZ_OK ) {
        print "Unable to extract archive from $zipFile\n";
        $zipError = 3;
    }

References

Archive::Zip on CPAN

Personal tools