Extract7ZipUtil.java

package ui.common.util;

import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Extract7ZipUtil {

    private class ExtractCallback implements IArchiveExtractCallback {
        private final IInArchive inArchive;
        private OutputStream outputStream;
        private File file;

        private ExtractCallback(IInArchive inArchive) {
            this.inArchive = inArchive;
        }

        @Override
        public void setTotal(long total) {

        }

        @Override
        public void setCompleted(long completeValue) {

        }

        @Override
        public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
            if (outputStream != null) {
                try {
                    outputStream.close();
                    outputStream = null;
                } catch (IOException e1) {
                    throw new SevenZipException("Error closing file: " + file.getAbsolutePath());
                }
            }

            boolean isFolder = (Boolean) inArchive.getProperty(index, PropID.IS_FOLDER);

            if (extractAskMode != ExtractAskMode.EXTRACT) {
                // Skipped files or files being tested
                return null;
            }

            String path = (String) inArchive.getProperty(index, PropID.PATH);
            file = new File(outputDirectoryFile, path);
            if (isFolder) {
                createDirectory(file);
                return null;
            }

            createDirectory(file.getParentFile());
            filesList.add(file);

            try {
                outputStream = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                throw new SevenZipException("Error opening file: " + file.getAbsolutePath(), e);
            }

            return data -> {
                try {
                    outputStream.write(data);
                } catch (IOException e) {
                    throw new SevenZipException("Error writing to file: " + file.getAbsolutePath());
                }
                return data.length; // Return the amount of consumed data
            };
        }

        private void createDirectory(File parentFile) throws SevenZipException {
            if (!parentFile.exists()) {
                if (!parentFile.mkdirs()) {
                    throw new SevenZipException("Error creating directory: " + parentFile.getAbsolutePath());
                }
            }
        }

        @Override
        public void prepareOperation(ExtractAskMode extractAskMode) {

        }

        @Override
        public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
            if (outputStream != null) {
                try {
                    outputStream.close();
                    outputStream = null;
                } catch (IOException e) {
                    throw new SevenZipException("Error closing file: " + file.getAbsolutePath());
                }
            }
        }

    }

    private final File archive;
    private final File outputDirectoryFile;
    private final List<File> filesList = new ArrayList<>();

    public Extract7ZipUtil(File archive, File outputDirectory) {
        this.archive = archive;
        this.outputDirectoryFile = outputDirectory;
    }

    public void extract() throws IOException {
        try (RandomAccessFile randomAccessFile = new RandomAccessFile(archive, "r")) {
            try (IInArchive inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile))) {
                inArchive.extract(null, false, new ExtractCallback(inArchive));
            }
        }
    }

    public File getZipFile() {
        return new File(archive.getAbsolutePath()) {
            @Override
            public File[] listFiles() {
                return filesList.toArray(new File[0]);
            }
        };

    }

}