info > Archive::Zip

πŸ“¦ NAME

πŸ“– Archive::Zip - Provide an interface to ZIP archive files.

πŸš€ Quick Reference

Use CaseCommandDescription
πŸ“‚ Create a new empty zipmy $zip = Archive::Zip->new();πŸ†• Create a new empty zip archive object.
πŸ“ Add a directory to zip$zip->addDirectory('dirname/');βž• Append a directory member to the zip.
πŸ“ Add a string as a file$zip->addString('content', 'name.txt');βž• Append a member from a string, set compression if desired.
πŸ“„ Add a file from disk$zip->addFile('file.pl', 'newname.pl');βž• Append a member from an external file, optionally rename.
πŸ’Ύ Save zip to file$zip->writeToFileNamed('output.zip');πŸ’Ύ Write the zip archive to a named file, returns AZ_OK.
πŸ“– Read an existing zip$zip->read('input.zip');πŸ“– Read zipfile headers from a file, appending members.
πŸ” Get member by name$zip->memberNamed('filename.txt');πŸ” Return reference to member matching given internal filename.
πŸ“ Extract member contents$zip->contents('member.txt');πŸ“ Return uncompressed data for a member, or set new contents.
πŸ“‚ Extract all files$zip->extractTree();πŸ“‚ Extract all files in the zip with original names.
πŸ—‘οΈ Remove a member$zip->removeMember('member.txt');πŸ—‘οΈ Remove and return the given member or name.
πŸ”„ Update member from file$zip->updateMember('member.txt', 'newfile.txt');πŸ”„ Update a single member from a file if changed.
🌳 Add entire directory tree$zip->addTree('.', 'dest');🌳 Add all readable files and directories below root as dest/*.

πŸ“₯ 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

:CONSTANTS

Exports the following constants:

:MISC_CONSTANTS

Exports the following constants (only necessary for extending the module):

:ERROR_CODES

Explained below. Returned from most methods.

πŸšͺ 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!";
}

πŸ—œοΈ 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 Archive::Zip methods 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

Various operations on a zip file modify members. When a member is passed as an argument, you can either use a reference to the member itself, or the name of a member. Of course, using the name requires that names be unique within a zip (this is not enforced).

πŸ’Ύ Zip Archive I/O operations

A Zip archive can be written to a file or file handle, or read from one.

Archive::Zip
πŸ“¦ NAME πŸš€ Quick Reference πŸ“₯ SYNOPSIS πŸ“– DESCRIPTION πŸ“€ EXPORTS πŸšͺ ERROR CODES πŸ“¦ Archive::Zip Methods

Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
2026-08-14 21:26 @2600:1f28:365:80b0:4d23:66fa:c2bb:7bae
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!