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