|
3 | 3 | import com.robin.core.base.util.Const; |
4 | 4 | import com.robin.core.base.util.FileUtils; |
5 | 5 | import net.jpountz.lz4.LZ4FrameOutputStream; |
6 | | -import org.anarres.lzo.*; |
7 | | - |
8 | | -import org.apache.commons.compress.compressors.brotli.BrotliCompressorInputStream; |
| 6 | +import org.anarres.lzo.LzoAlgorithm; |
| 7 | +import org.anarres.lzo.LzoCompressor; |
| 8 | +import org.anarres.lzo.LzoLibrary; |
| 9 | +import org.anarres.lzo.LzoOutputStream; |
9 | 10 | import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; |
10 | 11 | import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream; |
11 | 12 | import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream; |
12 | 13 | import org.tukaani.xz.LZMA2Options; |
13 | | -import org.tukaani.xz.LZMAOutputStream; |
14 | 14 | import org.tukaani.xz.XZOutputStream; |
15 | 15 | import org.xerial.snappy.SnappyOutputStream; |
16 | 16 |
|
17 | | -import java.io.BufferedOutputStream; |
18 | 17 | import java.io.IOException; |
19 | | -import java.io.InputStream; |
20 | 18 | import java.io.OutputStream; |
21 | | -import java.util.ArrayList; |
22 | | -import java.util.List; |
23 | 19 | import java.util.zip.GZIPOutputStream; |
24 | 20 | import java.util.zip.ZipEntry; |
25 | 21 | import java.util.zip.ZipOutputStream; |
26 | 22 |
|
27 | 23 |
|
28 | 24 | public class CompressEncoder { |
29 | | - private CompressEncoder(){ |
| 25 | + private CompressEncoder() { |
30 | 26 |
|
31 | 27 | } |
32 | 28 |
|
33 | | - /** |
34 | | - *wrap OutputStream with specify compress Format (Zip file can only support One file) |
35 | | - * @param path file path |
36 | | - * @param rawstream raw outputstream |
37 | | - * @return wrap outputStream |
38 | | - */ |
39 | | - public static OutputStream getOutputStreamByCompressType(String path,OutputStream rawstream) throws IOException{ |
| 29 | + /** |
| 30 | + * wrap OutputStream with specify compress Format (Zip file can only support One file) |
| 31 | + * |
| 32 | + * @param path file path |
| 33 | + * @param rawstream raw outputstream |
| 34 | + * @return wrap outputStream |
| 35 | + */ |
| 36 | + public static OutputStream getOutputStreamByCompressType(String path, OutputStream rawstream) throws IOException { |
40 | 37 | OutputStream outputStream; |
41 | | - FileUtils.FileContent content=FileUtils.parseFile(path); |
42 | | - Const.CompressType type=content.getCompressType(); |
43 | | - switch (type){ |
| 38 | + FileUtils.FileContent content = FileUtils.parseFile(path); |
| 39 | + Const.CompressType type = content.getCompressType(); |
| 40 | + switch (type) { |
44 | 41 | case COMPRESS_TYPE_GZ: |
45 | | - outputStream=new GZIPOutputStream(wrapOutputStream(rawstream)); |
| 42 | + outputStream = new GZIPOutputStream(rawstream); |
46 | 43 | break; |
47 | 44 | case COMPRESS_TYPE_BZ2: |
48 | | - outputStream=new BZip2CompressorOutputStream(wrapOutputStream(rawstream)); |
| 45 | + outputStream = new BZip2CompressorOutputStream(rawstream); |
49 | 46 | break; |
50 | 47 | case COMPRESS_TYPE_LZO: |
51 | 48 | LzoAlgorithm algorithm = LzoAlgorithm.LZO1X; |
52 | 49 | LzoCompressor compressor = LzoLibrary.getInstance().newCompressor(algorithm, null); |
53 | | - outputStream = new LzoOutputStream(wrapOutputStream(rawstream), compressor); |
| 50 | + outputStream = new LzoOutputStream(rawstream, compressor); |
54 | 51 | break; |
55 | 52 | case COMPRESS_TYPE_SNAPPY: |
56 | | - outputStream=new SnappyOutputStream(wrapOutputStream(rawstream)); |
| 53 | + outputStream = new SnappyOutputStream(rawstream); |
57 | 54 | break; |
58 | 55 | case COMPRESS_TYPE_ZIP: |
59 | | - ZipOutputStream stream1=new ZipOutputStream(wrapOutputStream(rawstream)); |
60 | | - stream1.putNextEntry(new ZipEntry(content.getFileName()+"."+content.getFileFormat())); |
61 | | - outputStream=stream1; |
| 56 | + ZipOutputStream stream1 = new ZipOutputStream(rawstream); |
| 57 | + stream1.putNextEntry(new ZipEntry(content.getFileName() + "." + content.getFileFormat())); |
| 58 | + outputStream = stream1; |
62 | 59 | break; |
63 | 60 | case COMPRESS_TYPE_LZ4: |
64 | | - outputStream=new LZ4FrameOutputStream(wrapOutputStream(rawstream)); |
| 61 | + outputStream = new LZ4FrameOutputStream(rawstream); |
65 | 62 | break; |
66 | 63 | case COMPRESS_TYPE_LZMA: |
67 | | - outputStream=new XZOutputStream(wrapOutputStream(rawstream),new LZMA2Options()); |
| 64 | + outputStream = new XZOutputStream(rawstream, new LZMA2Options()); |
68 | 65 | break; |
69 | 66 | case COMPRESS_TYPE_ZSTD: |
70 | | - outputStream=new ZstdCompressorOutputStream(wrapOutputStream(rawstream)); |
| 67 | + outputStream = new ZstdCompressorOutputStream(rawstream); |
71 | 68 | break; |
72 | 69 | case COMPRESS_TYPE_BROTLI: |
73 | 70 | throw new IOException("BROTLI encode not support now!"); |
74 | 71 | case COMPRESS_TYPE_XZ: |
75 | | - outputStream=new XZCompressorOutputStream(wrapOutputStream(rawstream)); |
| 72 | + outputStream = new XZCompressorOutputStream(rawstream); |
76 | 73 | break; |
77 | 74 | default: |
78 | | - outputStream=rawstream; |
| 75 | + outputStream = rawstream; |
79 | 76 | } |
80 | 77 |
|
81 | 78 | return outputStream; |
82 | 79 | } |
83 | | - private static OutputStream wrapOutputStream(OutputStream outputStream){ |
84 | | - OutputStream out=null; |
85 | | - if(outputStream instanceof BufferedOutputStream){ |
86 | | - out=outputStream; |
87 | | - }else{ |
88 | | - out=new BufferedOutputStream(outputStream); |
89 | | - } |
90 | | - return out; |
91 | | - } |
92 | 80 | } |
0 commit comments