Perl:从 Java .jar 文件读取 MANIFEST.MF 文件

发布于 2024-09-07 06:31:38 字数 598 浏览 3 评论 0原文

我试图弄清楚如何从 perl 中的 java jar 文件读取 META-INF/MANIFEST.MF 文件。我正在尝试使用 Mail::Header 来分隔中的属性清单。如果已经从 jar 中提取了清单文件,则效果很好,但我试图弄清楚如何将清单提取到内存中,然后立即放入 Header 对象中。这就是我到目前为止所拥有的:

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

我无法弄清楚使用哪个构造函数和/或哪个提取/读取函数来读取 $manifest 文件句柄。 (我是 Perl 新手)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-09-14 06:31:39

MemberRead 有一个非常愚蠢的 API。它应该提供一个真实句柄,或者至少以兼容的方式模仿IO::File(因此我们将其传递给Mail::Header) > 构造函数或至少能够调用 getlines 方法),但没有。

我们可以通过将文件内容临时存储在数组中来解决它的不兼容性。

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

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文