Regex 有没有办法使用Perl压缩一组与正则表达式匹配的文件?

Regex 有没有办法使用Perl压缩一组与正则表达式匹配的文件?,regex,perl,zip,Regex,Perl,Zip,我想在一个目录中组织一些文件 在文件名中有一个模式,我想用它来组织它们,并且我已经能够使用regex通过这个motif来保存文件名 我想知道我是否可以更进一步,通过Perl脚本将这些文件压缩成zip文件 #!/usr/bin/perl use strict; use warnings; my $dir = "/home/user/Desktop/"; opendir DIR, $dir or die "cannot open dir $dir: $!"; my @file = readdir

我想在一个目录中组织一些文件

在文件名中有一个模式,我想用它来组织它们,并且我已经能够使用regex通过这个motif来保存文件名

我想知道我是否可以更进一步,通过Perl脚本将这些文件压缩成zip文件

#!/usr/bin/perl
use strict;
use warnings;

my $dir = "/home/user/Desktop/";
opendir DIR, $dir or die "cannot open dir $dir: $!";
my @file = readdir DIR;
closedir DIR;

my @files_to_compress;
my $counter = 0;

foreach my $dir_file (@file) {
  if ($dir_file =~ m/(^regex.*\.txt$)/) {
    #Save filenames
    $files_to_compress[$counter] = $dir_file;
    $counter++;
  }
}
通常使用Perl以以下方式编写:

my @files_to_compress = grep m/^regex.*\.txt$/, @file;
您将以这种方式创建Zip存档

use Archive::Zip qw( :ERROR_CODES );
my $dir = "/home/user/Desktop/";
my $zip = Archive::Zip->new();
opendir my $dh, $dir or die "cannot open dir $dir: $!";
$zip->addFile(grep m/^regex.*\.txt$/, readdir $dh);
$zip->writeToFileNamed('someZip.zip') == AZ_OK or die 'write error';
但要简单得多

use Archive::Zip qw( :ERROR_CODES );
my $dir = "/home/user/Desktop/";
my $zip = Archive::Zip->new();
$zip->addFile(<$dir/regex*.txt>);
$zip->writeToFileNamed('someZip.zip') == AZ_OK or die 'write error';
use Archive::Zip qw(:错误代码);
my$dir=“/home/user/Desktop/”;
我的$zip=Archive::zip->new();
$zip->addFile();
$zip->writeToFileNamed('someZip.zip')==AZ_OK或die'write error';

这里有一个建议,建议您在不使用Perl的情况下使用Perl,这对您来说太过分了:

find /home/user/Desktop/ -regex '.*/regex.*\.txt' -print | xargs zip archive.zip
您说过,“…通过Perl脚本将这些文件压缩成zip文件…”如果我理解正确,您希望每个
regex*.txt
文件都有一个zip文件。如果是这种情况,使用模块和
glob
)可能会有帮助:

use strict;
use warnings;
use Archive::Zip qw( :ERROR_CODES );

my $dir = '/home/user/Desktop';

for (<$dir/regex*.txt>) {
    my $zip     = Archive::Zip->new();
    my $zipFile = $zip->addFile($_);
    $zip->writeToFileNamed( $_ . '.zip' ) == AZ_OK or die "Unable to save file $_.zip\n";
}
使用严格;
使用警告;
使用存档::Zip qw(:错误代码);
my$dir='/home/user/Desktop';
对于(){
我的$zip=Archive::zip->new();
my$zipFile=$zip->addFile($);
$zip->writeToFileNamed($\.'.zip')==AZ\u确定或死“无法保存文件$\.zip\n”;
}

您当前使用什么压缩文件?如果是gzip或类似的东西,我需要创建.zip文件。这里有一个zip存档模块:我想你需要更好地解释你想做什么。您是想用等效的zip文件替换目录,还是只想创建一个包含给定目录中所有
regex…txt
文件的zip库。您应该注意,您的正则表达式匹配任何开头为
regex.tx
,后面是可选的
t
,以及之后的任何内容,因此
regexaaaaaa.tx.a.a.01
将匹配得很好。@Borodin,
应该是
$
。谢谢你接电话。至于我想做什么,我想得到所有的regex..txt文件并将它们添加到一个zip文件中。这不会替换现有的目录/组织结构。我只需要压缩这些文件以进行传输。这不是答案,所以您认为OP的regex
^regex.*.txt?
与此无关?OP要求一个Perl解决方案。不,我不认为这无关紧要;你为什么这么说?因为你的
find
命令做了一些非常不同的事情。另一方面,如果OP真正需要的就是
zip archive.zip regex*.txt
,为什么要用几十行Perl代码替换它呢?(学习是一个公平的答案,但如果这不是目标的话?)。此解决方案不适用于最初发布的正则表达式,因此@Borodin发表了评论。
use strict;
use warnings;
use Archive::Zip qw( :ERROR_CODES );

my $dir = '/home/user/Desktop';

for (<$dir/regex*.txt>) {
    my $zip     = Archive::Zip->new();
    my $zipFile = $zip->addFile($_);
    $zip->writeToFileNamed( $_ . '.zip' ) == AZ_OK or die "Unable to save file $_.zip\n";
}