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