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?

Upvotes: 0

Views: 154

Answers (1)

choroba
choroba

Reputation: 241828

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;
}

Upvotes: 1

Related Questions