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  import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
20  import org.codehaus.plexus.archiver.ArchiverException;
21  import org.codehaus.plexus.archiver.util.Compressor;
22  import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
23  import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
24  
25  /**
26   * @author philip.lourandos
27   * @since 3.3
28   */
29  public class XZCompressor extends Compressor
30  {
31  
32      private XZCompressorOutputStream xzOut;
33  
34      public XZCompressor()
35      {
36      }
37  
38      @Override
39      public void compress() throws ArchiverException
40      {
41          try
42          {
43              xzOut = new XZCompressorOutputStream( bufferedOutputStream( fileOutputStream( getDestFile() ) ) );
44              compress( getSource(), xzOut );
45          }
46          catch ( IOException ioe )
47          {
48              throw new ArchiverException( "Problem creating xz " + ioe.getMessage(), ioe );
49          }
50      }
51  
52      @Override
53      public void close()
54      {
55          try
56          {
57              if ( this.xzOut != null )
58              {
59                  this.xzOut.close();
60                  xzOut = null;
61              }
62          }
63          catch ( final IOException e )
64          {
65              throw new ArchiverException( "Failure closing target.", e );
66          }
67      }
68  
69  }