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

I'm trying to do something which I'm sure is pretty simple, using Archive::Zip.

When you install a new FireFox, it has the default size of the history menu set to ten, which I like to change to something like 30 or 40.

To do this I grab a file called "browser.jar", unzip it, open "content/browser/browser.js" from the resulting folder, edit it, put it back, and zip it up again.

I spent about an hour last night trying to automate that with perl and Archive::Zip and gave up in frustration.

I guess maybe I don't understand Zip files? Should I be trying to extract a "member" file from "browser.jar"? Or using extractTree() because the file has a zipped directory structure? I don't understand the point of the "root" option in extractTree(), and when I try to extract the file 'content/browser/browser.js' directly from the Zip file, I get an error.

Here's what I want in pseudocode:

open '/path/to/browser.jar'; extract '/path/to/one/file/browser.js'; ## edit the file -- this bit I can do! put back '/path/to/one/file/browser.js'; close '/path/to/browser.jar';


($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Explain Archive::Zip to me please?
by johnnywang (Priest) on Sep 11, 2005 at 05:35 UTC
    You probably want something like the following:
    use strict; use Archive::Zip; #read the .jar file my $zip = Archive::Zip->new( 'helloworld.jar' ); # extract what you want my $file = "content/helloworld/helloworldOverlay.js"; my $member = $zip->extractMember($file); # do something with the file $file # now add it back $zip->removeMember($member); $zip->addFile($file); $zip->overwrite();
      Hi, thanks, that was almost exactly it. This does what I wanted:
      my $zip = Archive::Zip->new( 'browser.jar' ) || die "$!"; # extract what you want my $file = "content/browser/browser.js"; $zip->extractMember($file); # Edit the file as necessary, # which is at ./content/browser/browser.js # now add it back $zip->removeMember($file); $zip->addFile($file); $zip->overwrite();

      The extractMember function actually does extract a tree, as in, it extracts whatever directories are necessary to mirror that one file and its enclosing directories. So the file is there in the current working directory, inside the portion of the tree where it was located in the zip file.



      ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
      =~y~b-v~a-z~s; print
Re: Explain Archive::Zip to me please?
by Tanktalus (Canon) on Sep 11, 2005 at 05:17 UTC

    I'm sure you've heard this before - or at least have seen it before - but, what code have you tried, and what is the exact error message that you're getting?

    A zip file has "member" files that are archived/compressed within. Extracting a tree, without looking at the docs, sounds like asking for some directory, recursively, that is archived/compressed within a zip file. The "root" option sounds like the destination directory that will hold the unarchived/uncompressed copy of the recursive tree. But, still, it would be handy to see the code and the error message you're getting.

      I knew someone would post "show us your code"! But seriously, my problem isn't the code, it's that I don't understand the POD for Archive::Zip. Here's a perfect example.
      The "root" option sounds like the destination directory that will hold the unarchived/uncompressed copy of the recursive tree.

      Yes, it does. But it isn't. There's a totally separate destination option. It told me that the "root" option would be used to extract everything beginning with that string, so, as the folder I wanted to extract started with "c" (it was called "content"), I used "c" as the root and got a folder called "ontent". Maybe it's just me but I don't understand the documentation.



      ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
      =~y~b-v~a-z~s; print