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.annotation.Nonnull;
19  import javax.inject.Named;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import org.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream;
26  import org.codehaus.plexus.archiver.AbstractUnArchiver;
27  import org.codehaus.plexus.archiver.ArchiverException;
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 zstd-compressed files.
37   */
38  @Named("zst")
39  public class ZstdUnArchiver extends AbstractUnArchiver {
40  
41      private static final String OPERATION_ZSTD = "zstd";
42  
43      public ZstdUnArchiver() {}
44  
45      public ZstdUnArchiver(File source) {
46          super(source);
47      }
48  
49      @Override
50      protected void execute() throws ArchiverException {
51          if (getSourceFile().lastModified() > getDestFile().lastModified()) {
52              getLogger()
53                      .info("Expanding " + getSourceFile().getAbsolutePath() + " to "
54                              + getDestFile().getAbsolutePath());
55  
56              copyFully(
57                      getZstdInputStream(bufferedInputStream(fileInputStream(getSourceFile(), OPERATION_ZSTD))),
58                      bufferedOutputStream(fileOutputStream(getDestFile(), OPERATION_ZSTD)),
59                      OPERATION_ZSTD);
60          }
61      }
62  
63      public static @Nonnull ZstdCompressorInputStream getZstdInputStream(InputStream in) throws ArchiverException {
64          try {
65              return new ZstdCompressorInputStream(in);
66          } catch (IOException ioe) {
67              throw new ArchiverException("Trouble creating Zstd compressor, invalid file ?", ioe);
68          }
69      }
70  
71      @Override
72      protected void execute(String path, File outputDirectory) throws ArchiverException {
73          throw new UnsupportedOperationException("Targeted execution not supported in zstd format");
74      }
75  }