File Coverage

blib/lib/IO/File/CompressOnClose/Zip.pm
Criterion Covered Total %
statement 37 39 94.8
branch 8 12 66.6
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 56 62 90.3


line stmt bran cond sub pod time code
1             #
2             # $Id: Zip.pm,v 1.1 2003/12/28 00:15:15 james Exp $
3             #
4              
5             =head1 NAME
6              
7             IO::File::CompressOnClose::Zip - Zip compression for
8             IO::File::CompressOnClose
9              
10             =head1 SYNOPSIS
11              
12             use IO::File::AutoCompress::Zip;
13             my $file = IO::File::CompressOnClose::Zip->new('>foo');
14             print $file "foo bar baz\n";
15             $file->close; # file will be compressed to foo.zip
16              
17             =cut
18              
19             package IO::File::CompressOnClose::Zip;
20              
21 1     1   1076 use strict;
  1         2  
  1         56  
22 1     1   6 use warnings;
  1         2  
  1         50  
23              
24 1     1   5 use vars qw|$VERSION @ISA|;
  1         3  
  1         101  
25              
26             @ISA = qw|IO::File::CompressOnClose|;
27             $VERSION = $IO::File::CompressOnClose::VERSION;
28              
29 1     1   7 use Archive::Zip qw|:ERROR_CODES|;
  1         3  
  1         209  
30 1     1   7 use Carp qw|croak|;
  1         2  
  1         57  
31 1     1   5 use IO::File;
  1         2  
  1         217  
32 1     1   625 use IO::File::CompressOnClose;
  1         5  
  1         282  
33              
34              
35             # compress using zip
36             sub compress
37             {
38              
39 2     2 1 5 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.zip";
44             }
45            
46             # create a new archive
47 2 50       15 my $zip = Archive::Zip->new
48             or croak("cannot instantiate Archive::Zip object");
49            
50             # figure out the name of the member in the archive
51 2         75 my $member_name;
52 2 100       6 unless( $member_name = $self->member_filename ) {
53 1         9 require File::Basename;
54 1         69 $member_name = File::Basename::basename($src_file);
55             }
56            
57             # add the source file to the archive
58 2 50       10 unless( $zip->addFile( $src_file, $member_name ) ) {
59 0         0 croak("cannot add $src_file to archive");
60             }
61            
62             # write out the archive
63 2 50       817 unless( AZ_OK == $zip->writeToFileNamed( $dst_file ) ) {
64 0         0 croak("cannot write archive $dst_file");
65             }
66            
67             }
68              
69              
70             # accessor methods
71             sub member_filename
72             {
73            
74 3     3 1 337 my($self, $newval) = @_;
75 3         4 my $oldval = ${*$self}->{member_filename};
  3         11  
76 3 100       10 ${*$self}->{member_filename} = $newval if( @_ > 1 );
  1         3  
77 3         18 return $oldval;
78            
79             }
80              
81             # keep require happy
82             1;
83              
84              
85             __END__