Archive::Zip::SimpleUnzip - Read Zip Archives
use Archive::Zip::SimpleUnzip qw($SimpleUnzipError) ; my $z = new Archive::Zip::SimpleUnzip "my.zip" or die "Cannot open zip file: $SimpleUnzipError\n" ; # How many members in the archive? my $members = scalar $z->names(); # Get the names of all the members in a zip archive my @names = $z->names(); # test member existence if ($z->exists("abc.txt")) { ... } # read the zip comment my $comment = $zip->comment(); # open a member by name my $member = $z->member("abc.txt"); my $name = $member->name(); my $content = $member->content(); my $comment = $member->comment(); # Iterate through a zip archive while (my $member = $z->next) { print $member->name() . "\n" ; } # Archive::Zip::SimpleUnzip::Member # open a filehandle to read from a zip member $fh = $member->open("mydata1.txt"); # read blocks of data read($fh, $buffer, 1234) ; # or a line at a time $line = <$fh> ; close $fh; $z->close();
Archive::Zip::SimpleUnzip is a module that allows reading of Zip archives. For writing Zip archives, there is a companion module, called Archive::Zip::SimpleZip, that can create Zip archives.
NOTE This is alpha quality code, so the interface may change.
Read zip archive from a file, a filehandle or from an in-memory buffer.
Perl Filehandle interface for reading a zip member.
Supports deflate, store, bzip2 and lzma compression.
Supports Zip64, so can read archves larger than 4Gig and/or have greater than 64K members.
$z = new Archive::Zip::SimpleUnzip "myzipfile.zip" [, OPTIONS] ; $z = new Archive::Zip::SimpleUnzip \$buffer [, OPTIONS] ; $z = new Archive::Zip::SimpleUnzip $filehandle [, OPTIONS] ;
The constructor takes one mandatory parameter along with zero or more optional parameters.
The mandatory parameter controls where the zip archive is read from. This can be any one of the following
Input from a Filename
When SimpleUnzip is passed a string, it will read the zip archive from the filename stored in the string.
Input from a String
When SimpleUnzip is passed a string reference, like \$buffer, it will read the in-memory zip archive from that string.
\$buffer
Input from a Filehandle
When SimpleUnzip is passed a filehandle, it will read the zip archive from that filehandle. Note the filehandle must be seekable.
See "Options" for a list of the optional parameters that can be specified when calling the constructor.
None yet.
Returns the payload data for $member. Returns undef if the member does not exist.
undef
Returns the comment, if any, associated with the zip archive.
Tests for the existence of member "name" in the zip archive.
In scalar context returns the number of members in the Zip archive.
In array context returns a list of the names of the members in the Zip archive.
Returns the next member from the zip archive as a Archive::Zip::SimpleUnzip::Member object. See "Archive::Zip::SimpleUnzip::Member"
Standard usage is
use Archive::Zip::SimpleUnzip qw($SimpleUnzipError) ; my $match = "hello"; my $zipfile = "my.zip"; my $z = new Archive::Zip::SimpleUnzip $zipfile or die "Cannot open zip file: $SimpleUnzipError\n" ; while (my $member = $z->next()) { my $name = $member->name(); my $fh = $member->open(); while (<$fh>) { my $offset = print "$name, line $.\n" if /$match/; } }
The next method returns a member object of type Archive::Zip::SimpleUnzip::Member that has the following methods.
next
Archive::Zip::SimpleUnzip::Member
Returns the name of the member.
Returns the member comment.
Returns the uncompressed content.
Returns a filehandle that can be used to read the uncompressed content.
The code below shoes how this module is used to read the contents of the member "abc.txt" from the zip archive "my1.zip".
use Archive::Zip::SimpleUnzip qw($SimpleUnzipError) ; my $z = new Archive::Zip::SimpleUnzip "my1.zip" or die "Cannot open zip file: $SimpleUnzipError\n" ; my $name = "abc.txt"; if ($z->exists($name)) { print $z->content($name); } else { warn "$name not present in my1.zip\n" }
use Archive::Zip::SimpleUnzip qw($SimpleUnzipError) ; my $zipfile = "my.zip"; my $z = new Archive::Zip::SimpleUnzip $zipfile or die "Cannot open zip file: $SimpleUnzipError\n" ; my $members = $z->names(); print "Zip file '$zipfile' has $members entries\n"; while (my $member = $z->next()) { print "$member->name()\n"; }
Here is a simple grep, that walks through a zip file and prints matching strings.
The intention is to be interoperable with zip archives created by other programs, like pkzip or WinZip, but the majority of testing carried out used the Info-Zip zip/unzip programs running on Linux.
This doesn't necessarily mean that there is no interoperability with other zip programs like pkzip and WinZip - it just means that I haven't tested them. Please report any issues you find.
The following compression methods are supported
This is the most common compression used in zip archives.
This is used when no compression has been carried out.
Only if the IO-Compress-Bzip2 module is available.
IO-Compress-Bzip2
Only if the IO-Compress-Lzma module is available.
IO-Compress-Lzma
This modules supports Zip64, so it can read archves larger than 4Gig and/or have greater than 64K members.
The following features are no currently supported.
Compression methods not listed in "Compression Methods Supported"
Multi-Volume Archives
Encrypted Archives
Archive::Zip::SimpleZip, Archive::Zip, IO::Compress::Zip, IO::Uncompress::UnZip
This module was written by Paul Marquess, pmqs@cpan.org.
See the Changes file.
Copyright (c) 2018 Paul Marquess. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Archive::Zip::SimpleZip, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Archive::Zip::SimpleZip
CPAN shell
perl -MCPAN -e shell install Archive::Zip::SimpleZip
For more information on module installation, please visit the detailed CPAN module installation guide.