This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update IO-Compress to CPAN version 2.096
[perl5.git] / cpan / IO-Compress / bin / streamzip
CommitLineData
d3d73539
CBW
1#!/usr/bin/perl
2
3# Streaming zip
4
5use strict;
6use warnings;
7
8use IO::Compress::Zip qw(zip
9 ZIP_CM_STORE
10 ZIP_CM_DEFLATE
11 ZIP_CM_BZIP2
12 ZIP_CM_LZMA );
13use Getopt::Long;
14
4737eb08 15my $VERSION = '1.001';
d3d73539
CBW
16
17my $compression_method = ZIP_CM_DEFLATE;
18my $stream = 0;
19my $zipfile = '-';
20my $memberName = '-' ;
21my $zip64 = 0 ;
22
23GetOptions("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
33Usage()
34 if @ARGV;
35
36
37zip '-' => $zipfile,
38 Name => $memberName,
39 Zip64 => $zip64,
40 Method => $compression_method,
41 Stream => $stream
42 or die "Error creating zip file '$zipfile': $\n" ;
43
44exit 0;
45
46sub 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
73sub Usage
74{
4737eb08
TR
75 print <<EOM;
76Usage:
77 producer | streamzip [OPTIONS] | consumer
78 producer | streamzip [OPTIONS] -zipfile=output.zip
d3d73539
CBW
79
80Stream data from stdin, compress into a Zip container, and stream to stdout.
81
82OPTIONS
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 101Copyright (c) 2019-2020 Paul Marquess. All rights reserved.
d3d73539
CBW
102
103This program is free software; you can redistribute it and/or
104modify it under the same terms as Perl itself.
105
106EOM
4737eb08 107 exit;
d3d73539
CBW
108}
109
110
111__END__
112=head1 NAME
113
114streamzip - 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
123This program will read data from C<stdin>, compress it into a zip container
124and, by default, write a I<streamed> zip file to C<stdout>. No temporary
125files are created.
d3d73539 126
4737eb08
TR
127The zip container written to C<stdout> is, by necessity, written in
128streaming format. Most programs that read Zip files can cope with a
129streamed zip file, but if interoperability is important, and your workflow
130allows you to write the zip file directly to disk you can create a
131non-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
139Create a Zip64-compliant zip container. Use this option if the input is
140greater than 4Gig.
d3d73539
CBW
141
142Default is disabled.
143
4737eb08 144=item -zipfile=F
d3d73539 145
4737eb08 146Write zip container to the filename C<F>.
d3d73539 147
4737eb08 148Use the C<Stream> option to force the creation of a streamed zip file.
d3d73539 149
4737eb08 150=item -member-name=M
d3d73539
CBW
151
152This option is used to name the "file" in the zip container.
153
154Default is '-'.
155
4737eb08 156=item -stream
d3d73539 157
4737eb08 158Ignored when writing to C<stdout>.
d3d73539 159
4737eb08
TR
160If the C<zipfile> option is specified, including this option will trigger
161the creation of a streamed zip file.
d3d73539 162
4737eb08 163Default: Always enabled when writing to C<stdout>, otherwise disabled.
d3d73539 164
4737eb08 165=item -method=M
d3d73539 166
4737eb08 167Compress using method C<M>.
d3d73539
CBW
168
169Valid 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 176Note that Lzma compress needs C<IO::Compress::Lzma> to be installed.
d3d73539 177
4737eb08 178Default is C<deflate>.
d3d73539 179
4737eb08 180=item -version
d3d73539
CBW
181
182Display version number [$VERSION]
183
184=item -help
185
186Display help
187
188=back
189
190=head2 When to use a Streamed Zip File
191
4737eb08
TR
192A Streamed Zip File is useful in situations where you cannot seek
193backwards/forwards in the file.
d3d73539 194
4737eb08
TR
195A good examples is when you are serving dynamic content from a Web Server
196straight into a socket without needing to create a temporary zip file in
197the filesystsm.
d3d73539
CBW
198
199Similarly if your workfow uses a Linux pipelined commands.
200
201=head1 SUPPORT
202
203General feedback/questions/bug reports should be sent to
204L<https://github.com/pmqs/IO-Compress/issues> (preferred) or
205L<https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.
206
207
208=head1 AUTHOR
209
210Paul Marquess F<pmqs@cpan.org>.
211
4737eb08 212=head1 COPYRIGHT
d3d73539 213
4737eb08 214Copyright (c) 2019-2020 Paul Marquess. All rights reserved.
d3d73539
CBW
215
216This program is free software; you can redistribute it and/or modify it
4737eb08 217under the same terms as Perl itself.