| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Streaming zip | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 28 | ▶ | use IO::Compress::Zip qw(zip | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | ZIP_CM_STORE | ||||||||||||||||||||||||||||
| 10 | ZIP_CM_DEFLATE | ||||||||||||||||||||||||||||
| 11 | 28 | ZIP_CM_BZIP2 ) ; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 28 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 28 | my $VERSION = '1.00'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 28 | my $compression_method = ZIP_CM_DEFLATE; | |||||||||||||||||||||||||||
| 18 | 28 | my $stream = 0; | |||||||||||||||||||||||||||
| 19 | 28 | my $zipfile = '-'; | |||||||||||||||||||||||||||
| 20 | 28 | my $memberName = '-' ; | |||||||||||||||||||||||||||
| 21 | 28 | my $zip64 = 0 ; | |||||||||||||||||||||||||||
| 22 | 28 | my $level ; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | GetOptions("zip64" => \$zip64, | ||||||||||||||||||||||||||||
| 25 | "method=s" => \&lookupMethod, | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | "0" => sub { $level = 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | "1" => sub { $level = 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | "2" => sub { $level = 2 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | "3" => sub { $level = 3 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | "4" => sub { $level = 4 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | "5" => sub { $level = 5 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | "6" => sub { $level = 6 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | "7" => sub { $level = 7 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | "8" => sub { $level = 8 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | "9" => sub { $level = 9 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | "stream" => \$stream, | ||||||||||||||||||||||||||||
| 37 | "zipfile=s" => \$zipfile, | ||||||||||||||||||||||||||||
| 38 | "member-name=s" => \$memberName, | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | 'version' => sub { print "$VERSION\n"; exit 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 28 | ▶ | 'help' => \&Usage, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | ) | ||||||||||||||||||||||||||||
| 42 | or Usage(); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 28 | ▶ | Usage() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | if @ARGV; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 28 | my @extraOpts = (); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 28 | ▶ | if ($compression_method == ZIP_CM_DEFLATE && defined $level) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$compression_method == 8 and defined $level
MC/DC100%2/2
$compression_method == 8 and defined $level
$compression_method == 8
defined $level
Truth table100%3/3
A and B
A= $compression_method == 8B= defined $level
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 20 | push @extraOpts, (Level => $level) | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # force streaming zip file when writing to stdout. | ||||||||||||||||||||||||||||
| 55 | 28 | ▶ | $stream = 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | if $zipfile eq '-'; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 28 | ▶ | zip '-' => $zipfile, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | exit 0; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub lookupMethod | ||||||||||||||||||||||||||||
| 69 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
lookupMethod: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 70 | 6 | my $name = shift; | |||||||||||||||||||||||||||
| 71 | 6 | my $value = shift ; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 6 | 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 | my $method = $valid{ lc $value }; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 6 | ▶ | Usage("Unknown method '$value'") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | if ! defined $method; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 6 | ▶ | installModule("Lzma") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | if $method == 14 ; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 6 | ▶ | installModule("Xz") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | if $method == 95 ; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 6 | ▶ | installModule("Zstd") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | if $method == 93; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 6 | $compression_method = $method; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub installModule | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
installModule: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 100 | 0 | my $name = shift ; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | eval " use IO::Compress::$name; use IO::Compress::Adapter::$name ; " ; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | die "Method '$name' needs IO::Compress::$name\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | if $@; | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub Usage | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
Usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 109 | 0 | print <<EOM; | |||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||