View Javadoc
1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.codehaus.plexus.archiver.snappy;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import javax.annotation.Nonnull;
23  import org.codehaus.plexus.archiver.AbstractUnArchiver;
24  import org.codehaus.plexus.archiver.ArchiverException;
25  import org.iq80.snappy.SnappyInputStream;
26  import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
27  import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
28  import static org.codehaus.plexus.archiver.util.Streams.copyFully;
29  import static org.codehaus.plexus.archiver.util.Streams.fileInputStream;
30  import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
31  
32  /**
33   * Unarchiver for snappy-compressed files.
34   */
35  public class SnappyUnArchiver
36      extends AbstractUnArchiver
37  {
38  
39      private final static String OPERATION_SNAPPY = "snappy";
40  
41      public SnappyUnArchiver()
42      {
43      }
44  
45      public SnappyUnArchiver( File sourceFile )
46      {
47          super( sourceFile );
48      }
49  
50      @Override
51      protected void execute()
52          throws ArchiverException
53      {
54          if ( getSourceFile().lastModified() > getDestFile().lastModified() )
55          {
56              getLogger().info(
57                  "Expanding " + getSourceFile().getAbsolutePath() + " to " + getDestFile().getAbsolutePath() );
58  
59              copyFully(
60                  getSnappyInputStream( bufferedInputStream( fileInputStream( getSourceFile(), OPERATION_SNAPPY ) ) ),
61                  bufferedOutputStream( fileOutputStream( getDestFile(), OPERATION_SNAPPY ) ), OPERATION_SNAPPY );
62  
63          }
64      }
65  
66      public static @Nonnull
67      SnappyInputStream getSnappyInputStream( InputStream bis )
68          throws ArchiverException
69      {
70          try
71          {
72              return new SnappyInputStream( bis );
73          }
74          catch ( IOException e )
75          {
76              throw new ArchiverException( "Trouble creating Snappy compressor, invalid file ?", e );
77          }
78      }
79  
80      @Override
81      protected void execute( String path, File outputDirectory )
82      {
83          throw new UnsupportedOperationException( "Targeted extraction not supported in Snappy format." );
84      }
85  
86  }