be.yildizgames.common.compression.sevenzip.SevenZipFileInfoRetriever Maven / Gradle / Ivy
/*
* This file is part of the Yildiz-Engine project, licenced under the MIT License (MIT)
*
* Copyright (c) 2021-2022 Grégory Van den Borre
*
* More infos available: https://engine.yildiz-games.be
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package be.yildizgames.common.compression.sevenzip;
import be.yildizgames.common.compression.FileInfo;
import be.yildizgames.common.compression.FileInfoRetriever;
import be.yildizgames.common.hashing.Algorithm;
import be.yildizgames.common.hashing.FileHash;
import be.yildizgames.common.hashing.HashingFactory;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZFileOptions;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* This class will provide the file infos from a 7zip archive.
*
* @author Grégory Van den Borre
*/
public class SevenZipFileInfoRetriever implements FileInfoRetriever {
/**
* Path of the archive, never null.
*/
private final Path path;
/**
* Construct a new instance.
*
* @param path Path of the archive, cannot be null.
*/
public SevenZipFileInfoRetriever(final Path path) {
super();
this.path = Objects.requireNonNull(path);
}
@Override
public final List getFileInfo(Algorithm... algorithms) {
if (algorithms == null || algorithms.length == 0) {
return noCompute();
}
return computeHashes(algorithms);
}
private List noCompute() {
var result = new ArrayList();
try (var sevenZFile = new SevenZFile(this.path.toFile(), SevenZFileOptions.builder().withTryToRecoverBrokenArchives(true).build())) {
for (var e : sevenZFile.getEntries()) {
if (!e.isDirectory()) {
result.add(new FileInfo(e.getName(), List.of()));
}
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
return result;
}
private List computeHashes(Algorithm... algorithms) {
var result = new ArrayList();
Map> hashes = new HashMap<>();
for (var a : algorithms) {
try (var sevenZFile = new SevenZFile(this.path.toFile(), SevenZFileOptions.builder().withTryToRecoverBrokenArchives(true).build())) {
for (var e : sevenZFile.getEntries()) {
if (!e.isDirectory()) {
var is = sevenZFile.getInputStream(e);
var hash = HashingFactory.get(a).compute(is, (int) e.getSize());
if (!hashes.containsKey(e.getName())) {
hashes.put(e.getName(), new ArrayList<>());
}
hashes.get(e.getName()).add(hash);
}
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
for(var entry : hashes.entrySet()) {
result.add(new FileInfo(entry.getKey(), entry.getValue()));
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy