Archive::Extract(3pm) User Contributed Perl Documentation Archive::Extract(3pm)
Archive::Extract - A generic archive extracting mechanism
| Use Case | Command | Description |
|---|---|---|
| Create extractor object | my $ae = Archive::Extract->new( archive => 'file.tar.gz' ); | Auto-detects type from extension |
| Extract to current directory | $ae->extract; | Extracts archive to cwd() |
| Extract to specific path | $ae->extract( to => '/tmp' ); | Extract to given directory |
| Handle extraction errors | $ae->extract or die $ae->error; | Die with error message on failure |
| List files in archive | my @files = @{ $ae->files }; | Array ref of file paths |
| Get extraction directory | my $outdir = $ae->extract_path; | Path where files were extracted |
| Determine type from filename | my $type = Archive::Extract->type_for('file.zip'); | Returns type string based on extension |
| Check if archive is .tgz | $ae->is_tgz; | True/false for .tar.gz |
use Archive::Extract;
### build an Archive::Extract object ###
my $ae = Archive::Extract->new( archive => 'foo.tgz' );
### extract to cwd() ###
my $ok = $ae->extract;
### extract to /tmp ###
my $ok = $ae->extract( to => '/tmp' );
### what if something went wrong?
my $ok = $ae->extract or die $ae->error;
### files from the archive ###
my $files = $ae->files;
### dir that was extracted to ###
my $outdir = $ae->extract_path;
### quick check methods ###
$ae->is_tar # is it a .tar file?
$ae->is_tgz # is it a .tar.gz or .tgz file?
$ae->is_gz; # is it a .gz file?
$ae->is_zip; # is it a .zip file?
$ae->is_bz2; # is it a .bz2 file?
$ae->is_tbz; # is it a .tar.bz2 or .tbz file?
$ae->is_lzma; # is it a .lzma file?
$ae->is_xz; # is it a .xz file?
$ae->is_txz; # is it a .tar.xz or .txz file?
### absolute path to the archive you provided ###
$ae->archive;
### commandline tools, if found ###
$ae->bin_tar # path to /bin/tar, if found
$ae->bin_gzip # path to /bin/gzip, if found
$ae->bin_unzip # path to /bin/unzip, if found
$ae->bin_bunzip2 # path to /bin/bunzip2 if found
$ae->bin_unlzma # path to /bin/unlzma if found
$ae->bin_unxz # path to /bin/unxz if found
Archive::Extract is a generic archive extraction mechanism.
It allows you to extract any archive file of the type .tar, .tar.gz, .gz, .Z, tar.bz2, .tbz, .bz2, .zip, .xz,, .txz, .tar.xz or .lzma without having to worry how it does so, or use different interfaces for each type by using either perl modules, or commandline tools on your system.
See the โHOW IT WORKSโ section further down for details.
$ae = Archive::Extract->new( archive => '/path/to/archive', [type => TYPE] )Creates a new โArchive::Extractโ object based on the archive file you passed it. Automatically determines the type of archive based on the extension, but you can override that by explicitly providing the โtypeโ argument, potentially by calling type_for().
Valid values for โtypeโ are:
Returns a โArchive::Extractโ object on success, or false on failure.
$ae->extract( [to => '/output/path'] )Extracts the archive represented by the Archive::Extract object to the path of your choice as specified by the โtoโ argument. Defaults to cwd().
Since โ.gzโ files never hold a directory, but only a single file; if the โtoโ argument is an existing directory, the file is extracted there, with its โ.gzโ suffix stripped. If the โtoโ argument is not an existing directory, the โtoโ argument is understood to be a filename, if the archive type is โgzโ. In the case that you did not specify a โtoโ argument, the output file will be the name of the archive file, stripped from its โ.gzโ suffix, in the current working directory.
extract will try a pure perl solution first, and then fall back to commandline tools if they are available. See the โGLOBAL VARIABLESโ section below on how to alter this behaviour.
It will return true on success, and false on failure.
On success, it will also set the follow attributes in the object:
$ae->extract_path โ This is the directory that the files where extracted to.$ae->files โ This is an array ref with the paths of all the files in the archive, relative to the โtoโ argument you specified.File::Spec->catfile( $to, $ae->files->[0] );
Note that all files from a tar archive will be in unix format, as per the tar specification.$ae->error([BOOL]) โ Returns the last encountered error as string. Pass it a true value to get the Carp::longmess() output instead.$ae->extract_path โ This is the directory the archive got extracted to. See โextract()โ for details.$ae->files โ This is an array ref holding all the paths from the archive. See โextract()โ for details.$ae->archive โ This is the full path to the archive file represented by this โArchive::Extractโ object.$ae->type โ This is the type of archive represented by this โArchive::Extractโ object. See accessors below for an easier way to use this. See the โnew()โ method for details.$ae->types โ Returns a list of all known โtypesโ for Archive::Extractโs โnewโ method.$ae->is_tgz โ Returns true if the file is of type โ.tar.gzโ.$ae->is_tar โ Returns true if the file is of type โ.tarโ.$ae->is_gz โ Returns true if the file is of type โ.gzโ.$ae->is_Z โ Returns true if the file is of type โ.Zโ.$ae->is_zip โ Returns true if the file is of type โ.zipโ.$ae->is_lzma โ Returns true if the file is of type โ.lzmaโ.$ae->is_xz โ Returns true if the file is of type โ.xzโ.$ae->bin_tar โ Returns the full path to your tar binary, if found.$ae->bin_gzip โ Returns the full path to your gzip binary, if found.$ae->bin_unzip โ Returns the full path to your unzip binary, if found.$ae->bin_unlzma โ Returns the full path to your unlzma binary, if found.$ae->bin_unxz โ Returns the full path to your unxz binary, if found.$ae->have_old_bunzip2 โ Older versions of /bin/bunzip2, from before the โbunzip2 1.0โ release, require all archive names to end in โ.bz2โ or it will not extract them. This method checks if you have a recent version of โbunzip2โ that allows any extension, or an older one that doesnโt.$ae->debug( MESSAGE ) โ This method outputs MESSAGE to the default filehandle if $DEBUG is true. Itโs a small method, but itโs here if youโd like to subclass it so you can do something else with any debugging output.type_for($archive)
Given an archive file name, it determines the type by parsing the file name extension. Used by โnew()โ when the โtypeโ parameter is not passed. Also useful when the archive file does not include a suffix but the file name is otherwise known, such as when a file is uploaded to a web server and stored with a temporary name that differs from the original name, and you want to use the same detection pattern as Archive::Extract. Example:
my $ae = Archive::Extract->new(
archive => '/tmp/02af6s',
type => Archive::Extract::type_for('archive.zip'),
);
โArchive::Extractโ tries first to determine what type of archive you are passing it, by inspecting its suffix. It does not do this by using Mime magic, or something related. See โCAVEATSโ below.
Once it has determined the file type, it knows which extraction methods it can use on the archive. It will try a perl solution first, then fall back to a commandline tool if that fails. If that also fails, it will return false, indicating it was unable to extract the archive. See the section on โGLOBAL VARIABLESโ to see how to alter this order.
โArchive::Extractโ trusts on the extension of the archive to determine what type it is, and what extractor methods therefore can be used. If your archives do not have any of the extensions as described in the โnew()โ method, you will have to specify the type explicitly, or โArchive::Extractโ will not be able to extract the archive for you.
โArchive::Extractโ can use either pure perl modules or command line programs under the hood. Some of the pure perl modules (like โArchive::Tarโ and Compress::unLZMA) take the entire contents of the archive into memory, which may not be feasible on your system. Consider setting the global variable $Archive::Extract::PREFER_BIN to 1, which will prefer the use of command line programs and wonโt consume so much memory.
See the โGLOBAL VARIABLESโ section below for details.
Older versions of /bin/bunzip2 do not support arbitrary file extensions and insist on a โ.bz2โ suffix. Although we do our best to guard against this, if you experience a bunzip2 error, it may be related to this. For details, please see the โhave_old_bunzip2โ method.
Set this variable to true to have all calls to command line tools be printed out, including all their output. This also enables Carp::longmess errors, instead of the regular carp errors.
Good for tracking down why things donโt work with your particular setup.
Defaults to false.
This variable controls whether errors encountered internally by โArchive::Extractโ should be carpโd or not.
Set to false to silence warnings. Inspect the output of the error() method manually to see what went wrong.
Defaults to true.
This variable controls whether โArchive::Extractโ should prefer the use of perl modules, or commandline tools to extract archives.
Set to true to have โArchive::Extractโ prefer commandline tools.
Defaults to false.
Maybe this module should use something like โFile::Typeโ to determine the type, rather than blindly trust the suffix.
Currently, โArchive::Extractโ does a chdir to the extraction dir before extraction, and a chdir back again after. This is not necessarily thread safe. See โrt.cpan.orgโ bug โ#45671โ for details.
Please report bugs or other issues to <bug-archive-extract AT rt.org>.
This module by Jos Boumans <kane AT cpan.org>.
This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.
perl v5.32.1 2021-09-25 Archive::Extract(3pm)
Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-10 08:29 @216.73.216.58
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)