Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Edit Manifest file in jar archive

by jonnyfolk (Vicar)
on Jan 10, 2010 at 23:07 UTC ( #816657=perlquestion: print w/replies, xml ) Need Help??
jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to edit the MANIFEST.MF file in a .jar application using Archive::Zip

I'm not sure if I am going about it the right way. I'm reading and editing the file, printing the revised file externally. I've made a few attempts to get the new file to replace the file in the app but without success, the new app being identical to the original!

#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI ':standard'; print "Content-type: text/html\n\n"; my $file = 'MANIFEST.MF'; use Archive::Zip; use Archive::Zip::MemberRead; my $zip = Archive::Zip->new("MyWeb.jar"); my $fh = Archive::Zip::MemberRead->new($zip, "META-INF/MANIFEST.MF" +); open FH, '>', $file or die $!; while (defined(my $line = $fh->getline())) { chomp $line; if ($line =~ /id=(.*)$/) { $line =~ s/$1/1012/; print FH "$line\n"; print "$line\n"; } else { print FH "$line\n"; print "$line\n"; } } my $member1 = $zip->replaceMember( 'MANIFEST.MF', $file ); $zip->overwriteAs('file.jar');

Replies are listed 'Best First'.
Re: Edit Manifest file in jar archive
by almut (Canon) on Jan 10, 2010 at 23:45 UTC
    $zip->replaceMember( 'MANIFEST.MF', $file );

    Maybe try instead

    $zip->replaceMember( 'META-INF/MANIFEST.MF', $file );

    Also, it seems (though the docs are somewhat ambiguous) the method wants a member object for the new member, not a file name, so try  (untested)

    ... my $new_member = Archive::Zip::Member->newFromFile($file); $zip->replaceMember( 'META-INF/MANIFEST.MF', $new_member);

    If that doesn't work either, what's the return value of replaceMember()? (according to the docs, it returns undef on error)

      This is almost right, but then the zip file will contain a file 'MANIFEST.MF' instead of 'META-INF/MANIFEST.MF'. I think updateMember will work, though. Also, you need to close FH;


      close FH or die "error writing $file: $!\n"; $zip->updateMember('META-INF/MANIFEST.MF', $file) or die "updateMember";

      Thanks very much for your input. This is definitely on the right lines - it is getting the required file into the app but in the wrong place!!

      I have an empty META-INF directory (so Manifest file is being deleted, but alongside that (in the main directory) the MANIFEST.MF file. I will now need to discover how to move the file into the directory. Thank you once again.

Re: Edit Manifest file in jar archive
by gmargo (Hermit) on Jan 10, 2010 at 23:48 UTC

    You're doing this as a CGI? Does the web server have write permission?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://816657]
Approved by biohisham
[marto]: Sane Dots, chapter 13: ITIL never work :P
[marto]: sorry, bad pun of it'll :P
[Corion]: :-D
[Corion]: I think that ITIL is mostly to prevent people from doing harm to a system that basically runs unattended :)
[Corion]: Maybe you could reduce ITIL to a computer network, a man and a dog. The network runs the computations. The man is there to feed the dog. The dog is there to prevent the man from accessing the computers.
[marto]: from experience of this side of the fence, it's all about proving to people that you are ITIL compliant, then the client ignores logical processes anyway :P

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2017-01-24 10:33 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (203 votes). Check out past polls.