如何使用 Perl 列出 ZIP 文件的内容?

发布于 2024-11-28 05:25:45 字数 509 浏览 0 评论 0原文

我的目标就是标题中所说的。

我有以下代码行,它只是打印出 [view archive] ,当我单击它时,浏览器只会下载 zip 文件。

print "\<a href=\"http:\/\/intranet.domain.com\/~devcvs\/view-file.cgi?file=$reviewdata{'document'}&review_id=$reviewdata{'id'}\"\>[view archive]\<\/a\>\n";

我想做的是在页面上的任何位置列出此 zip 文件中包含的文件,例如在该链接链接到的下面甚至新页面上,并将文件名作为参数。

我相信一旦完成此操作,浏览器就应该处理剩下的事情,只需单击这些文件并在浏览器中查看它们,因为它们将是 pdf 和 html 文件,我预计不会有任何问题。

我确信有一个模块可以做到这一点,但我不确定如何使用它来实现我的目标。

非常感谢任何帮助。

What I am aiming to do is pretty much what it says in the title.

I have the following line of code which simply prints out [view archive] and when I click it the browser just downloads the zip file.

print "\<a href=\"http:\/\/intranet.domain.com\/~devcvs\/view-file.cgi?file=$reviewdata{'document'}&review_id=$reviewdata{'id'}\"\>[view archive]\<\/a\>\n";

What I would love to do is to list the files contained within this zip file anywhere on the page, e.g. just underneath or even a new page which this link links to and takes the filename as a parameter.

I believe once this is done the browser should take care of the rest in terms of just clicking these files and viewing them in the browser as they will be pdfs and html files which I don't foresee any problems with.

I am sure there is a module that does this but I am unsure of how to accomplish my goal using it.

Any help is much appreciated.

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

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

发布评论

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

评论(2

第七度阳光i 2024-12-05 05:25:45

看一下 Archive::Zip

use strict;
use warnings;

use Archive::Zip qw/ :ERROR_CODES :CONSTANTS /;

my $zipFile = 'someZip.zip';

my $zip = Archive::Zip->new();

unless ( $zip->read( $zipFile ) == AZ_OK ) {  # Make sure archive got read

    die 'read error';
}

my @files = $zip->memberNames();  # Lists all members in archive

print $_, "\n" for @files;

Have a look at Archive::Zip :

use strict;
use warnings;

use Archive::Zip qw/ :ERROR_CODES :CONSTANTS /;

my $zipFile = 'someZip.zip';

my $zip = Archive::Zip->new();

unless ( $zip->read( $zipFile ) == AZ_OK ) {  # Make sure archive got read

    die 'read error';
}

my @files = $zip->memberNames();  # Lists all members in archive

print $_, "\n" for @files;
沫雨熙 2024-12-05 05:25:45

使用 Archive::Zip 肯定会让代码变得更简单,如果您要广泛处理 zip 文件,您可能应该安装该模块。

然而,对于那些不想安装任何东西的人来说,有一种方法可以仅使用核心模块 IO::Uncompress::Unzip 来列出 zip 文件的内容(已经是任何标准 Perl 发行版的一部分) )。

use strict;
use warnings;

use IO::Uncompress::Unzip qw($UnzipError);

my $zipFile = '/path/to/zipfile.zip';

my $u = IO::Uncompress::Unzip->new($zipFile)
    or die "Error: $UnzipError\n";

my $status;
for ($status = 1; $status > 0; $status = $u->nextStream()) {
    my $header = $u->getHeaderInfo();
    my $zippedFile = $header->{Name};
    if ($zippedFile =~ /\/$/) {
        last if $status < 0;
        next;
    }
    print "$zippedFile\n";
}

Using Archive::Zip certainly makes the code easier, and you should probably install that module if you are going to work extensively on zip files.

However, for those who prefer not to install anything, there is a way to list the content of a zip file just using the core module IO::Uncompress::Unzip (already part of any standard Perl distribution).

use strict;
use warnings;

use IO::Uncompress::Unzip qw($UnzipError);

my $zipFile = '/path/to/zipfile.zip';

my $u = IO::Uncompress::Unzip->new($zipFile)
    or die "Error: $UnzipError\n";

my $status;
for ($status = 1; $status > 0; $status = $u->nextStream()) {
    my $header = $u->getHeaderInfo();
    my $zippedFile = $header->{Name};
    if ($zippedFile =~ /\/$/) {
        last if $status < 0;
        next;
    }
    print "$zippedFile\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文