1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.plexus.archiver.zstd;
17
18 import java.io.BufferedOutputStream;
19 import java.io.IOException;
20
21 import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream;
22 import org.codehaus.plexus.archiver.ArchiverException;
23 import org.codehaus.plexus.archiver.util.Compressor;
24
25 import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
26 import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
27
28
29
30
31 public class ZstdCompressor extends Compressor {
32
33 private Integer level;
34
35 private ZstdCompressorOutputStream zstdOut;
36
37 public ZstdCompressor() {}
38
39 public void setLevel(Integer level) {
40 this.level = level;
41 }
42
43 @Override
44 public void compress() throws ArchiverException {
45 try {
46 BufferedOutputStream outStream = bufferedOutputStream(fileOutputStream(getDestFile()));
47 if (level == null) {
48 zstdOut = new ZstdCompressorOutputStream(outStream);
49 } else {
50 zstdOut = new ZstdCompressorOutputStream(outStream, level);
51 }
52 compress(getSource(), zstdOut);
53 } catch (IOException ioe) {
54 throw new ArchiverException("Problem creating zstd " + ioe.getMessage(), ioe);
55 }
56 }
57
58 @Override
59 public void close() {
60 try {
61 if (this.zstdOut != null) {
62 this.zstdOut.close();
63 zstdOut = null;
64 }
65 } catch (final IOException e) {
66 throw new ArchiverException("Failure closing target.", e);
67 }
68 }
69 }