Commit | Line | Data |
---|---|---|
d3d73539 CBW |
1 | #!/usr/bin/perl |
2 | ||
3 | # Streaming zip | |
4 | ||
5 | use strict; | |
6 | use warnings; | |
7 | ||
8 | use IO::Compress::Zip qw(zip | |
9 | ZIP_CM_STORE | |
10 | ZIP_CM_DEFLATE | |
11 | ZIP_CM_BZIP2 | |
12 | ZIP_CM_LZMA ); | |
13 | use Getopt::Long; | |
14 | ||
4737eb08 | 15 | my $VERSION = '1.001'; |
d3d73539 CBW |
16 | |
17 | my $compression_method = ZIP_CM_DEFLATE; | |
18 | my $stream = 0; | |
19 | my $zipfile = '-'; | |
20 | my $memberName = '-' ; | |
21 | my $zip64 = 0 ; | |
22 | ||
23 | GetOptions("zip64" => \$zip64, | |
24 | "method=s" => \&lookupMethod, | |
25 | "stream" => \$stream, | |
26 | "zipfile=s" => \$zipfile, | |
27 | "member-name=s" => \$memberName, | |
28 | 'version' => sub { print "$VERSION\n"; exit 0 }, | |
29 | 'help' => \&Usage, | |
30 | ) | |
31 | or Usage(); | |
32 | ||
33 | Usage() | |
34 | if @ARGV; | |
35 | ||
36 | ||
37 | zip '-' => $zipfile, | |
38 | Name => $memberName, | |
39 | Zip64 => $zip64, | |
40 | Method => $compression_method, | |
41 | Stream => $stream | |
42 | or die "Error creating zip file '$zipfile': $\n" ; | |
43 | ||
44 | exit 0; | |
45 | ||
46 | sub lookupMethod | |
47 | { | |
48 | my $name = shift; | |
49 | my $value = shift ; | |
50 | ||
51 | my %valid = ( store => ZIP_CM_STORE, | |
52 | deflate => ZIP_CM_DEFLATE, | |
53 | bzip2 => ZIP_CM_BZIP2, | |
54 | lzma => ZIP_CM_LZMA, | |
55 | ); | |
56 | ||
57 | my $method = $valid{ lc $value }; | |
58 | ||
59 | Usage("Unknown method '$value'") | |
60 | if ! defined $method; | |
61 | ||
62 | # If LZMA was rquested, check that it is available | |
63 | if ($method == ZIP_CM_LZMA) | |
64 | { | |
65 | eval ' use IO::Compress::Adapter::Lzma'; | |
4737eb08 | 66 | die "Method 'LZMA' needs IO::Compress::Adapter::Lzma\n" |
d3d73539 CBW |
67 | if ! defined $IO::Compress::Lzma::VERSION; |
68 | } | |
69 | ||
70 | $compression_method = $method; | |
71 | } | |
72 | ||
73 | sub Usage | |
74 | { | |
4737eb08 TR |
75 | print <<EOM; |
76 | Usage: | |
77 | producer | streamzip [OPTIONS] | consumer | |
78 | producer | streamzip [OPTIONS] -zipfile=output.zip | |
d3d73539 CBW |
79 | |
80 | Stream data from stdin, compress into a Zip container, and stream to stdout. | |
81 | ||
82 | OPTIONS | |
83 | ||
4737eb08 TR |
84 | -zipfile=F Write zip container to the filename 'F' |
85 | Outputs to stdout if zipfile not specified. | |
86 | -member-name=M Set member name to 'M' [Default '-'] | |
d3d73539 | 87 | -zip64 Create a Zip64-compliant zip file [Default: No] |
4737eb08 TR |
88 | Enable Zip64 if input is greater than 4Gig. |
89 | -stream Force a streamed zip file when zipfile is also enabled. | |
d3d73539 | 90 | Only applies when 'zipfile' option is used. [Default: No] |
4737eb08 TR |
91 | Stream is always enabled when writing to stdout. |
92 | -method=M Compress using method 'M'. | |
d3d73539 CBW |
93 | Valid methods are |
94 | store Store without compression | |
95 | deflate Use Deflate compression [Deflault] | |
96 | bzip2 Use Bzip2 compression | |
97 | lzma Use LZMA compression [needs IO::Compress::Lzma] | |
98 | Lzma needs IO::Compress::Lzma to be installed. | |
99 | -version Display version number [$VERSION] | |
100 | ||
4737eb08 | 101 | Copyright (c) 2019-2020 Paul Marquess. All rights reserved. |
d3d73539 CBW |
102 | |
103 | This program is free software; you can redistribute it and/or | |
104 | modify it under the same terms as Perl itself. | |
105 | ||
106 | EOM | |
4737eb08 | 107 | exit; |
d3d73539 CBW |
108 | } |
109 | ||
110 | ||
111 | __END__ | |
112 | =head1 NAME | |
113 | ||
114 | streamzip - create a zip file from stdin | |
115 | ||
116 | =head1 SYNOPSIS | |
117 | ||
9e0c85b9 | 118 | producer | streamzip [opts] | consumer |
d3d73539 CBW |
119 | producer | streamzip [opts] -zipfile=output.zip |
120 | ||
121 | =head1 DESCRIPTION | |
122 | ||
4737eb08 TR |
123 | This program will read data from C<stdin>, compress it into a zip container |
124 | and, by default, write a I<streamed> zip file to C<stdout>. No temporary | |
125 | files are created. | |
d3d73539 | 126 | |
4737eb08 TR |
127 | The zip container written to C<stdout> is, by necessity, written in |
128 | streaming format. Most programs that read Zip files can cope with a | |
129 | streamed zip file, but if interoperability is important, and your workflow | |
130 | allows you to write the zip file directly to disk you can create a | |
131 | non-streamed zip file using the C<zipfile> option. | |
d3d73539 CBW |
132 | |
133 | =head2 OPTIONS | |
134 | ||
135 | =over 5 | |
136 | ||
137 | =item -zip64 | |
138 | ||
4737eb08 TR |
139 | Create a Zip64-compliant zip container. Use this option if the input is |
140 | greater than 4Gig. | |
d3d73539 CBW |
141 | |
142 | Default is disabled. | |
143 | ||
4737eb08 | 144 | =item -zipfile=F |
d3d73539 | 145 | |
4737eb08 | 146 | Write zip container to the filename C<F>. |
d3d73539 | 147 | |
4737eb08 | 148 | Use the C<Stream> option to force the creation of a streamed zip file. |
d3d73539 | 149 | |
4737eb08 | 150 | =item -member-name=M |
d3d73539 CBW |
151 | |
152 | This option is used to name the "file" in the zip container. | |
153 | ||
154 | Default is '-'. | |
155 | ||
4737eb08 | 156 | =item -stream |
d3d73539 | 157 | |
4737eb08 | 158 | Ignored when writing to C<stdout>. |
d3d73539 | 159 | |
4737eb08 TR |
160 | If the C<zipfile> option is specified, including this option will trigger |
161 | the creation of a streamed zip file. | |
d3d73539 | 162 | |
4737eb08 | 163 | Default: Always enabled when writing to C<stdout>, otherwise disabled. |
d3d73539 | 164 | |
4737eb08 | 165 | =item -method=M |
d3d73539 | 166 | |
4737eb08 | 167 | Compress using method C<M>. |
d3d73539 CBW |
168 | |
169 | Valid method names are | |
170 | ||
171 | * store Store without compression | |
172 | * deflate Use Deflate compression [Deflault] | |
173 | * bzip2 Use Bzip2 compression | |
4737eb08 | 174 | * lzma Use LZMA compression |
d3d73539 | 175 | |
4737eb08 | 176 | Note that Lzma compress needs C<IO::Compress::Lzma> to be installed. |
d3d73539 | 177 | |
4737eb08 | 178 | Default is C<deflate>. |
d3d73539 | 179 | |
4737eb08 | 180 | =item -version |
d3d73539 CBW |
181 | |
182 | Display version number [$VERSION] | |
183 | ||
184 | =item -help | |
185 | ||
186 | Display help | |
187 | ||
188 | =back | |
189 | ||
190 | =head2 When to use a Streamed Zip File | |
191 | ||
4737eb08 TR |
192 | A Streamed Zip File is useful in situations where you cannot seek |
193 | backwards/forwards in the file. | |
d3d73539 | 194 | |
4737eb08 TR |
195 | A good examples is when you are serving dynamic content from a Web Server |
196 | straight into a socket without needing to create a temporary zip file in | |
197 | the filesystsm. | |
d3d73539 CBW |
198 | |
199 | Similarly if your workfow uses a Linux pipelined commands. | |
200 | ||
201 | =head1 SUPPORT | |
202 | ||
203 | General feedback/questions/bug reports should be sent to | |
204 | L<https://github.com/pmqs/IO-Compress/issues> (preferred) or | |
205 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>. | |
206 | ||
207 | ||
208 | =head1 AUTHOR | |
209 | ||
210 | Paul Marquess F<pmqs@cpan.org>. | |
211 | ||
4737eb08 | 212 | =head1 COPYRIGHT |
d3d73539 | 213 | |
4737eb08 | 214 | Copyright (c) 2019-2020 Paul Marquess. All rights reserved. |
d3d73539 CBW |
215 | |
216 | This program is free software; you can redistribute it and/or modify it | |
4737eb08 | 217 | under the same terms as Perl itself. |