Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Zip only files, not directory hierarchy.

by linuxer (Curate)
on May 14, 2014 at 18:39 UTC ( #1086070=note: print w/replies, xml ) Need Help??


in reply to Zip only files, not directory hierarchy.

Have a look at the perldoc of IO::Compress::Zip. It mentions an option FilterName, which could be used.

Quoting:

...

Filtername => sub { ... }

... For example, the code below shows how FilterName can be used to remove the path component from a series of filenames before they are stored in $zipfile.

sub compressTxtFiles { my $zipfile = shift ; my $dir = shift ; zip [ <$dir/*.txt> ] => $zipfile, FilterName => sub { s[^$dir/][] } ; }
Update

Did a test; it works ;-)

#! /usr/bin/env perl use strict; use warnings; use File::Basename qw( basename ); use IO::Compress::Zip qw( zip $ZipError ); my @files = glob( 'data/*/*.dat' ); my $zipfile = './zipped.zip'; print "zipping $_\n" for @files; zip [ @files ] => $zipfile, FilterName => sub { $_ = basename($_); }; __END__ # source files data/foo/FOO.dat data/bar/BAR.dat # files in zip (according to 'unzip -l') FOO.dat BAR.dat

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1086070]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2020-06-07 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (42 votes). Check out past polls.

    Notices?