Command Line Zip for Windows

Windows doesn't have a command line utility for compressing files (that I know of), and I had to come up with a way to automate some file transfers today. So I whipped up a tiny Perl script that will zip up a file. I figure someone else might need it. (Or someone might know how to do this in one line.) You'll need the Archive::Zip module, and the following code—which is pretty much right out of the documentation:
# Grab the incoming file
my $argv = join(' ', @ARGV) or die "Usage: zipme.pl [file location]\n";

# Grab the file name
my($dir, $file) = $argv =~ m/(.*\\)(.*)$/;

# Create a Zip file
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
my $zip = Archive::Zip->new();

# Add the file
my $file_member = $zip->addFile($argv, $file);

# Save the Zip file
unless ( $zip->writeToFileNamed($argv.'.zip') == AZ_OK ) {
    die 'couldn\'t zip';
}
Save this code as zipme.pl, and you'll be set. Pass in a filename, and you'll get a compressed file of the same name plus the .zip extension. So:

C:\>perl zipme.pl C:\path\to\giant.file

will give you C:\path\to\giant.file.zip. It works well with Windows batch files, and will save me a bunch of bandwidth.
« Previous post / Next post »

Comments

Sorry, had a couple of minutes to kill...

perl -MArchive::Zip -e "qq(@ARGV) =~ m/(.*[\\\/])?(.*)$/ && ($_ = Archive::Zip->new) && $_->addFile(qq(@ARGV), $2) && $_->writeToFileNamed(qq(@ARGV.zip)) && die qq(couldn't zip)" %1 %2 %3 %4 %5 %6 %7 %8 %9
ha, awesome. Thanks, chris.
I highly recommend cygwin.
Thanks, Larry. I use a couple cygwin components in places--especially for secure file transfers--but I haven't seen a need to install everything. I guess if installed the complete cygwin package it would include a zip/tar command line tool, eh? I'll take a look at it next time something like this comes up.
Hi! You're reading a single post on a weblog by Paul Bausch where I share recommended links, my photos, and occasional thoughts.
×

Search Results

No emoji found