Search in sources :

Example 36 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project phoenicis by PhoenicisOrg.

the class Zip method uncompress.

/**
 * Uncompress a tar
 *
 * @param countingInputStream to count the number of byte extracted
 * @param outputDir           The directory where files should be extracted
 * @return A list of extracted files
 * @throws ArchiveException if the process fails
 */
private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream, final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) {
    final List<File> uncompressedFiles = new LinkedList<>();
    try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("zip", inputStream)) {
        ZipArchiveEntry entry;
        while ((entry = (ZipArchiveEntry) debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                LOGGER.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    LOGGER.info(String.format("Attempting to createPrefix output directory %s.", outputFile.getAbsolutePath()));
                    Files.createDirectories(outputFile.toPath());
                }
            } else {
                LOGGER.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
                outputFile.getParentFile().mkdirs();
                try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) {
                    IOUtils.copy(debInputStream, outputFileStream);
                }
            }
            uncompressedFiles.add(outputFile);
            stateCallback.accept(new ProgressEntity.Builder().withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100).withProgressText("Extracting " + outputFile.getName()).build());
        }
        return uncompressedFiles;
    } catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) {
        throw new ArchiveException("Unable to extract the file", e);
    }
}
Also used : LinkedList(java.util.LinkedList) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Example 37 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project jbpm-work-items by kiegroup.

the class ArchiveWorkItemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    String archive = (String) workItem.getParameter("Archive");
    List<File> files = (List<File>) workItem.getParameter("Files");
    try {
        OutputStream outputStream = new FileOutputStream(new File(archive));
        ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar", outputStream);
        if (files != null) {
            for (File file : files) {
                final TarArchiveEntry entry = new TarArchiveEntry("testdata/test1.xml");
                entry.setModTime(0);
                entry.setSize(file.length());
                entry.setUserId(0);
                entry.setGroupId(0);
                entry.setMode(0100000);
                os.putArchiveEntry(entry);
                IOUtils.copy(new FileInputStream(file), os);
            }
        }
        os.closeArchiveEntry();
        os.close();
        manager.completeWorkItem(workItem.getId(), null);
    } catch (Throwable cause) {
        handleException(cause);
        manager.abortWorkItem(workItem.getId());
    }
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) FileOutputStream(java.io.FileOutputStream) List(java.util.List) File(java.io.File) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 38 with ArchiveStreamFactory

use of org.apache.commons.compress.archivers.ArchiveStreamFactory in project POL-POM-5 by PlayOnLinux.

the class Tar method uncompress.

/**
 * Uncompress a tar
 *
 * @param countingInputStream
 *            to count the number of byte extracted
 * @param outputDir
 *            The directory where files should be extracted
 * @return A list of extracted files
 * @throws ArchiveException
 *             if the process fails
 */
private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream, final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) {
    final List<File> uncompressedFiles = new LinkedList<>();
    try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar", inputStream)) {
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                LOGGER.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    LOGGER.info(String.format("Attempting to createPrefix output directory %s.", outputFile.getAbsolutePath()));
                    Files.createDirectories(outputFile.toPath());
                }
            } else {
                LOGGER.info(String.format("Creating output file %s (%s).", outputFile.getAbsolutePath(), entry.getMode()));
                if (entry.isSymbolicLink()) {
                    Files.createSymbolicLink(Paths.get(outputFile.getAbsolutePath()), Paths.get(entry.getLinkName()));
                } else {
                    try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) {
                        IOUtils.copy(debInputStream, outputFileStream);
                        Files.setPosixFilePermissions(Paths.get(outputFile.getPath()), fileUtilities.octToPosixFilePermission(entry.getMode()));
                    }
                }
            }
            uncompressedFiles.add(outputFile);
            stateCallback.accept(new ProgressEntity.Builder().withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100).withProgressText("Extracting " + outputFile.getName()).build());
        }
        return uncompressedFiles;
    } catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) {
        throw new ArchiveException("Unable to extract the file", e);
    }
}
Also used : LinkedList(java.util.LinkedList) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream)

Aggregations

ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)38 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)18 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)14 File (java.io.File)12 FileInputStream (java.io.FileInputStream)12 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)11 InputStream (java.io.InputStream)10 BufferedInputStream (java.io.BufferedInputStream)9 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 LinkedList (java.util.LinkedList)9 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)9 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)8 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)8 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)8 OutputStream (java.io.OutputStream)7 GZIPInputStream (java.util.zip.GZIPInputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Path (java.nio.file.Path)4