Example usage for org.apache.commons.compress.archivers ArchiveInputStream getNextEntry

List of usage examples for org.apache.commons.compress.archivers ArchiveInputStream getNextEntry

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveInputStream getNextEntry.

Prototype

public abstract ArchiveEntry getNextEntry() throws IOException;

Source Link

Document

Returns the next Archive Entry in this Stream.

Usage

From source file:com.ttech.cordovabuild.infrastructure.archive.ArchiveUtils.java

private static void extractArchive(Path localPath, ArchiveInputStream ais) throws IOException {
    ArchiveEntry ae;/*from  w  ww.  jav  a2s  . c  o  m*/
    while ((ae = ais.getNextEntry()) != null) {
        if (ae.isDirectory()) {
            continue;
        }
        Path filePath = localPath.resolve(ae.getName());
        if (!filePath.getParent().equals(localPath))
            Files.createDirectories(filePath.getParent());
        try (OutputStream outputStream = Files.newOutputStream(filePath)) {
            IOUtils.copy(ais, outputStream);
        }
    }
}

From source file:com.ALC.SC2BOAserver.util.SC2BOAserverFileUtil.java

/**
 * This method extracts data to a given directory.
 * /*from ww w  .  j  av  a 2 s. c  o m*/
 * @param directory the directory to extract into
 * @param zipIn input stream pointing to the zip file
 * @throws ArchiveException
 * @throws IOException
 * @throws FileNotFoundException
 */

public static void extractZipToDirectory(File directory, InputStream zipIn)
        throws ArchiveException, IOException, FileNotFoundException {

    ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", zipIn);
    while (true) {
        ZipArchiveEntry entry = (ZipArchiveEntry) in.getNextEntry();
        if (entry == null) {
            in.close();
            break;
        }
        //Skip empty files
        if (entry.getName().equals("")) {
            continue;
        }

        if (entry.isDirectory()) {
            File file = new File(directory, entry.getName());
            file.mkdirs();
        } else {
            File outFile = new File(directory, entry.getName());
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(outFile);
            IOUtils.copy(in, out);
            out.flush();
            out.close();
        }
    }
}

From source file:ezbake.deployer.utilities.VersionHelper.java

public static String getVersionFromArtifact(ByteBuffer artifact) throws IOException {
    String versionNumber = null;//from w w  w.j av a2  s .co  m
    try (CompressorInputStream uncompressedInput = new GzipCompressorInputStream(
            new ByteArrayInputStream(artifact.array()))) {
        ArchiveInputStream input = new TarArchiveInputStream(uncompressedInput);
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) input.getNextEntry()) != null) {
            if (!entry.isDirectory() && entry.getName().endsWith(VERSION_FILE)) {
                versionNumber = IOUtils.toString(input, Charsets.UTF_8).trim();
                break;
            }
        }
    }
    return versionNumber;
}

From source file:com.petpet.c3po.gatherer.FileExtractor.java

/**
 * Unpack data from the stream to specified directory.
 * /*from w  ww.  ja  v  a2  s  . co  m*/
 * @param in
 *          stream with tar data
 * @param outputDir
 *          destination directory
 * @return true in case of success, otherwise - false
 */
private static void extract(ArchiveInputStream in, File outputDir) {
    try {
        ArchiveEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            // replace : for windows OS.
            final File file = new File(outputDir, entry.getName().replaceAll(":", "_"));

            if (entry.isDirectory()) {

                if (!file.exists()) {
                    file.mkdirs();
                }

            } else {
                file.getParentFile().mkdirs();
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);

                try {

                    IOUtils.copy(in, out);
                    out.flush();

                } finally {
                    try {
                        out.close();

                    } catch (IOException e) {
                        LOG.debug("An error occurred while closing the output stream for file '{}'. Error: {}",
                                file, e.getMessage());
                    }
                }
            }
        }

    } catch (IOException e) {
        LOG.debug("An error occurred while handling archive file. Error: {}", e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                LOG.debug("An error occurred while closing the archive stream . Error: {}", e.getMessage());
            }
        }
    }
}

From source file:big.zip.java

/**
 * When given a zip file, this method will open it up and extract all files
 * inside into a specific folder on disk. Typically on BIG archives, the zip
 * file only contains a single file with the original file name.
 * @param fileZip           The zip file that we want to extract files from
 * @param folderToExtract   The folder where the extract file will be placed 
 * @return True if no problems occurred, false otherwise
 *//*from  www  .  j  av  a 2s  . co  m*/
public static boolean extract(final File fileZip, final File folderToExtract) {
    // preflight check
    if (fileZip.exists() == false) {
        System.out.println("ZIP126 - Zip file not found: " + fileZip.getAbsolutePath());
        return false;
    }
    // now proceed to extract the files
    try {
        final InputStream inputStream = new FileInputStream(fileZip);
        final ArchiveInputStream ArchiveStream;
        ArchiveStream = new ArchiveStreamFactory().createArchiveInputStream("zip", inputStream);
        final ZipArchiveEntry entry = (ZipArchiveEntry) ArchiveStream.getNextEntry();
        final OutputStream outputStream = new FileOutputStream(new File(folderToExtract, entry.getName()));
        IOUtils.copy(ArchiveStream, outputStream);

        // flush and close all files
        outputStream.flush();
        outputStream.close();
        ArchiveStream.close();
        inputStream.close();
    } catch (ArchiveException ex) {
        Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(zip.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
}

From source file:com.chnoumis.commons.zip.utils.ZipUtils.java

public static void unZip(InputStream is) throws ArchiveException, IOException {
    ArchiveInputStream in = null;
    try {/*from   w  ww .  ja v a2 s .  com*/
        in = new ArchiveStreamFactory().createArchiveInputStream("zip", is);
        ZipArchiveEntry entry = null;
        while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) {
            OutputStream o = new FileOutputStream(entry.getName());
            try {
                IOUtils.copy(in, o);
            } finally {
                o.close();
            }
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    is.close();
}

From source file:com.ifs.megaprofiler.helper.FileExtractor.java

/**
 * Unpack data from the stream to specified directory.
 * /*from   ww w. ja  v a2s  .  c o  m*/
 * @param in
 *            stream with tar data
 * @param outputDir
 *            destination directory
 * @return true in case of success, otherwise - false
 */
private static void extract(ArchiveInputStream in, File outputDir) {
    try {
        ArchiveEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            // replace : for windows OS.
            final File file = new File(outputDir, entry.getName().replaceAll(":", "_"));

            if (entry.isDirectory()) {

                if (!file.exists()) {
                    file.mkdirs();
                }

            } else {
                file.getParentFile().mkdirs();
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);

                try {

                    IOUtils.copy(in, out);
                    out.flush();

                } finally {
                    try {
                        out.close();

                    } catch (IOException e) {
                        // LOG.debug(
                        // "An error occurred while closing the output stream for file '{}'. Error: {}",
                        // file,
                        // e.getMessage() );
                    }
                }
            }
        }

    } catch (IOException e) {
        // LOG.debug("An error occurred while handling archive file. Error: {}",
        // e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // LOG.debug(
                // "An error occurred while closing the archive stream . Error: {}",
                // e.getMessage() );
            }
        }
    }
}

From source file:com.chnoumis.commons.zip.utils.ZipUtils.java

public static List<ZipInfo> unZiptoZipInfo(InputStream is) throws ArchiveException, IOException {
    List<ZipInfo> results = new ArrayList<ZipInfo>();
    ArchiveInputStream in = null;
    try {//w  ww . j  a va 2  s. c om
        in = new ArchiveStreamFactory().createArchiveInputStream("zip", is);
        ZipArchiveEntry entry = null;
        while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) {
            ZipInfo zipInfo = new ZipInfo();
            OutputStream o = new ByteArrayOutputStream();
            try {
                IOUtils.copy(in, o);
            } finally {
                o.close();
            }
            zipInfo.setFileName(entry.getName());
            zipInfo.setFileContent(o.toString().getBytes());
            results.add(zipInfo);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    is.close();

    return results;
}

From source file:com.amazonaws.codepipeline.jenkinsplugin.ExtractionTools.java

private static void extractArchive(final File destination, final ArchiveInputStream archiveInputStream)
        throws IOException {
    final int BUFFER_SIZE = 8192;
    ArchiveEntry entry = archiveInputStream.getNextEntry();

    while (entry != null) {
        final File destinationFile = getDestinationFile(destination, entry.getName());

        if (entry.isDirectory()) {
            destinationFile.mkdir();/*w  ww .j a  v  a2  s. c o m*/
        } else {
            destinationFile.getParentFile().mkdirs();
            try (final OutputStream fileOutputStream = new FileOutputStream(destinationFile)) {
                final byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead;

                while ((bytesRead = archiveInputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, bytesRead);
                }
            }
        }

        entry = archiveInputStream.getNextEntry();
    }
}

From source file:com.bahmanm.karun.Utils.java

/**
 * Extracts a gzip'ed tar archive.//w w  w  . ja  v a 2  s .  c o m
 * 
 * @param archivePath Path to archive
 * @param destDir Destination directory
 * @throws IOException 
 */
public synchronized static void extractTarGz(String archivePath, File destDir)
        throws IOException, ArchiveException {
    // copy
    File tarGzFile = File.createTempFile("karuntargz", "", destDir);
    copyFile(archivePath, tarGzFile.getAbsolutePath());

    // decompress
    File tarFile = File.createTempFile("karuntar", "", destDir);
    FileInputStream fin = new FileInputStream(tarGzFile);
    BufferedInputStream bin = new BufferedInputStream(fin);
    FileOutputStream fout = new FileOutputStream(tarFile);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(bin);
    final byte[] buffer = new byte[1024];
    int n = 0;
    while (-1 != (n = gzIn.read(buffer))) {
        fout.write(buffer, 0, n);
    }
    bin.close();
    fin.close();
    gzIn.close();
    fout.close();

    // extract
    final InputStream is = new FileInputStream(tarFile);
    ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) ain.getNextEntry()) != null) {
        OutputStream out;
        if (entry.isDirectory()) {
            File f = new File(destDir, entry.getName());
            f.mkdirs();
            continue;
        } else
            out = new FileOutputStream(new File(destDir, entry.getName()));
        IOUtils.copy(ain, out);
        out.close();
    }
    ain.close();
    is.close();
}