Archive-Zip
view release on metacpan or search on metacpan
lib/Archive/Zip.pm view on Meta::CPAN
sub setErrorHandler {
my $errorHandler = (ref($_[0]) eq 'HASH') ? shift->{subroutine} : shift;
$errorHandler = \&Carp::carp unless defined($errorHandler);
my $oldErrorHandler = $Archive::Zip::ErrorHandler;
$Archive::Zip::ErrorHandler = $errorHandler;
return $oldErrorHandler;
}
######################################################################
# Private utility functions (not methods).
sub _printError {
my $string = join(' ', @_, "\n");
my $oldCarpLevel = $Carp::CarpLevel;
$Carp::CarpLevel += 2;
&{$ErrorHandler}($string);
$Carp::CarpLevel = $oldCarpLevel;
}
# This is called on format errors.
sub _formatError {
shift if ref($_[0]);
_printError('format error:', @_);
return AZ_FORMAT_ERROR;
}
# This is called on IO errors.
sub _ioError {
shift if ref($_[0]);
_printError('IO error:', @_, ':', $!);
return AZ_IO_ERROR;
}
# This is called on generic errors.
sub _error {
shift if ref($_[0]);
_printError('error:', @_);
return AZ_ERROR;
}
# This is called if zip64 format is not supported but would be
# required.
sub _zip64NotSupported {
shift if ref($_[0]);
_printError('zip64 format not supported on this Perl interpreter');
return AZ_ERROR;
}
# Called when a subclass should have implemented
# something but didn't
sub _subclassResponsibility {
Carp::croak("subclass Responsibility\n");
}
# Try to set the given file handle or object into binary mode.
sub _binmode {
my $fh = shift;
return _CAN($fh, 'binmode') ? $fh->binmode() : binmode($fh);
}
# Attempt to guess whether file handle is seekable.
# Because of problems with Windows, this only returns true when
# the file handle is a real file.
sub _isSeekable {
my $fh = shift;
return 0 unless ref $fh;
_ISA($fh, "IO::Scalar") # IO::Scalar objects are brokenly-seekable
and return 0;
_ISA($fh, "IO::String")
and return 1;
if (_ISA($fh, "IO::Seekable")) {
# Unfortunately, some things like FileHandle objects
# return true for Seekable, but AREN'T!!!!!
_ISA($fh, "FileHandle")
and return 0;
return 1;
}
# open my $fh, "+<", \$data;
ref $fh eq "GLOB" && eval { seek $fh, 0, 1 } and return 1;
_CAN($fh, "stat")
and return -f $fh;
return (_CAN($fh, "seek") and _CAN($fh, "tell")) ? 1 : 0;
}
# Print to the filehandle, while making sure the pesky Perl special global
# variables don't interfere.
sub _print {
my ($self, $fh, @data) = @_;
local $\;
return $fh->print(@data);
}
# Return an opened IO::Handle
# my ( $status, fh ) = _newFileHandle( 'fileName', 'w' );
# Can take a filename, file handle, or ref to GLOB
# Or, if given something that is a ref but not an IO::Handle,
# passes back the same thing.
sub _newFileHandle {
my $fd = shift;
my $status = 1;
my $handle;
if (ref($fd)) {
if (_ISA($fd, 'IO::Scalar') or _ISA($fd, 'IO::String')) {
$handle = $fd;
} elsif (_ISA($fd, 'IO::Handle') or ref($fd) eq 'GLOB') {
$handle = IO::File->new;
$status = $handle->fdopen($fd, @_);
} else {
$handle = $fd;
}
} else {
$handle = IO::File->new;
$status = $handle->open($fd, @_);
}
return ($status, $handle);
lib/Archive/Zip.pm view on Meta::CPAN
my $zip = Archive::Zip->new();
# Add a directory
my $dir_member = $zip->addDirectory( 'dirname/' );
# Add a file from a string with compression
my $string_member = $zip->addString( 'This is a test', 'stringMember.txt' );
$string_member->desiredCompressionMethod( COMPRESSION_DEFLATED );
# Add a file from disk
my $file_member = $zip->addFile( 'xyz.pl', 'AnotherName.pl' );
# Save the Zip file
unless ( $zip->writeToFileNamed('someZip.zip') == AZ_OK ) {
die 'write error';
}
# Read a Zip file
my $somezip = Archive::Zip->new();
unless ( $somezip->read( 'someZip.zip' ) == AZ_OK ) {
die 'read error';
}
# Change the compression type for a file in the Zip
my $member = $somezip->memberNamed( 'stringMember.txt' );
$member->desiredCompressionMethod( COMPRESSION_STORED );
unless ( $zip->writeToFileNamed( 'someOtherZip.zip' ) == AZ_OK ) {
die 'write error';
}
=head1 DESCRIPTION
The Archive::Zip module allows a Perl program to create, manipulate, read,
and write Zip archive files.
Zip archives can be created, or you can read from existing zip files.
Once created, they can be written to files, streams, or strings. Members
can be added, removed, extracted, replaced, rearranged, and enumerated.
They can also be renamed or have their dates, comments, or other attributes
queried or modified. Their data can be compressed or uncompressed as needed.
Members can be created from members in existing Zip files, or from existing
directories, files, or strings.
This module uses the L<Compress::Raw::Zlib> library to read and write the
compressed streams inside the files.
One can use L<Archive::Zip::MemberRead> to read the zip file archive members
as if they were files.
=head2 File Naming
Regardless of what your local file system uses for file naming, names in a
Zip file are in Unix format (I<forward> slashes (/) separating directory
names, etc.).
C<Archive::Zip> tries to be consistent with file naming conventions, and will
translate back and forth between native and Zip file names.
However, it can't guess which format names are in. So two rules control what
kind of file name you must pass various routines:
=over 4
=item Names of files are in local format.
C<File::Spec> and C<File::Basename> are used for various file
operations. When you're referring to a file on your system, use its
file naming conventions.
=item Names of archive members are in Unix format.
This applies to every method that refers to an archive member, or
provides a name for new archive members. The C<extract()> methods
that can take one or two names will convert from local to zip names
if you call them with a single name.
=back
=head2 Archive::Zip Object Model
=head3 Overview
Archive::Zip::Archive objects are what you ordinarily deal with.
These maintain the structure of a zip file, without necessarily
holding data. When a zip is read from a disk file, the (possibly
compressed) data still lives in the file, not in memory. Archive
members hold information about the individual members, but not
(usually) the actual member data. When the zip is written to a
(different) file, the member data is compressed or copied as needed.
It is possible to make archive members whose data is held in a string
in memory, but this is not done when a zip file is read. Directory
members don't have any data.
=head2 Inheritance
Exporter
Archive::Zip Common base class, has defs.
Archive::Zip::Archive A Zip archive.
Archive::Zip::Member Abstract superclass for all members.
Archive::Zip::StringMember Member made from a string
Archive::Zip::FileMember Member made from an external file
Archive::Zip::ZipFileMember Member that lives in a zip file
Archive::Zip::NewFileMember Member whose data is in a file
Archive::Zip::DirectoryMember Member that is a directory
=head1 EXPORTS
=over 4
=item :CONSTANTS
Exports the following constants:
FA_MSDOS FA_UNIX GPBF_ENCRYPTED_MASK
GPBF_DEFLATING_COMPRESSION_MASK GPBF_HAS_DATA_DESCRIPTOR_MASK
COMPRESSION_STORED COMPRESSION_DEFLATED IFA_TEXT_FILE_MASK
IFA_TEXT_FILE IFA_BINARY_FILE COMPRESSION_LEVEL_NONE
COMPRESSION_LEVEL_DEFAULT COMPRESSION_LEVEL_FASTEST
COMPRESSION_LEVEL_BEST_COMPRESSION
( run in 1.246 second using v1.01-cache-2.11-cpan-39bf76dae61 )