Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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');

Comment on Edit Manifest file in jar archive
Download Code
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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-12-21 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls