Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
streamzip302 linesDownload Raw Back to core_perl
1#!/usr/bin/perl2    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'3	if 0; # ^ Run only under a shell4#!/usr/bin/perl5 6# Streaming zip7 8use strict;9use warnings;10 11use IO::Compress::Zip qw(zip12                         ZIP_CM_STORE13                         ZIP_CM_DEFLATE14                         ZIP_CM_BZIP2 ) ;15 16use Getopt::Long;17 18my $VERSION = '1.00';19 20my $compression_method = ZIP_CM_DEFLATE;21my $stream = 0;22my $zipfile = '-';23my $memberName = '-' ;24my $zip64 = 0 ;25my $level ;26 27GetOptions("zip64"          => \$zip64,28           "method=s"       => \&lookupMethod,29           "0"              => sub { $level = 0 },30           "1"              => sub { $level = 1 },31           "2"              => sub { $level = 2 },32           "3"              => sub { $level = 3 },33           "4"              => sub { $level = 4 },34           "5"              => sub { $level = 5 },35           "6"              => sub { $level = 6 },36           "7"              => sub { $level = 7 },37           "8"              => sub { $level = 8 },38           "9"              => sub { $level = 9 },39           "stream"         => \$stream,40           "zipfile=s"      => \$zipfile,41           "member-name=s"  => \$memberName,42           'version'        => sub { print "$VERSION\n"; exit 0 },43           'help'           => \&Usage,44          )45    or Usage();46 47Usage()48    if @ARGV;49 50my @extraOpts = ();51 52if ($compression_method == ZIP_CM_DEFLATE && defined $level)53{54    push @extraOpts, (Level => $level)55}56 57# force streaming zip file when writing to stdout.58$stream = 159    if $zipfile eq '-';60 61zip '-' => $zipfile,62           Name   => $memberName,63           Zip64  => $zip64,64           Method => $compression_method,65           Stream => $stream,66           @extraOpts67    or die "Error creating zip file '$zipfile': $\n" ;68 69exit 0;70 71sub lookupMethod72{73    my $name  = shift;74    my $value = shift ;75 76    my %valid = ( store   => ZIP_CM_STORE,77                  deflate => ZIP_CM_DEFLATE,78                  bzip2   => ZIP_CM_BZIP2,79                  lzma    => 14,80                  xz      => 95,81                  zstd    => 93,82                );83 84    my $method = $valid{ lc $value };85 86    Usage("Unknown method '$value'")87        if ! defined $method;88 89    installModule("Lzma")90        if $method == 14 ;91 92    installModule("Xz")93        if $method == 95 ;94 95    installModule("Zstd")96        if $method == 93;97 98    $compression_method =  $method;99}100 101sub installModule102{103    my $name = shift ;104 105    eval " use IO::Compress::$name; use IO::Compress::Adapter::$name ; " ;106    die "Method '$name' needs IO::Compress::$name\n"107        if $@;108}109 110sub Usage111{112    print <<EOM;113Usage:114  producer | streamzip [OPTIONS] | consumer115  producer | streamzip [OPTIONS] -zipfile output.zip116 117Stream data from stdin, compress into a Zip container, and either stream to stdout, or118write to a named file.119 120OPTIONS121 122  -zipfile=F      Write zip container to the filename 'F'123                  Outputs to stdout if zipfile not specified.124  -member-name=M  Set member name to 'M' [Default '-']125  -0 ... -9       Set compression level for Deflate126                  [Default: 6]127  -zip64          Create a Zip64-compliant zip file [Default: No]128                  Enable Zip64 if input is greater than 4Gig.129  -stream         Force a streamed zip file when 'zipfile' option is also enabled.130                  Only applies when 'zipfile' option is used. [Default: No]131                  Stream is always enabled when writing to stdout.132  -method=M       Compress using method 'M'.133                  Valid methods are134                    store    Store without compression135                    deflate  Use Deflate compression [Deflault]136                    bzip2    Use Bzip2 compression137                    lzma     Use LZMA compression [needs IO::Compress::Lzma]138                    xz       Use LZMA compression [needs IO::Compress::Xz]139                    zstd     Use LZMA compression [needs IO::Compress::Zstd]140  -version        Display version number [$VERSION]141 142Copyright (c) 2019-2024 Paul Marquess. All rights reserved.143 144This program is free software; you can redistribute it and/or145modify it under the same terms as Perl itself.146 147EOM148    exit;149}150 151 152__END__153=head1 NAME154 155streamzip - create a zip file from stdin156 157=head1 SYNOPSIS158 159    producer | streamzip [opts] | consumer160    producer | streamzip [opts] -zipfile=output.zip161 162=head1 DESCRIPTION163 164This program will read data from C<stdin>, compress it into a zip container165and, by default, write a I<streamed> zip file to C<stdout>. No temporary166files are created.167 168The zip container written to C<stdout> is, by necessity, written in169streaming format. Most programs that read Zip files can cope with a170streamed zip file, but if interoperability is important, and your workflow171allows you to write the zip file directly to disk you can create a172non-streamed zip file using the C<zipfile> option.173 174=head2 OPTIONS175 176=over 5177 178=item -zip64179 180Create a Zip64-compliant zip container. Use this option if the input is181greater than 4Gig.182 183Default is disabled.184 185=item  -zipfile=F186 187Write zip container to the filename C<F>.188 189Use the C<Stream> option to force the creation of a streamed zip file.190 191=item  -member-name=M192 193This option is used to name the "file" in the zip container.194 195Default is '-'.196 197=item  -stream198 199Ignored when writing to C<stdout>.200 201If the C<zipfile> option is specified, including this option will trigger202the creation of a streamed zip file.203 204Default: Always enabled when writing to C<stdout>, otherwise disabled.205 206=item  -method=M207 208Compress using method C<M>.209 210Valid method names are211 212    * store    Store without compression213    * deflate  Use Deflate compression [Deflault]214    * bzip2    Use Bzip2 compression215    * lzma     Use LZMA compression216    * xz       Use xz compression217    * zstd     Use Zstandard compression218 219Note that Lzma compress needs C<IO::Compress::Lzma> to be installed.220 221Note that Zstd compress needs C<IO::Compress::Zstd> to be installed.222 223Default is C<deflate>.224 225=item -0, -1, -2, -3, -4, -5, -6, -7, -8, -9226 227Sets the compression level for C<deflate>. Ignored for all other compression methods.228 229C<-0> means no compression and C<-9> for maximum compression.230 231Default is 6232 233=item  -version234 235Display version number236 237=item -help238 239Display help240 241=back242 243=head2 Examples244 245Create a zip file bt reading daa from stdin246 247    $ echo Lorem ipsum dolor sit | perl ./bin/streamzip >abcd.zip248 249Check the contents of C<abcd,zip> with the standard C<unzip> utility250 251    Archive:  abcd.zip252      Length      Date    Time    Name253    ---------  ---------- -----   ----254           22  2021-01-08 19:45   -255    ---------                     -------256           22                     1 file257 258Notice how the C<Name> is set to C<->.259That is the default for a few zip utilities whwre the member name is not given.260 261If you want to explicitly name the file, use the C<-member-name> option as follows262 263    $ echo Lorem ipsum dolor sit | perl ./bin/streamzip -member-name latin >abcd.zip264 265    $ unzip -l abcd.zip266    Archive:  abcd.zip267      Length      Date    Time    Name268    ---------  ---------- -----   ----269           22  2021-01-08 19:47   latin270    ---------                     -------271           22                     1 file272 273 274=head2 When to write a Streamed Zip File275 276A Streamed Zip File is useful in situations where you cannot seek277backwards/forwards in the file.278 279A good examples is when you are serving dynamic content from a Web Server280straight into a socket without needing to create a temporary zip file in281the filesystsm.282 283Similarly if your workfow uses a Linux pipelined commands.284 285=head1 SUPPORT286 287General feedback/questions/bug reports should be sent to288L<https://github.com/pmqs/IO-Compress/issues> (preferred) or289L<https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.290 291 292=head1 AUTHOR293 294Paul Marquess F<pmqs@cpan.org>.295 296=head1 COPYRIGHT297 298Copyright (c) 2019-2024 Paul Marquess. All rights reserved.299 300This program is free software; you can redistribute it and/or modify it301under the same terms as Perl itself.302 
codekingpro/portable-devtools · Team Ai