View Javadoc
1   /*
2    * Copyright 2016 Codehaus.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.archiver.xz;
17  
18  import java.io.IOException;
19  
20  import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
21  import org.codehaus.plexus.archiver.ArchiverException;
22  import org.codehaus.plexus.archiver.util.Compressor;
23  
24  import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
25  import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
26  
27  /**
28   * @author philip.lourandos
29   * @since 3.3
30   */
31  public class XZCompressor extends Compressor {
32  
33      private XZCompressorOutputStream xzOut;
34  
35      public XZCompressor() {}
36  
37      @Override
38      public void compress() throws ArchiverException {
39          try {
40              xzOut = new XZCompressorOutputStream(bufferedOutputStream(fileOutputStream(getDestFile())));
41              compress(getSource(), xzOut);
42          } catch (IOException ioe) {
43              throw new ArchiverException("Problem creating xz " + ioe.getMessage(), ioe);
44          }
45      }
46  
47      @Override
48      public void close() {
49          try {
50              if (this.xzOut != null) {
51                  this.xzOut.close();
52                  xzOut = null;
53              }
54          } catch (final IOException e) {
55              throw new ArchiverException("Failure closing target.", e);
56          }
57      }
58  }