【问题标题】:How to unzip more than 1 files from more than one sub-folders using perl script?如何使用 perl 脚本从多个子文件夹中解压缩多个文件?
【发布时间】:2014-10-28 06:04:06
【问题描述】:

我在父目录中有多个子文件夹,每个子文件夹有 10 多个 zip 文件。例如。 父目录:/parentdir 子目录:/parentdir/2011-01,2011-02... 直到 2014-06,依此类推.. 每个月。

每个子目录:file1.zip, file2.zip, ... file10.zip

我想使用 perl 脚本解压缩同一子文件夹中的所有文件,并在解压后删除 .zip 文件或将 file1.zip 解压缩到 file1

您能帮忙在 unix 机器上使用 PERL 脚本来实现吗?

有可能使用

use strict;
use warnings;
use IO::Uncompress::Unzip qw(unzip $UnzipError);

my $zipfile = $ARGV[0];
my $u = new IO::Uncompress::Unzip $zipfile
or die "Cannot open $zipfile: $UnzipError";

die "Zipfile has no members"
 if ! defined $u->getHeaderInfo;
}

但它提供了仅解压缩/解压缩一个文件的选项

米勒的回答更新

使用http://pkgs.org/centos-6/epel-i386/perl-Path-Class-0.25-1.el6.noarch.rpm.html 并安装了 rpm for Path::Class for per to recon.,现在仍然无法解压缩子文件夹中的文件:IO::Uncompress::Unzip: input parameter not a filename, filehandle , unzipper.pl 第 13 行的数组引用或标量引用

我有一个父文件夹 api_parent_folder 在子文件夹中 - 2014-01、2014-02 ...

在上述脚本中,我已将 /parentdir 更改为 /mnt/api_parent_folder

我一直在下面..

现在仍然无法解压缩子文件夹中的文件:IO::Uncompress::Unzip: unzipper.pl 第 13 行的输入参数不是文件名、文件句柄、数组引用或标量引用

【问题讨论】:

    标签: perl


    【解决方案1】:

    使用Path::Class 遍历目录结构:

    use strict;
    use warnings;
    
    use Path::Class;
    use IO::Uncompress::Unzip qw(unzip $UnzipError);
    
    my $dir = dir('/parentdir');
    
    while ( my $subdir = $dir->next ) {
        next unless $subdir->is_dir();
        while ( my $zipfile = $subdir->next ) {
            my $u = IO::Uncompress::Unzip->new("$zipfile")
                or die "Cannot open $zipfile: $UnzipError";
    
            die "Zipfile has no members" if !defined $u->getHeaderInfo;
        }
    }
    

    【讨论】:

    • 使用 pkgs.org/centos-6/epel-i386/… 并安装了 Path::Class 的 rpm 以进行每次侦察,
    • @AruKaya 通过简单地对 $zipfile 变量进行字符串化来修复。 my $u = IO::Uncompress::Unzip->new("$zipfile")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2015-01-10
    相关资源
    最近更新 更多