我有以下文件:9001_20150918165942_00085.xml.gz
我想用 gunzip 压缩“9001_20150918165942_00085.xml”,我该如何在 perl 中做到这一点。
问候,Bhushan
答案1
简单的方法system
#!/usr/bin/env perl
system("gunzip 9001_20150918165942_00085.xml.gz")
或者
#!/usr/bin/env perl
system("gunzip","9001_20150918165942_00085.xml.gz");
原生 Perl 方式
#!/usr/bin/env perl
use strict;
BEGIN { @ARGV= map { glob($_) } @ARGV }
use Compress::Zlib;
die "Usage: $0 {file.gz|outfile=infile} ...\n"
unless @ARGV ;
foreach my $infile (@ARGV) {
my $outfile= $infile;
if( $infile =~ /=/ ) {
( $outfile, $infile )= split /=/, $infile;
} elsif( $outfile !~ s/[._]gz$//i ) {
$infile .= ".gz";
}
my $gz= gzopen( $infile, "rb" )
or die "Cannot open $infile: $gzerrno\n";
open( OUT, "> $outfile\0" )
or die "Can't write $outfile: $!\n";
binmode(OUT);
my $buffer;
print OUT $buffer
while $gz->gzread($buffer) > 0;
die "Error reading from $infile: $gzerrno\n"
if $gzerrno != Z_STREAM_END;
$gz->gzclose();
close(OUT)
or warn "Error closing $outfile: $!\n";
}