Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Manipulating ZIP files in Perl

by John M. Dlugosz (Monsignor)
on Jan 05, 2003 at 01:56 UTC ( #224357=perlquestion: print w/replies, xml ) Need Help??

John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I want to rename items in a zipfile. More directly, I want to add a subdirectory to the filenames recorded in the zip that was not actually present when the ZIP utility packed up the files.

The info-zip command line tool doesn't have any option for adding files under a different (qualified) name than what it finds. I understand there are Zipper tools written in Perl that offer the same functionality. Does anything like that have this feature, or is mature enough that I could use it instead and tinker with it to add such a feature?


Replies are listed 'Best First'.
(bbfu) (Archive::Zip) Re: Manipulating ZIP files in Perl
by bbfu (Curate) on Jan 05, 2003 at 04:02 UTC

    Unless I'm misunderstanding what you want, Archive::Zip should do what you ask. The following snippet will add the file s2.tst to the zip file, creating tst2/ if it doesn't already exist in the archive.

    #!/usr/bin/perl use warnings; use strict; use Archive::Zip qw( :ERROR_CODES ); my $zip = Archive::Zip->new(); $zip->read('') == AZ_OK or die "read error\n"; $zip->addFile('tst2/s2.tst'); $zip->overwrite() == AZ_OK or die "write error\n";

    According to the docs, Archive::Zip can also rename already-existing items in a Zip file.

    Update: Looking at your question again, it seems I may have misunderstood it. :) From the doc's, it seems like Archive::Zip should still be able to do what you want, using the replaceMember() method, but my initial tests don't seem to be working. I'll play with it some more.

    Update2: Ah, got it. I think this is what you want:

    #!/usr/bin/perl use warnings; use strict; use Archive::Zip qw( :ERROR_CODES ); my $zip = Archive::Zip->new(); $zip->read('') == AZ_OK or die "read error\n"; my $m1 = $zip->memberNamed('tst2/s2.tst'); $m1->fileName('foo/tst2/s2.tst'); # Rename the file, adding +a subdir $zip->overwrite() == AZ_OK or die "Write error\n";

    Black flowers blossum
    Fearless on my breath

      Do you know if this solution can work with archives more than 4GB in size (zip64) ?
      thank you
Re: Manipulating ZIP files in Perl
by hardburn (Abbot) on Jan 06, 2003 at 15:18 UTC

    Kinda a side point of the parent node, and doesn't necessarily deal with Perl, but I thought of this while reading it:

    Does anyone know of a tool that can automatically check for "exploding archives" (archive files that unload everything into the current directory instead of creating a single subdirectory), pack the archive into a subdir, and release the archive from there?

      I made one for you, and posted it here (under the Code Catacombs). :)

      Black flowers blossum
      Fearless on my breath

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://224357]
Approved by gjb
Front-paged by Courage
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2020-02-24 10:11 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (104 votes). Check out past polls.