|
1 | | -import java.io.*; |
| 1 | +import java.io.InputStream; |
| 2 | +import java.io.OutputStream; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.BufferedInputStream; |
2 | 7 | import java.nio.file.Files; |
3 | 8 | import java.nio.file.Path; |
4 | | -import org.apache.commons.compress.archivers.*; |
| 9 | + |
| 10 | +import org.apache.commons.compress.archivers.ArchiveEntry; |
| 11 | +import org.apache.commons.compress.archivers.ArchiveException; |
| 12 | +import org.apache.commons.compress.archivers.ArchiveInputStream; |
| 13 | +import org.apache.commons.compress.archivers.ArchiveStreamFactory; |
5 | 14 | import org.apache.commons.compress.compressors.CompressorException; |
6 | 15 | import org.apache.commons.compress.compressors.CompressorInputStream; |
7 | 16 | import org.apache.commons.compress.compressors.CompressorStreamFactory; |
8 | | -import org.apache.commons.compress.compressors.gzip.*; |
| 17 | +import org.apache.commons.compress.compressors.lz4.*; |
| 18 | +import org.apache.commons.compress.archivers.ar.ArArchiveInputStream; |
| 19 | +import org.apache.commons.compress.archivers.arj.ArjArchiveInputStream; |
| 20 | +import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream; |
| 21 | +import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; |
| 22 | +import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; |
| 23 | +import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream; |
| 24 | +import org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream; |
| 25 | +import org.apache.commons.compress.compressors.snappy.SnappyCompressorInputStream; |
| 26 | +import org.apache.commons.compress.compressors.xz.XZCompressorInputStream; |
| 27 | +import org.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream; |
| 28 | +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; |
| 29 | +import org.apache.commons.compress.compressors.brotli.BrotliCompressorInputStream; |
| 30 | +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; |
| 31 | +import org.apache.commons.compress.compressors.deflate.DeflateCompressorInputStream; |
| 32 | +import org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream; |
| 33 | +import org.apache.commons.compress.compressors.z.ZCompressorInputStream; |
9 | 34 |
|
10 | 35 | public class CommonsCompressHandler { |
11 | | - public static void commonsCompressorInputStream(InputStream inputStream) throws IOException { |
12 | | - BufferedInputStream in = new BufferedInputStream(inputStream); |
13 | | - OutputStream out = Files.newOutputStream(Path.of("tmpfile")); |
14 | | - GzipCompressorInputStream gzIn = |
15 | | - new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(in); |
16 | | - // for testing |
17 | | - new org.apache.commons.compress.compressors.brotli.BrotliCompressorInputStream(in); |
18 | | - new org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream(in); |
19 | | - new org.apache.commons.compress.compressors.deflate.DeflateCompressorInputStream(in); |
20 | | - new org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream(in); |
21 | | - new org.apache.commons.compress.compressors.lz4.BlockLZ4CompressorInputStream(in); |
22 | | - new org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream(in); |
23 | | - new org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream(in); |
24 | | - new org.apache.commons.compress.compressors.snappy.SnappyCompressorInputStream(in); |
25 | | - new org.apache.commons.compress.compressors.xz.XZCompressorInputStream(in); |
26 | | - new org.apache.commons.compress.compressors.z.ZCompressorInputStream(in); |
27 | | - new org.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream(in); |
28 | 36 |
|
29 | | - int buffersize = 4096; |
30 | | - final byte[] buffer = new byte[buffersize]; |
31 | | - int n = 0; |
32 | | - while (-1 != (n = gzIn.read(buffer))) { |
33 | | - out.write(buffer, 0, n); |
| 37 | + static void commonsCompressArchiveInputStream(InputStream inputStream) throws ArchiveException { |
| 38 | + new ArArchiveInputStream(inputStream); // $bomb |
| 39 | + new ArjArchiveInputStream(inputStream); // $bomb |
| 40 | + new CpioArchiveInputStream(inputStream); // $bomb |
| 41 | + new JarArchiveInputStream(inputStream); // $bomb |
| 42 | + new ZipArchiveInputStream(inputStream); // $bomb |
34 | 43 | } |
35 | | - out.close(); |
36 | | - gzIn.close(); |
37 | | - } |
38 | 44 |
|
39 | | - static void commonsCompressArchiveInputStream(InputStream inputStream) throws ArchiveException { |
40 | | - new org.apache.commons.compress.archivers.ar.ArArchiveInputStream(inputStream); |
41 | | - new org.apache.commons.compress.archivers.arj.ArjArchiveInputStream(inputStream); |
42 | | - new org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream(inputStream); |
43 | | - new org.apache.commons.compress.archivers.jar.JarArchiveInputStream(inputStream); |
44 | | - new org.apache.commons.compress.archivers.zip.ZipArchiveInputStream(inputStream); |
45 | | - } |
| 45 | + public static void commonsCompressorInputStream(InputStream inputStream) throws IOException { |
| 46 | + BufferedInputStream in = new BufferedInputStream(inputStream); |
| 47 | + OutputStream out = Files.newOutputStream(Path.of("tmpfile")); |
| 48 | + GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); // $bomb |
| 49 | + // for testing |
| 50 | + new BrotliCompressorInputStream(in); // $bomb |
| 51 | + new BZip2CompressorInputStream(in); // $bomb |
| 52 | + new DeflateCompressorInputStream(in); // $bomb |
| 53 | + new Deflate64CompressorInputStream(in); // $bomb |
| 54 | + new BlockLZ4CompressorInputStream(in); // $bomb |
| 55 | + new LZMACompressorInputStream(in); // $bomb |
| 56 | + new Pack200CompressorInputStream(in); // $bomb |
| 57 | + new SnappyCompressorInputStream(in); // $bomb |
| 58 | + new XZCompressorInputStream(in); // $bomb |
| 59 | + new ZCompressorInputStream(in); // $bomb |
| 60 | + new ZstdCompressorInputStream(in); // $bomb |
46 | 61 |
|
47 | | - static void commonsCompressArchiveInputStream2(InputStream inputStream) { |
48 | | - byte[] readBuffer = new byte[4096]; |
49 | | - try (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream zipInputStream = |
50 | | - new org.apache.commons.compress.archivers.zip.ZipArchiveInputStream(inputStream)) { |
51 | | - ArchiveEntry entry = null; |
52 | | - while ((entry = zipInputStream.getNextEntry()) != null) { |
53 | | - if (!zipInputStream.canReadEntryData(entry)) { |
54 | | - continue; |
| 62 | + int buffersize = 4096; |
| 63 | + final byte[] buffer = new byte[buffersize]; |
| 64 | + int n = 0; |
| 65 | + while (-1 != (n = gzIn.read(buffer))) { |
| 66 | + out.write(buffer, 0, n); |
55 | 67 | } |
56 | | - File f = new File("tmpfile"); |
57 | | - try (OutputStream outputStream = new FileOutputStream(f)) { |
58 | | - int readLen; |
59 | | - while ((readLen = zipInputStream.read(readBuffer)) != -1) { // BAD |
60 | | - outputStream.write(readBuffer, 0, readLen); |
61 | | - } |
| 68 | + out.close(); |
| 69 | + gzIn.close(); |
| 70 | + } |
| 71 | + |
| 72 | + static void commonsCompressArchiveInputStream2(InputStream inputStream) { |
| 73 | + byte[] readBuffer = new byte[4096]; |
| 74 | + try (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream zipInputStream = |
| 75 | + new org.apache.commons.compress.archivers.zip.ZipArchiveInputStream(inputStream)) { // $bomb |
| 76 | + ArchiveEntry entry = null; |
| 77 | + while ((entry = zipInputStream.getNextEntry()) != null) { |
| 78 | + if (!zipInputStream.canReadEntryData(entry)) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + File f = new File("tmpfile"); |
| 82 | + try (OutputStream outputStream = new FileOutputStream(f)) { |
| 83 | + int readLen; |
| 84 | + while ((readLen = zipInputStream.read(readBuffer)) != -1) { |
| 85 | + outputStream.write(readBuffer, 0, readLen); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } catch (IOException e) { |
| 90 | + throw new RuntimeException(e); |
62 | 91 | } |
63 | | - } |
64 | | - } catch (IOException e) { |
65 | | - throw new RuntimeException(e); |
66 | 92 | } |
67 | | - } |
68 | 93 |
|
69 | | - static void commonsCompressArchiveStreamFactory(InputStream inputStream) |
70 | | - throws IOException, ArchiveException { |
71 | | - BufferedInputStream bin = new BufferedInputStream(inputStream); |
72 | | - ArchiveInputStream zipInputStream = new ArchiveStreamFactory().createArchiveInputStream(bin); |
73 | | - ArchiveEntry entry = null; |
74 | | - byte[] readBuffer = new byte[4096]; |
75 | | - while ((entry = zipInputStream.getNextEntry()) != null) { |
76 | | - if (!zipInputStream.canReadEntryData(entry)) { |
77 | | - continue; |
78 | | - } |
79 | | - File f = new File("tmpfile"); |
80 | | - try (OutputStream outputStream = new FileOutputStream(f)) { |
81 | | - int readLen; |
82 | | - while ((readLen = zipInputStream.read(readBuffer)) != -1) { // BAD |
83 | | - outputStream.write(readBuffer, 0, readLen); |
| 94 | + static void commonsCompressArchiveStreamFactory(InputStream inputStream) |
| 95 | + throws IOException, ArchiveException { |
| 96 | + BufferedInputStream bin = new BufferedInputStream(inputStream); |
| 97 | + ArchiveInputStream zipInputStream = new ArchiveStreamFactory().createArchiveInputStream(bin); |
| 98 | + ArchiveEntry entry = null; |
| 99 | + byte[] readBuffer = new byte[4096]; |
| 100 | + while ((entry = zipInputStream.getNextEntry()) != null) { |
| 101 | + if (!zipInputStream.canReadEntryData(entry)) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + File f = new File("tmpfile"); |
| 105 | + try (OutputStream outputStream = new FileOutputStream(f)) { |
| 106 | + int readLen; |
| 107 | + while ((readLen = zipInputStream.read(readBuffer)) != -1) { // $bomb |
| 108 | + outputStream.write(readBuffer, 0, readLen); |
| 109 | + } |
| 110 | + } |
84 | 111 | } |
85 | | - } |
86 | 112 | } |
87 | | - } |
88 | 113 |
|
89 | | - static void commonsCompressCompressorStreamFactory(InputStream inputStream) |
90 | | - throws IOException, CompressorException { |
91 | | - BufferedInputStream bin = new BufferedInputStream(inputStream); |
92 | | - CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream(bin); |
93 | | - OutputStream out = Files.newOutputStream(Path.of("tmpfile")); |
94 | | - int buffersize = 4096; |
95 | | - final byte[] buffer = new byte[buffersize]; |
96 | | - int n = 0; |
97 | | - while (-1 != (n = in.read(buffer))) { // BAD |
98 | | - out.write(buffer, 0, n); |
| 114 | + static void commonsCompressCompressorStreamFactory(InputStream inputStream) |
| 115 | + throws IOException, CompressorException { |
| 116 | + BufferedInputStream bin = new BufferedInputStream(inputStream); |
| 117 | + CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream(bin); |
| 118 | + OutputStream out = Files.newOutputStream(Path.of("tmpfile")); |
| 119 | + int buffersize = 4096; |
| 120 | + final byte[] buffer = new byte[buffersize]; |
| 121 | + int n = 0; |
| 122 | + while (-1 != (n = in.read(buffer))) { // $bomb |
| 123 | + out.write(buffer, 0, n); |
| 124 | + } |
| 125 | + out.close(); |
| 126 | + in.close(); |
99 | 127 | } |
100 | | - out.close(); |
101 | | - in.close(); |
102 | | - } |
103 | 128 | } |
0 commit comments