|
| 1 | +/* |
| 2 | + * Copyright 2022 The Apache Software Foundation |
| 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.zstd; |
| 17 | + |
| 18 | +import javax.inject.Named; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import org.codehaus.plexus.archiver.AbstractArchiver; |
| 22 | +import org.codehaus.plexus.archiver.ArchiveEntry; |
| 23 | +import org.codehaus.plexus.archiver.ArchiverException; |
| 24 | +import org.codehaus.plexus.archiver.ResourceIterator; |
| 25 | +import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException; |
| 26 | + |
| 27 | +/** |
| 28 | + * Zstd archiver. |
| 29 | + */ |
| 30 | +@Named( "zst" ) |
| 31 | +public class ZstdArchiver extends AbstractArchiver |
| 32 | +{ |
| 33 | + |
| 34 | + private final ZstdCompressor compressor = new ZstdCompressor(); |
| 35 | + |
| 36 | + public ZstdArchiver() |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Set compression level |
| 42 | + */ |
| 43 | + public void setLevel( Integer level ) |
| 44 | + throws ArchiverException |
| 45 | + { |
| 46 | + compressor.setLevel( level ); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void execute() throws ArchiverException, IOException |
| 51 | + { |
| 52 | + if ( !checkForced() ) |
| 53 | + { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + ResourceIterator iter = getResources(); |
| 58 | + if ( !iter.hasNext() ) |
| 59 | + { |
| 60 | + throw new EmptyArchiveException( "archive cannot be empty" ); |
| 61 | + } |
| 62 | + ArchiveEntry entry = iter.next(); |
| 63 | + if ( iter.hasNext() ) |
| 64 | + { |
| 65 | + throw new ArchiverException( "There is more than one file in input." ); |
| 66 | + } |
| 67 | + compressor.setSource( entry.getResource() ); |
| 68 | + compressor.setDestFile( getDestFile() ); |
| 69 | + compressor.compress(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean isSupportingForced() |
| 74 | + { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void close() throws IOException |
| 80 | + { |
| 81 | + compressor.close(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected String getArchiveType() |
| 86 | + { |
| 87 | + return "zstd"; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments