File Coverage

blib/lib/IO/File/CompressOnClose/Gzip.pm
Criterion Covered Total %
statement 31 35 88.5
branch 5 10 50.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 45 54 83.3


line stmt bran cond sub pod time code
1             #
2             # $Id: Gzip.pm,v 1.2 2003/12/28 00:15:15 james Exp $
3             #
4              
5             =head1 NAME
6              
7             IO::File::CompressOnClose::Gzip - Gzip compression for
8             IO::File::CompressOnClose
9              
10             =head1 SYNOPSIS
11              
12             use IO::File::AutoCompress::Gzip;
13             my $file = IO::File::CompressOnClose::Gzip->new('>foo');
14             print $file "foo bar baz\n";
15             $file->close; # file will be compressed to foo.gz
16              
17             =cut
18              
19             package IO::File::CompressOnClose::Gzip;
20              
21 2     2   4059 use strict;
  2         15  
  2         147  
22 2     2   13 use warnings;
  2         4  
  2         78  
23              
24 2     2   10 use vars qw|$VERSION @ISA|;
  2         5  
  2         215  
25              
26             @ISA = qw|IO::File::CompressOnClose|;
27             $VERSION = $IO::File::CompressOnClose::VERSION;
28              
29 2     2   12 use Carp qw|croak|;
  2         3  
  2         153  
30 2     2   10 use IO::File;
  2         3  
  2         412  
31 2     2   544 use IO::File::CompressOnClose;
  2         5  
  2         94  
32 2     2   2534 use IO::Zlib;
  2         407330  
  2         14  
33              
34              
35             # compress using gzip
36             sub compress
37             {
38              
39 2     2 1 6 my($self, $src_file, $dst_file) = @_;
40            
41             # tack on a .gz extension
42 2 50       6 unless( $dst_file ) {
43 2         5 $dst_file = "$src_file.gz";
44             }
45            
46             # compress the file
47 2         2 my($in,$out);
48 2 50       13 unless( $in = IO::File->new($src_file, 'r') ) {
49 0         0 croak("cannot open $src_file for read: $!");
50             }
51 2 50       260 unless( $out = IO::Zlib->new($dst_file, 'w') ) {
52 0         0 croak("cannot open $dst_file for write: $!");
53             }
54              
55 2         4611 while( <$in> ) {
56 1         13 print $out $_;
57             }
58              
59             # close both files
60 2 50       239 unless( $in->close ) {
61 0         0 croak("cannot close $src_file after read: $!");
62             }
63 2 50       67 unless( $out->close ) {
64 0           croak("cannot close $dst_file after write: $!");
65             }
66            
67             }
68              
69             # keep require happy
70             1;
71              
72              
73             __END__