简体   繁体   中英

How to gzip several files in Perl one by one

I have a list of files that I want to compress using Perl. Currently I have this:

gzip \@tocompress -> $outputfile
    or die "Failed to compress: $GzipError\n"

But I want to iterate over the files and add them one at the time to the compress file. How can I achieve that?

It seems you're using IO::Compress::Gzip . Use the Append option of gzip :

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

use IO::Compress::Gzip qw{ gzip $GzipError };

my $outputfile = 'out.gz';
my @tocompress = glob '*.txt';

for my $file (@tocompress) {
    next unless -f $file;
    print STDERR "Adding $file\n";
    gzip($file, $outputfile, Append => 1) or die $GzipError;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM