http://www.perlmonks.org?node_id=816657

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;

      So,

      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?