#!/usr/bin/env perl # Quinten Yearsley (qyearsley@gmail.com) # This script is a very simple substitute for 'aunpack': # It is inflexible and only performs the task of unpacking # archives; nothing else. # use strict; use warnings; use File::Copy; use File::Basename; use File::Spec::Functions; # A hash mapping filename extension to extraction program. # This will be used to determine what program to try to use. our %extractors = ( "tar.gz" => "tar xzf", "tgz" => "tar xzf", "tar.bz2" => "tar xjf", "tbz2" => "tar xjf", "zip" => "unzip", "rar" => "unrar e", "7z" => "7z e", "gz" => "gunzip", "bz2" => "bunzip2", "tar" => "tar xf", "Z" => "uncompress", ); # Get the filename of the archive. if (scalar @ARGV == 0) { print "Usage: $0 \n"; exit 1; } my $archive = $ARGV[0]; # Get the appropriate extraction program based on the filename. my $extractor; for my $extension (keys %extractors) { if ($archive =~ /.*$extension/) { $extractor = $extractors{$extension}; print "Using '$extractor' to extract '$archive'...\n"; last; } } unless ($extractor) { print "No suitable extractor was found based on the filename extension.\n"; exit 1; } # Extract into a (possibly temporary) directory. my $extract_dir = "$archive.extract"; mkdir $extract_dir; copy $archive, $extract_dir; chdir $extract_dir; my $archive_basename = basename $archive; system "$extractor $archive_basename"; unlink $archive_basename; # If there was only one file/dir, move it into the original dir and clean up. my @files = glob '*'; my $num_files = scalar @files; if ($num_files == 1) { my $file = $files[0]; move $file, catfile(updir, $file); chdir updir; rmdir $extract_dir; print "Extracted '$file' to the current directory.\n"; } else { print "Extracted $num_files items to '$extract_dir'.\n"; }