如何从存档中解压缩文件而忽略文件名?

如何从存档中解压缩文件而忽略文件名?

我有几个 ZIP 和 RAR 存档,其中存档内的文件名是乱码的,并且它们包含无效的文件系统字符,例如 ?-s *-s 或 !-s 或非常长的文件和目录名,这些名称会混淆常用的存档工具,或者根本无法识别创建文件。因为只有内容很重要,所以我只想将这些档案中的文件提取到平面结构中的单个目录中,并使用 file0、file1、file2 等通用名称...最简单的方法是什么?

答案1

Daniel S. Sterling 编写的 Perl 脚本位于https://gist.github.com/eqhmcow/5389877(参考自IO::解压缩::解压缩)看起来它几乎可以满足您的需要。

  • 将第 46 行更改为:my $status, $filenumber = 0;
  • 注释行 52-54(放在#每行的开头)
  • 将第 61 行更改为my $destfile = "file" . $filenumber++;

经过这些修改后的整个脚本如下所示以供参考:

#!/usr/bin/perl
# example perl code, this may not actually run without tweaking, especially on Windows

use strict;
use warnings;

=pod
IO::Uncompress::Unzip works great to process zip files; but, it doesn't include a routine to actually
extract an entire zip file.
Other modules like Archive::Zip include their own unzip routines, which aren't as robust as IO::Uncompress::Unzip;
eg. they don't work on zip64 archive files.
So, the following is code to actually use IO::Uncompress::Unzip to extract a zip file.
=cut

use File::Spec::Functions qw(splitpath);
use IO::File;
use IO::Uncompress::Unzip qw($UnzipError);
use File::Path qw(mkpath);

# example code to call unzip:
unzip(shift);

=head2 unzip
Extract a zip file, using IO::Uncompress::Unzip.
Arguments: file to extract, destination path
    unzip('stuff.zip', '/tmp/unzipped');
=cut

sub unzip {
    my ($file, $dest) = @_;

    die 'Need a file argument' unless defined $file;
    $dest = "." unless defined $dest;

    my $u = IO::Uncompress::Unzip->new($file)
        or die "Cannot open $file: $UnzipError";

    my $status, $filenumber = 0;
    for ($status = 1; $status > 0; $status = $u->nextStream()) {
        my $header = $u->getHeaderInfo();
        my (undef, $path, $name) = splitpath($header->{Name});
        my $destdir = "$dest/$path";

        # unless (-d $destdir) {
        #     mkpath($destdir) or die "Couldn't mkdir $destdir: $!";
        # }

        if ($name =~ m!/$!) {
            last if $status < 0;
            next;
        }

        my $destfile = "file" . $filenumber++;
        my $buff;
        my $fh = IO::File->new($destfile, "w")
            or die "Couldn't write to $destfile: $!";
        while (($status = $u->read($buff)) > 0) {
            $fh->write($buff);
        }
        $fh->close();
        my $stored_time = $header->{'Time'};
        utime ($stored_time, $stored_time, $destfile)
            or die "Couldn't touch $destfile: $!";
    }

    die "Error processing $file: $!\n"
        if $status < 0 ;

    return;
}

1;

相关内容