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 javax.annotation.Nonnull;
20  import javax.inject.Named;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import io.airlift.compress.snappy.SnappyFramedInputStream;
27  import org.codehaus.plexus.archiver.AbstractUnArchiver;
28  import org.codehaus.plexus.archiver.ArchiverException;
29  
30  import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
31  import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
32  import static org.codehaus.plexus.archiver.util.Streams.copyFully;
33  import static org.codehaus.plexus.archiver.util.Streams.fileInputStream;
34  import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
35  
36  /**
37   * Unarchiver for snappy-compressed files.
38   */
39  @Named("snappy")
40  public class SnappyUnArchiver extends AbstractUnArchiver {
41  
42      private static final String OPERATION_SNAPPY = "snappy";
43  
44      public SnappyUnArchiver() {}
45  
46      public SnappyUnArchiver(File sourceFile) {
47          super(sourceFile);
48      }
49  
50      @Override
51      protected void execute() throws ArchiverException {
52          if (getSourceFile().lastModified() > getDestFile().lastModified()) {
53              getLogger()
54                      .info("Expanding " + getSourceFile().getAbsolutePath() + " to "
55                              + getDestFile().getAbsolutePath());
56  
57              copyFully(
58                      getSnappyInputStream(bufferedInputStream(fileInputStream(getSourceFile(), OPERATION_SNAPPY))),
59                      bufferedOutputStream(fileOutputStream(getDestFile(), OPERATION_SNAPPY)),
60                      OPERATION_SNAPPY);
61          }
62      }
63  
64      public static @Nonnull SnappyFramedInputStream getSnappyInputStream(InputStream bis) throws ArchiverException {
65          try {
66              return new SnappyFramedInputStream(bis, true);
67          } catch (IOException e) {
68              throw new ArchiverException("Trouble creating Snappy compressor, invalid file ?", e);
69          }
70      }
71  
72      @Override
73      protected void execute(String path, File outputDirectory) {
74          throw new UnsupportedOperationException("Targeted extraction not supported in Snappy format.");
75      }
76  }