Skip to content

Perl script to unzip files

G9th November 2016

CNo Comments


A Perl script that will unzip files by extracting contents and saving them to a new directory named after the source zip file.

The following Perl script can be used to unzip files. The script relies on Archive::Zip (which will need to be installed) and File::Basename (which comes with Perl).

To activate, open cmd and enter perl \path\to\ \path\to\

use strict;
use warnings;

package TidyUnzipper;

use strict;
use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
use Exporter 'import';

our @EXPORT = qw(unzip);

# For Command-Line Options
my ($zip_file, $out_file, $filter) = @ARGV;
unzip($zip_file, $out_file, $filter) if scalar @ARGV;


# Make file name string
my $shortname = fileparse($zip_file, qr/\\E/);

# Unzip file into a new directory called zip_temp
sub Unzipper {
	my ($zip_file, $out_file, $filter) = @_;
	my $zip = Archive::Zip->new();
	unless ( $zip->read( $zip_file ) == AZ_OK ) { 
		die 'Read error: The file could not be unzipped';
  $zip->extractTree( '', 'zip_temp/' );	

# Rename zip_temp
	rename("zip_temp", $shortname);

# Explanation
use File::Basename;
use strict;

	# Get full path in $path
	my $filename = basename($zip_file);

	# Print statement
print "The file $filename has been unzipped.\n";

This code has been published on GitHub Gist and is loosely based on



No comments have yet been submitted. Be the first!

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.