Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.aimms.pro.exceptions.ExtractionException;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.RandomAccessFile;
- import java.util.ArrayList;
- import java.util.List;
- import net.sf.sevenzipjbinding.ExtractAskMode;
- import net.sf.sevenzipjbinding.ExtractOperationResult;
- import net.sf.sevenzipjbinding.IArchiveExtractCallback;
- import net.sf.sevenzipjbinding.ISequentialOutStream;
- import net.sf.sevenzipjbinding.ISevenZipInArchive;
- import net.sf.sevenzipjbinding.PropID;
- import net.sf.sevenzipjbinding.SevenZip;
- import net.sf.sevenzipjbinding.SevenZipException;
- import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * Class taken from https://gist.github.com/borisbrodski/6120309
- *
- * @author Horia
- */
- public class ArchiveExtractorHelper {
- private final Logger LOG = LoggerFactory.getLogger(getClass());
- class ExtractCallback implements IArchiveExtractCallback {
- private ISevenZipInArchive inArchive;
- private int index;
- private Boolean skipExtraction;
- private OutputStream outputStream;
- private File file;
- ExtractCallback(ISevenZipInArchive inArchive) {
- this.inArchive = inArchive;
- }
- @Override
- public void setTotal(long total) throws SevenZipException {
- }
- @Override
- public void setCompleted(long completeValue) throws SevenZipException {
- }
- @Override
- public ISequentialOutStream getStream(int index,
- ExtractAskMode extractAskMode) throws SevenZipException {
- closeOutputStream();
- this.index = index;
- skipExtraction = (Boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
- String path = (String) inArchive.getProperty(index, PropID.PATH);
- file = new File(outputDirectoryFile, path);
- if (extractAskMode != ExtractAskMode.EXTRACT) {
- if (skipExtraction) {
- createDirectory(file);
- }
- return null;
- }
- createDirectory(file.getParentFile());
- try {
- outputStream = new FileOutputStream(file);
- } catch (FileNotFoundException e) {
- throw new SevenZipException("Error opening file: "
- + file.getAbsolutePath(), e);
- }
- return new ISequentialOutStream() {
- @Override
- public int write(byte[] data) throws SevenZipException {
- try {
- outputStream.write(data);
- } catch (IOException e) {
- throw new SevenZipException("Error writing to file: "
- + file.getAbsolutePath());
- }
- return data.length; // Return amount of proceed data
- }
- };
- }
- private void createDirectory(File parentFile) throws SevenZipException {
- if (!parentFile.exists()) {
- if (!parentFile.mkdirs()) {
- throw new SevenZipException("Error creating directory: "
- + parentFile.getAbsolutePath());
- }
- }
- }
- private void closeOutputStream() throws SevenZipException {
- if (outputStream != null) {
- try {
- outputStream.close();
- } catch (IOException e) {
- throw new SevenZipException("Error closing file: "
- + file.getAbsolutePath());
- }
- }
- }
- @Override
- public void prepareOperation(ExtractAskMode extractAskMode)
- throws SevenZipException {
- }
- @Override
- public void setOperationResult(
- ExtractOperationResult extractOperationResult)
- throws SevenZipException {
- if (skipExtraction) {
- return;
- }
- if (extractOperationResult != ExtractOperationResult.OK) {
- String path = (String) inArchive.getProperty(index, PropID.PATH);
- throw new SevenZipException("Extraction error: " + path);
- } else {
- }
- }
- }
- private File archive;
- private String outputDirectory;
- private File outputDirectoryFile;
- public ArchiveExtractorHelper(File archive, String outputDirectory) {
- this.archive = archive;
- this.outputDirectory = outputDirectory;
- }
- public void extract(String... fileNames) throws ExtractionException {
- checkArchiveFile();
- prepareOutputDirectory();
- extractAllArchive(fileNames);
- }
- private void prepareOutputDirectory() throws ExtractionException {
- outputDirectoryFile = new File(outputDirectory);
- if (!outputDirectoryFile.exists()) {
- outputDirectoryFile.mkdirs();
- } else {
- if (outputDirectoryFile.list().length != 0) {
- LOG.info("Output directory not empty: "
- + outputDirectory);
- }
- }
- }
- private void checkArchiveFile() throws ExtractionException {
- if (!archive.exists()) {
- throw new ExtractionException("Archive file not found: " + archive);
- }
- if (!archive.canRead()) {
- throw new ExtractionException("Can't read archive file: {}" + archive);
- }
- }
- private void extractAllArchive(String... files) throws ExtractionException {
- try (RandomAccessFile randomAccessFile = new RandomAccessFile(archive, "r")) {
- extractArchive(randomAccessFile, files);
- LOG.trace("succesfully extracted archive to {}", archive);
- } catch (FileNotFoundException e) {
- throw new ExtractionException("File not found", e);
- } catch (IOException e) {
- throw new ExtractionException("Error closing archive file", e);
- }
- }
- private int[] transformFileNamesToIndexes(ISevenZipInArchive inArchive, String... fileNames) throws SevenZipException {
- int[] filesToExtract = null;
- if (fileNames == null || fileNames.length == 0) {
- return filesToExtract;
- }
- int count = inArchive.getNumberOfItems();
- List<Integer> itemsToExtract = new ArrayList<>();
- for (String fileName : fileNames) {
- for (int index = 0; index < count; index++) {
- if (!((Boolean) inArchive.getProperty(index, PropID.IS_FOLDER)).booleanValue()) {
- if (fileName.equals(inArchive.getProperty(index, PropID.PATH))) {
- itemsToExtract.add(index);
- break;
- }
- }
- }
- }
- int index = 0;
- filesToExtract = new int[itemsToExtract.size()];
- for (Integer integer : itemsToExtract) {
- filesToExtract[index++] = integer.intValue();
- }
- return filesToExtract;
- }
- private void extractArchive(RandomAccessFile file, String... fileNames) throws ExtractionException {
- ISevenZipInArchive inArchive;
- boolean ok = false;
- int[] filesToExtract;
- try {
- inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(file));
- filesToExtract = transformFileNamesToIndexes(inArchive, fileNames);
- } catch (SevenZipException e) {
- throw new ExtractionException("Error opening archive", e);
- }
- try {
- inArchive.extract(filesToExtract, false, new ExtractCallback(inArchive));
- ok = true;
- } catch (SevenZipException e) {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append("Error extracting archive '");
- stringBuilder.append(archive);
- stringBuilder.append("': ");
- stringBuilder.append(e.getMessage());
- if (e.getCause() != null) {
- stringBuilder.append(" (");
- stringBuilder.append(e.getCause().getMessage());
- stringBuilder.append(')');
- }
- throw new ExtractionException(stringBuilder.toString(), e);
- } finally {
- try {
- inArchive.close();
- } catch (SevenZipException e) {
- if (ok) {
- throw new ExtractionException("Error closing archive", e);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement