Skip to content

Commit

Permalink
Repair the tool decompressing a file (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
OPeyrusse authored Jun 7, 2024
1 parent b75f5b0 commit 2f1a333
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Default owner for the whole repo
* @activeviam/activepivot-core-owners
* @activeviam/atoti-server-services-owners
14 changes: 9 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@
</goals>
<id>unsnappy</id>
<configuration>
<mainClass>com.activeviam.mac.Tools</mainClass>
<classpathScope>test</classpathScope>
<mainClass>com.activeviam.mac.tools.Tools</mainClass>
<arguments>
<argument>extract</argument>
<argument>${unsnappy.file}</argument>
Expand Down Expand Up @@ -330,11 +329,16 @@
<licenseMerge>Apache License 2.0|The Apache Software License, Version 2.0</licenseMerge>
<licenseMerge>BSD License|BSD</licenseMerge>
<licenseMerge>BSD License|The BSD License</licenseMerge>
<licenseMerge>Common Development and Distribution License 1.0|Common Development and Distribution License</licenseMerge>
<licenseMerge>Common Development and Distribution License 1.0|Common Development and Distribution License (CDDL) v1.0</licenseMerge>
<licenseMerge>Common Development and Distribution License 1.0|Common Development and Distribution
License
</licenseMerge>
<licenseMerge>Common Development and Distribution License 1.0|Common Development and Distribution
License (CDDL) v1.0
</licenseMerge>
<licenseMerge>Eclipse Public License 1.0|Eclipse Public License - Version 1.0</licenseMerge>
<licenseMerge>Eclipse Public License 1.0|Eclipse Public License (EPL), Version 1.0</licenseMerge>
<licenseMerge>GPL2 w/ CPE|GNU General Public License, Version 2 with the Classpath Exception</licenseMerge>
<licenseMerge>GPL2 w/ CPE|GNU General Public License, Version 2 with the Classpath Exception
</licenseMerge>
<licenseMerge>LGPL 2.1|LGPL, version 2.1</licenseMerge>
<licenseMerge>LGPL 2.1|GNU Lesser General Public License 2.1</licenseMerge>
<licenseMerge>MIT License|The MIT License</licenseMerge>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* property of ActiveViam. Any unauthorized use
* reproduction or transfer of this material is strictly prohibited
*/
package com.activeviam.mac;
package com.activeviam.mac.tools;

import com.activeviam.fwk.ActiveViamRuntimeException;
import com.qfs.pivot.monitoring.impl.MemoryStatisticSerializerUtil;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -18,6 +19,8 @@
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorInputStream;

/**
Expand All @@ -27,13 +30,13 @@
*/
public class Tools {

public static int main(final String[] args) {
private static final Logger LOGGER = Logger.getLogger("mac.tools");

public static void main(final String[] args) {
if ("extract".equals(args[0])) {
extractSnappyFileOrDirectory(args[1]);
return 0;
} else {
System.err.println("Unsupported command. Got " + Arrays.toString(args));
return 1;
LOGGER.severe(() -> "Unsupported command. Got " + Arrays.toString(args));
}
}

Expand All @@ -56,13 +59,13 @@ public static void extractSnappyFileOrDirectory(final Path path) {

protected static void assertFileExists(final Path path) {
if (!Files.exists(path)) {
throw new RuntimeException(path + " does not point to a valid file or directory");
throw new IllegalArgumentException(path + " does not point to a valid file or directory");
}
}

public static void extractSnappyDirectory(final Path path) {
try {
Files.list(path).forEach(Tools::extractSnappyFileOrDirectory);
try (final var fileStream = Files.list(path)) {
fileStream.forEach(Tools::extractSnappyFileOrDirectory);
} catch (IOException exception) {
throw new UncheckedIOException(exception);
}
Expand All @@ -73,11 +76,12 @@ public static void extractSnappyFile(final Path path) {
final String extension = MemoryStatisticSerializerUtil.COMPRESSED_FILE_EXTENSION;
boolean isCompressedFile = pathAsString.endsWith("." + extension);
if (!isCompressedFile) {
System.out.println(
"File "
+ pathAsString
+ " was skipped as it did not match the list of compressed extensions: "
+ MemoryStatisticSerializerUtil.COMPRESSED_FILE_EXTENSION);
LOGGER.info(
() ->
"File "
+ pathAsString
+ " was skipped as it did not match the list of compressed extensions: "
+ MemoryStatisticSerializerUtil.COMPRESSED_FILE_EXTENSION);
return;
}

Expand All @@ -91,18 +95,27 @@ public static void extractSnappyFile(final Path path) {
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(String.format("File `%s` does not exist", path), e);
}
final InputStream inputStream;
try {
inputStream = new FramedSnappyCompressorInputStream(rawInputStream);
} catch (IOException e) {
throw new RuntimeException(String.format("Cannot read `%s` as a Snappy file", path), e);
}
try {
Files.copy(inputStream, uncompressedPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(
String.format("Failed to extract `%s` to `%s`", path, uncompressedPath), e);
final InputStream inputStream;
try {
inputStream = new FramedSnappyCompressorInputStream(rawInputStream);
} catch (IOException e) {
throw new ActiveViamRuntimeException(
String.format("Cannot read `%s` as a Snappy file", path), e);
}
try {
Files.copy(inputStream, uncompressedPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new ActiveViamRuntimeException(
String.format("Failed to extract `%s` to `%s`", path, uncompressedPath), e);
}
} finally {
try {
rawInputStream.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to close the input stream", e);
}
}
System.out.printf("File `%s` extracted to `%s`%n", path, uncompressedPath);
LOGGER.log(Level.INFO, "File `{0}` extracted to `{1}`", new Object[] {path, uncompressedPath});
}
}

0 comments on commit 2f1a333

Please sign in to comment.