#!/usr/bin/perl -w # Local UnZip Utility 2010 expandinghead.net # Opens .zip, .gz, .tar, and .tar.gz zipped files. # The archived file you wish to open must be in # the same directory this script is installed in. # Option to list the files in a zip, tar or tar.gz archive and # open the zip, tar or tar.gz files in a new or existing sub-directory. # tar and tar.gz archived files, by default, will open in the # current directory if a name has not been entered into # the "Open in" field. If it does not exist, the script # will attempt to make it. # You may need to make this sub manually if you get # a warning message that it could not be created. # gz files are opened in the current directory # Be careful when opening a compressed file, it can # contain several files and directories. # Requires CGI.pm and Archive::Zip. # Chmod this script to 755 ### # This software is provided "as is" and any express or # implied warranties, including, but not limited to, the implied warranties # of merchantability and fitness for a particular purpose are disclaimed. # In no event shall the author be liable for any direct, indirect, # incidental, special, exemplary, or consequential damages ### my ($url, $dir, $mode, $effect, $command, $pid, $zip, @members, $members); use CGI qw(:standard); use strict; $url = param('url') || ''; $dir = param('dir') || ''; $mode = param('mode') || 'list'; $effect = 'nohighlight autoactivate selected'; print header,start_html(-title =>'UnZip Utility', -author =>'expandinghead.net', -bgcolor => '#ffffff', -text => '#000000'), p,h1('UnZip Utility'),p,'Enter the name of the .zip, .gz, .tar or .tar.gz file you wish to open.',hr; print start_form,table(Tr([ td(['File Name ',textfield(-size=>50,-name=>'url',-default=>'',$effect)]), td(['Open in ',textfield(-size=>50,-name=>'dir',-default=>'',$effect)]), td([radio_group(-name=>'mode',-values=>['list','open'], -default=>'list',)]), td([submit(undef,' Submit '),reset]), ])),end_form; if ($url ne '' ) { if (! -e $url) { print "
$url not found, did you enter the correct file name
"; } elsif ($url =~ /\.tar\.gz$/i) { if ($mode eq 'open') { if ($dir ne '') { if (! -d $dir) { &mk_dir; } $command = "tar --directory=$dir -xvzf $url"; } else { $command = "tar -xvzf $url"; } } else { $command = "tar -tvzf $url"; } if ($pid = open(README, "$command |")) { if ($mode eq 'open') { print "The following files have been extracted:

"; } else { print "The following files were found in the archive

"; } while() { print "$_
"; } close(README); } else { print "Error! Cannot open $url : $!.
"; } } elsif ($url =~ /\.tar$/i) { if ($mode eq 'open') { if ($dir ne '') { if (! -d $dir) { &mk_dir; } $command += "tar --directory=$dir -xvf $url"; } else { $command = "tar -xvf $url"; } } else { $command = "tar -tvf $url"; } if ($pid = open(README, "$command |")) { if ($mode eq 'open') { print "The following files have been extracted:

"; } else { print 'The following files were found in the archive',br,br; } while() { print "$_
"; } close(README); } else { print "Error! Cannot open $url : $!.
"; } } elsif ($url =~ /\.gz$/i) { $command = `gzip -dv $url`; $url =~ s/\.gz$//i; if (-e $url) { print "$command
"; print "$url has been decompressed
"; } else { print "
Check directory for results
"; } } elsif ($url =~ /\.zip$/i) { use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); $zip = Archive::Zip->new(); if ($mode ne 'open') { print 'The following files were found in the archive',br,br; die 'read error' unless $zip->read($url) == AZ_OK; @members = $zip->memberNames(); foreach $members (@members) { print "$members
"; } @members=(); } elsif ($dir ne '') { if (! -d $dir) { &mk_dir; } $command = `unzip -d $dir $url`; print "The following files have been extracted to $dir:

", "
$command

End of Action"; } else { $command = `unzip $url`; print "The following files have been extracted to the current directory :

", "
$command

End of Action"; } } else { print "The file must end in .zip, .gz, .tar or .tar.gz
"; } } else { print hr,p,b,'The compressed file you wish to open must be in the directory this script is running in.', p,b,' Use the list option to examine the contents of zip, tar or tar.gz archives before extracting.', p,b,' A sub-directory can be specified for .tar or tar.gz files to be opened in.', ' This script will attempt to creat a new sub-directory if it does not exist.', ' If left blank and a sub-directory is not specified, the file will be opened in the current directory.',br; } print p,hr,p,h5('expanding head'),end_html; sub mk_dir { $command = `mkdir $dir; chmod 755 $dir 2>&1`; if (!$command) { print "The Sub-Directory $dir has been created successfully.
"; } else { print "Warning : The Sub-Directory could not be created
$command
"; } }