views:

44

answers:

1

I'm trying to figure out how to read the META-INF/MANIFEST.MF file form a java jar file in perl. I'm attempting to use Mail::Header in order to separate the properties in the manifest. This works fine if the manifest file is already extracted from the jar, but I'm trying to figure out how to extract the manifest into memory and then immediately put into a Header object. This is what I have so far:

my $jarFile = "MyJar.jar";
my &jar = Archive::Zip->new($jarFile);

my $manifest = Archive::Zip::MemberRead->new($jar, "META-INF/MANIFEST.MF");

my $header = Mail::Header->new;
$header->read(????);

print $header->get("Class-Path");

I can't figure out which constructor and/or which extract/read function to use to read the $manifest file handle. (I'm new to perl)

+1  A: 

MemberRead has a really dumb API. It should give a real handle, or at least mimic IO::File in a compatible fashion (so we pass it to the Mail::Header constructor or at least be able to call the getlines method), but doesn't.

We can work around its incompatibility by storing the file content temporarily in an array.

my @lines;
{
    my $handle = Archive::Zip->new($jar_file)->memberNamed('META-INF/MANIFEST.MF')->readFileHandle;
    while (defined(my $line = $handle->getline)) { # even $_ doesn't work!! what a piece of camel dung
        push @lines, $line;
    }
}

my $headers = Mail::Header->new([@lines]);
print $headers->get('Class-Path');
daxim
Thanks daxim. Works for me. The only other problem I had with what I was trying to do was a parsing issue with the list of *.jar files. The manifest adds extra spaces in random spots of the list so this is what I did to get a list of jar files separated by ':'.join(".jar:", split(".jar", join("", split(" ", $headers->get("Class-Path"))))).".jar";
Nick