File Coverage

bin/streamzip
Criterion Covered Total %
statement 46 53 86.7
branch 11 20 55.0
condition 3 3 100.0
subroutine 15 18 83.3
pod n/a
total 75 94 79.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             # Streaming zip
4              
5 28     28   139480 use strict;
  28         47  
  28         1093  
6 28     28   118 use warnings;
  28         126  
  28         1705  
7              
8 28         3687 use IO::Compress::Zip qw(zip
9             ZIP_CM_STORE
10             ZIP_CM_DEFLATE
11 28     28   19960 ZIP_CM_BZIP2 ) ;
  28         302  
12              
13 28     28   21515 use Getopt::Long;
  28         446648  
  28         329  
14              
15 28         5056052 my $VERSION = '1.00';
16              
17 28         102 my $compression_method = ZIP_CM_DEFLATE;
18 28         60 my $stream = 0;
19 28         73 my $zipfile = '-';
20 28         71 my $memberName = '-' ;
21 28         59 my $zip64 = 0 ;
22 28         113 my $level ;
23              
24             GetOptions("zip64" => \$zip64,
25             "method=s" => \&lookupMethod,
26 2     2   3241 "0" => sub { $level = 0 },
27 2     2   3516 "1" => sub { $level = 1 },
28 2     2   3822 "2" => sub { $level = 2 },
29 2     2   3749 "3" => sub { $level = 3 },
30 2     2   3641 "4" => sub { $level = 4 },
31 2     2   2619 "5" => sub { $level = 5 },
32 2     2   3351 "6" => sub { $level = 6 },
33 2     2   3646 "7" => sub { $level = 7 },
34 2     2   3523 "8" => sub { $level = 8 },
35 2     2   3494 "9" => sub { $level = 9 },
36             "stream" => \$stream,
37             "zipfile=s" => \$zipfile,
38             "member-name=s" => \$memberName,
39 0     0   0 'version' => sub { print "$VERSION\n"; exit 0 },
  0         0  
40 28 50       1320 'help' => \&Usage,
41             )
42             or Usage();
43              
44 28 50       4578 Usage()
45             if @ARGV;
46              
47 28         71 my @extraOpts = ();
48              
49 28 100 100     191 if ($compression_method == ZIP_CM_DEFLATE && defined $level)
50             {
51 20         61 push @extraOpts, (Level => $level)
52             }
53              
54             # force streaming zip file when writing to stdout.
55 28 100       94 $stream = 1
56             if $zipfile eq '-';
57              
58 28 50       192 zip '-' => $zipfile,
59             Name => $memberName,
60             Zip64 => $zip64,
61             Method => $compression_method,
62             Stream => $stream,
63             @extraOpts
64             or die "Error creating zip file '$zipfile': $\n" ;
65              
66 28         0 exit 0;
67              
68             sub lookupMethod
69             {
70 6     6   7523 my $name = shift;
71 6         11 my $value = shift ;
72              
73 6         51 my %valid = ( store => ZIP_CM_STORE,
74             deflate => ZIP_CM_DEFLATE,
75             bzip2 => ZIP_CM_BZIP2,
76             lzma => 14,
77             xz => 95,
78             zstd => 93,
79             );
80              
81 6         17 my $method = $valid{ lc $value };
82              
83 6 50       22 Usage("Unknown method '$value'")
84             if ! defined $method;
85              
86 6 50       23 installModule("Lzma")
87             if $method == 14 ;
88              
89 6 50       18 installModule("Xz")
90             if $method == 95 ;
91              
92 6 50       14 installModule("Zstd")
93             if $method == 93;
94              
95 6         57 $compression_method = $method;
96             }
97              
98             sub installModule
99             {
100 0     0     my $name = shift ;
101              
102 0           eval " use IO::Compress::$name; use IO::Compress::Adapter::$name ; " ;
103 0 0         die "Method '$name' needs IO::Compress::$name\n"
104             if $@;
105             }
106              
107             sub Usage
108             {
109 0     0     print <
110             Usage:
111             producer | streamzip [OPTIONS] | consumer
112             producer | streamzip [OPTIONS] -zipfile output.zip
113              
114             Stream data from stdin, compress into a Zip container, and either stream to stdout, or
115             write to a named file.
116              
117             OPTIONS
118              
119             -zipfile=F Write zip container to the filename 'F'
120             Outputs to stdout if zipfile not specified.
121             -member-name=M Set member name to 'M' [Default '-']
122             -0 ... -9 Set compression level for Deflate
123             [Default: 6]
124             -zip64 Create a Zip64-compliant zip file [Default: No]
125             Enable Zip64 if input is greater than 4Gig.
126             -stream Force a streamed zip file when 'zipfile' option is also enabled.
127             Only applies when 'zipfile' option is used. [Default: No]
128             Stream is always enabled when writing to stdout.
129             -method=M Compress using method 'M'.
130             Valid methods are
131             store Store without compression
132             deflate Use Deflate compression [Default]
133             bzip2 Use Bzip2 compression
134             lzma Use LZMA compression [needs IO::Compress::Lzma]
135             xz Use LZMA compression [needs IO::Compress::Xz]
136             zstd Use LZMA compression [needs IO::Compress::Zstd]
137             -version Display version number [$VERSION]
138              
139             Copyright (c) 2019-2026 Paul Marquess. All rights reserved.
140              
141             This program is free software; you can redistribute it and/or
142             modify it under the same terms as Perl itself.
143              
144             EOM
145 0           exit;
146             }
147              
148              
149             __END__