]>
Commit | Line | Data |
---|---|---|
2786a68d QR |
1 | #!perl |
2 | # | |
3 | ||
4 | use warnings; | |
5 | use strict; | |
6 | use 5.014; | |
7 | ||
8 | use File::Slurp; | |
9 | use Getopt::Long; | |
10 | ||
11 | use IO::Compress::Brotli; | |
12 | use IO::Uncompress::Brotli; | |
13 | ||
14 | GetOptions( | |
15 | 'c|custom-dictionary=s' => \(my $DICTIONARY), | |
16 | 'd|decompress' => \(my $DECOMPRESS), | |
17 | 'h|help' => \(my $HELP), | |
18 | 'i|input=s' => \(my $INPUT), | |
19 | 'o|output=s' => \(my $OUTPUT), | |
20 | 'q|quality=i' => \(my $QUALITY = 11), | |
21 | 'r|repeat=i' => \(my $REPEAT), | |
22 | 's|stream=i' => \(my $STREAM), | |
23 | 'v|verbose' => \(my $VERBOSE), | |
24 | 'w|window=i' => \(my $WINDOW = 22), | |
25 | ); | |
26 | ||
27 | if( $HELP ) { | |
28 | say "Usage: $0 [--force] [--quality n] [--decompress] [--input filename] [--output filename]". | |
29 | " [--repeat iters] [--verbose] [--window n] [--custom-dictionary filename] [--stream size]"; | |
30 | exit 1; | |
31 | } | |
32 | ||
33 | my ($ifh, $ofh) = (\*STDIN, \*STDOUT); | |
34 | ||
35 | if( $INPUT ) { | |
36 | open $ifh, "<", $INPUT | |
37 | or die "Cannot open input file $INPUT.\n"; | |
38 | } | |
39 | binmode $ifh; | |
40 | ||
41 | if( $OUTPUT ) { | |
42 | open $ofh, ">", $OUTPUT | |
43 | or die "Cannot open output file $OUTPUT.\n"; | |
44 | } | |
45 | binmode $ofh; | |
46 | ||
47 | if( $DECOMPRESS ) { | |
48 | if( $STREAM ) { | |
49 | my $brotli = IO::Uncompress::Brotli->create(); | |
50 | while( read $ifh, (my $buf), $STREAM ) { | |
51 | print $ofh $brotli->decompress($buf); | |
52 | } | |
53 | } | |
54 | else { | |
55 | my $encoded = read_file( $ifh ); | |
56 | my $decoded = unbro( $encoded ); | |
57 | write_file( $ofh, $decoded ); | |
58 | } | |
59 | } | |
60 | else { | |
61 | if( $STREAM ) { | |
62 | my $brotli = IO::Compress::Brotli->create(); | |
63 | $brotli->quality( $QUALITY ); | |
64 | $brotli->window( $WINDOW ); | |
65 | while( read $ifh, (my $buf), $STREAM ) { | |
66 | print $ofh $brotli->compress($buf); | |
67 | } | |
68 | } | |
69 | else { | |
70 | my $decoded = read_file( $ifh ); | |
71 | my $encoded = bro( $decoded, $QUALITY, $WINDOW ); | |
72 | write_file( $ofh, $encoded ); | |
73 | } | |
74 | } | |
75 |