man > Archive::Zip(3pm)

📛 NAME

Archive::Zip — Provide an interface to ZIP archive files.

🚀 Quick Reference

Use CaseCommandDescription
đŸ“Ļ Create a new ZipArchive::Zip->new()Create an empty zip archive object
➕ Add a directory$zip->addDirectory('dirname/')Append a directory member to the zip
➕ Add a string as file$zip->addString($string, $name)Append a member from a string with optional compression
➕ Add a file from disk$zip->addFile('file.pl', 'zipName.pl')Append a member from an external file
💾 Write zip to file$zip->writeToFileNamed('out.zip')Save the archive to a named file
📖 Read zip from file$zip->read('file.zip')Read zipfile headers and append members
📂 List members$zip->members()Return array of all member objects
🔍 Find member by name$zip->memberNamed('name')Return member ref or undef
📄 Get member contents$zip->contents('member')Return uncompressed data for a member
đŸ—œī¸ Set compression$member->desiredCompressionMethod(COMPRESSION_DEFLATED)Change compression method for a member
🌲 Add entire directory tree$zip->addTree('.', 'prefix')Recursively add files from a directory
đŸŒŗ Extract all files$zip->extractTree()Extract all members with original names

📋 SYNOPSIS

# Create a Zip file
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
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';
}

📖 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 Compress::Raw::Zlib library to read and write the compressed streams inside the files.

One can use Archive::Zip::MemberRead to read the zip file archive members as if they were files.

📁 File Naming

Regardless of what your local file system uses for file naming, names in a Zip file are in Unix format (forward slashes (/) separating directory names, etc.).

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:

đŸ—ī¸ Archive::Zip Object Model

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.

đŸŒŗ 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

đŸ“Ļ EXPORTS

❌ ERROR CODES

Many of the methods in Archive::Zip return error codes. These are implemented as inline subroutines, using the use constant pragma. They can be imported into your namespace using the :ERROR_CODES tag:

use Archive::Zip qw( :ERROR_CODES );

...

unless ( $zip->read( 'myfile.zip' ) == AZ_OK ) {
    die "whoops!";
}

đŸšĒ Exit Codes

CodeConstantDescription
0AZ_OK✅ Success
1AZ_STREAM_END✅ Stream ended normally
2AZ_ERROR❌ Generic error
3AZ_FORMAT_ERROR❌ Format error in ZIP file
4AZ_IO_ERROR❌ I/O error

đŸ—œī¸ Compression

Archive::Zip allows each member of a ZIP file to be compressed (using the Deflate algorithm) or uncompressed.

Other compression algorithms that some versions of ZIP have been able to produce are not supported. Each member has two compression methods: the one it's stored as (this is always COMPRESSION_STORED for string and external file members), and the one you desire for the member in the zip file.

These can be different, of course, so you can make a zip member that is not compressed out of one that is, and vice versa.

You can inquire about the current compression and set the desired compression method:

my $member = $zip->memberNamed( 'xyz.txt' );
$member->compressionMethod();    # return current compression

# set to read uncompressed
$member->desiredCompressionMethod( COMPRESSION_STORED );

# set to read compressed
$member->desiredCompressionMethod( COMPRESSION_DEFLATED );

There are two different compression methods:

📊 Compression Levels

If a member's desiredCompressionMethod is COMPRESSION_DEFLATED, you can choose different compression levels. This choice may affect the speed of compression and decompression, as well as the size of the compressed member data.

$member->desiredCompressionLevel( 9 );

The levels given can be:

🔧 Archive::Zip Methods

The Archive::Zip class (and its invisible subclass Archive::Zip::Archive) implement generic zip file functionality. Creating a new Archive::Zip object actually makes an Archive::Zip::Archive object, but you don't have to worry about this unless you're subclassing.

đŸ—ī¸ Constructor

đŸ› ī¸ Zip Archive Utility Methods

These may be called as functions or as object methods. Do not call them as class methods:

$zip = Archive::Zip->new();
$crc = Archive::Zip::computeCRC32( 'ghijkl' );    # OK
$crc = $zip->computeCRC32( 'ghijkl' );            # also OK
$crc = Archive::Zip->computeCRC32( 'ghijkl' );    # NOT OK

🔍 Zip Archive Accessors

📂 Zip Archive Member Operations

💾 Zip Archive I/O Operations

🌲 Zip Archive Tree Operations

These enable operation on an entire tree of members or files.

use Archive::Zip;
my $zip = Archive::Zip->new();

# add all readable files and directories below . as xyz/*
$zip->addTree( '.', 'xyz' );

# add all readable plain files below /abc as def/*
$zip->addTree( '/abc', 'def', sub { -f && -r } );

# add all .c files below /tmp as stuff/*
$zip->addTreeMatching( '/tmp', 'stuff', '\.c$' );

# add all .o files below /tmp as stuff/* if they aren't writable
$zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { ! -w } );

# add all .so files below /tmp that are smaller than 200 bytes as stuff/*
$zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { -s < 200 } );

# and write them into a file
$zip->writeToFileNamed('xxx.zip');

# now extract the same files into /tmpx
$zip->extractTree( 'stuff', '/tmpx' );

🌐 Global Variables

🔧 MEMBER OPERATIONS

🏭 Member Class Methods

Several constructors allow you to construct members without adding them to a zip archive.

🔎 Member Simple Accessors

These methods get (and/or set) member attribute values.

📖 Low-level Member Data Reading

It is possible to use lower-level routines to access member data streams. Example:

my ( $member, $status, $bufferRef );
$member = $zip->memberNamed( 'xyz.txt' );
$member->desiredCompressionMethod( COMPRESSION_STORED );
$status = $member->rewindData();
die "error $status" unless $status == AZ_OK;
while ( ! $member->readIsDone() )
{
( $bufferRef, $status ) = $member->readChunk();
die "error $status"
            if $status != AZ_OK && $status != AZ_STREAM_END;
# do something with $bufferRef:
print $$bufferRef;
}
$member->endRead();

📄 Archive::Zip::FileMember Methods

The Archive::Zip::FileMember class extends Archive::Zip::Member and adds an externalFileName and an fh member.

📄 Archive::Zip::ZipFileMember Methods

The Archive::Zip::ZipFileMember class represents members that have been read from external zip files.

📚 REQUIRED MODULES

🐛 BUGS AND CAVEATS

âš ī¸ When not to use Archive::Zip

If you are just going to be extracting zips (and/or other archives) you are recommended to look at using Archive::Extract instead, as it is much easier to use and factors out archive-specific functionality.

🔄 Zip64 Format Support

Since version 1.66 Archive::Zip supports the zip64 format. On some Perl interpreters (those lacking 64-bit support or older than 5.10.0), zip64 is not supported. Constant ZIP64_SUPPORTED equals true if supported. If not supported and you try to read or write a zip64 archive, Archive::Zip returns AZ_ERROR with a message.

📋 "versionMadeBy" and "versionNeededToExtract"

These fields are handled as follows:

âš ī¸ Try to avoid IO::Scalar

As of version 1.11, this module no longer works with IO::Scalar as it incorrectly implements seeking. Use IO::String instead, which is smaller, lighter, and perfectly compatible with regular seekable filehandles. Support for IO::Scalar will likely not be restored.

❌ Wrong password for encrypted members

When an encrypted member is read using the wrong password, you currently have to re-read the entire archive to try again with the correct password.

📝 TO DO

🆘 SUPPORT

Bugs should be reported on GitHub:

https://github.com/redhotpenguin/perl-Archive-Zip/issues

For other issues contact the maintainer.

👤 AUTHOR

ÂŠī¸ COPYRIGHT

Some parts copyright 2006 - 2012 Adam Kennedy.

Some parts copyright 2005 Steve Peters.

Original work copyright 2000 - 2004 Ned Konz.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

👀 SEE ALSO

Archive::Zip(3pm)
📛 NAME 🚀 Quick Reference 📋 SYNOPSIS 📖 DESCRIPTION
📁 File Naming đŸ—ī¸ Archive::Zip Object Model đŸŒŗ Inheritance
đŸ“Ļ EXPORTS ❌ ERROR CODES đŸšĒ Exit Codes
đŸ—œī¸ Compression 📊 Compression Levels
🔧 Archive::Zip Methods
đŸ—ī¸ Constructor đŸ› ī¸ Zip Archive Utility Methods 🔍 Zip Archive Accessors 📂 Zip Archive Member Operations 💾 Zip Archive I/O Operations 🌲 Zip Archive Tree Operations 🌐 Global Variables
🔧 MEMBER OPERATIONS
🏭 Member Class Methods 🔎 Member Simple Accessors 📖 Low-level Member Data Reading 📄 Archive::Zip::FileMember Methods 📄 Archive::Zip::ZipFileMember Methods
📚 REQUIRED MODULES 🐛 BUGS AND CAVEATS
âš ī¸ When not to use Archive::Zip 🔄 Zip64 Format Support 📋 "versionMadeBy" and "versionNeededToExtract" âš ī¸ Try to avoid IO::Scalar ❌ Wrong password for encrypted members
📝 TO DO 🆘 SUPPORT 👤 AUTHOR ÂŠī¸ COPYRIGHT 👀 SEE ALSO

Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-18 20:58 @2600:1f28:365:80b0:b91e:58eb:6587:15c8
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^