Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

changing filenames using Archive::Zip

by geoffcox (Initiate)
on May 17, 2009 at 20:15 UTC ( [id://764550]=perlquestion: print w/replies, xml ) Need Help??

geoffcox has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have the code below which unzips a series of zip files. I would like to be able to change the names of the Word files produced to more systematic names. Say doc-1.zip produces fred.doc and doc-2.zip produces jane.doc I would like to change fred.doc to doc-1.doc and jane.doc to doc-2.doc You can that see I want to keep the number which the original zip file had. This is because I want to replace a whole series of zip files on a website with the Word files inside them. Ideas please?! Thanks Geoff
#!perl use warnings; use strict; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use File::Find; my $dir = 'c:/a-temp'; my @zips; find sub { -d and return; /\.zip$/ and push @zips, [$File::Find::dir, $_]; }, $dir; my $zip = Archive::Zip->new; for (@zips) { chdir $_->[0] or die "can't chdir to $_->[0]: $!"; $zip->read($_->[1]) == AZ_OK or die "can't read _->[1]"; $zip->extractTree == AZ_OK or die "can't extract $_->[1]"; }

Replies are listed 'Best First'.
Re: changing filenames using Archive::Zip
by grep (Monsignor) on May 17, 2009 at 21:58 UTC
    Use the members method to get each of the zip members one at a time. Then use extractMember to extract and rename.
    use warnings; use strict; use Archive::Zip; my $zip = Archive::Zip->new('foo.zip'); my $file_counter = 0; foreach my $member ( $zip->members ) { my $file_name = $member->fileName; my $extracted_file_name = 'file-'. $file_counter++ . '.foo'; print "extracting $file_name as $extracted_file_name\n"; $zip->extractMember($member, $extracted_file_name); }
    grep
    One dead unjugged rabbit fish later...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://764550]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-24 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found