Zip all .tif, .tiff, or .eps inside a directory and any of it's sub directories. Do not zip any other file types. I did this on a directory with over 160GB of files (varying 5MB to 65MB filesizes) and it ran overnight without a hitch. I'm sure it can be improved, but do not have time to mess with it. I thought it might be helpful to someone out there.
#!C:\Perl\bin\perl.exe -w use File::Find; use Archive::Zip qw( :ERROR_CODES :CONSTANTS); use Cwd; my $dir = cwd(); print "Zipping .tif, .tiff, and .eps files...\n$dir\n\n"; find(\&edits, $dir); sub edits() { my $filename = $_; if (-f and ( /\.tiff?$/ or /\.eps$/ )) { print "$File::Find::name\n"; my $barefile = $filename; substr( $barefile, rindex( $barefile, '.' ) ) = ''; if ($barefile ne '') { my $zip = Archive::Zip->new(); my $member = $zip->addFile($filename); if ($zip->writeToFileNamed("$barefile.zip") == AZ_OK) { unlink($File::Find::name) } else { die 'Error writing file'; } } } }
2005-12-07: Updated regular expression per Celada's input below.

Replies are listed 'Best First'.
Re: Zip all .tif and .eps files
by Celada (Monk) on Dec 06, 2005 at 19:44 UTC

    Be careful with those regular expressions. In fact this will zip all files that have either ti or ep almost anywhere in their names. Your regular expression, /.tif?/ matches any character (.) followed by ti (ti) followed maybe by f (f?). This reduces to checking for ti anywhere except right at the start.

    Maybe you are lucky and no unintended names matched, but you might want to take a look!

    What you wanted is /\.tiff?$/ (.tif or .tiff at the end of the filename only -- adding support for .tiff since TIFF is the name of the file format, not TIF. Using .tif in the name is an unfortunate and obsolete legacy from CP/M.)

    I notice you check for errors from writeToFileNamed, that's an excellent idea, often missing from quick single purpose scripts.

      Thanks for the input. I am still fairly new to regular expressions. 95% of the files were .tif or .eps. All of the other files were unaffected as far as I could tell. I didn't care too much because all it would have done was zip the file, but I will modify the script per your changes. Thanks again!
      Thanks buttroast
Re: zip_all for .tif & .eps
by Scott7477 (Chaplain) on Dec 30, 2005 at 14:09 UTC
    What was the resulting size of the zipped file?
      It was not all zipped up into a single file. What I did was zip up each file individually, one zip file per image. We still needed to be able to link to an individual file.
      Thanks buttroast