Example usage for org.apache.commons.compress.archivers ArchiveEntry getName

List of usage examples for org.apache.commons.compress.archivers ArchiveEntry getName

Introduction

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

Prototype

public String getName();

Source Link

Document

The name of the entry in the archive.

Usage

From source file:de.dentrassi.pm.utils.deb.Packages.java

public static Map<String, String> parseControlFile(final File packageFile) throws IOException, ParserException {
    try (final ArArchiveInputStream in = new ArArchiveInputStream(new FileInputStream(packageFile))) {
        ArchiveEntry ar;
        while ((ar = in.getNextEntry()) != null) {
            if (!ar.getName().equals("control.tar.gz")) {
                continue;
            }/*ww w  .j a v a2  s  .co  m*/
            try (final TarArchiveInputStream inputStream = new TarArchiveInputStream(new GZIPInputStream(in))) {
                TarArchiveEntry te;
                while ((te = inputStream.getNextTarEntry()) != null) {
                    String name = te.getName();
                    if (name.startsWith("./")) {
                        name = name.substring(2);
                    }
                    if (!name.equals("control")) {
                        continue;
                    }
                    return parseControlFile(inputStream);
                }
            }
        }
    }
    return null;
}

From source file:fr.gael.dhus.util.UnZip.java

public static void unCompress(String zip_file, String output_folder)
        throws IOException, CompressorException, ArchiveException {
    ArchiveInputStream ais = null;/*from   w w w. j a v a 2 s . co  m*/
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    FileInputStream fis = new FileInputStream(new File(zip_file));

    if (zip_file.toLowerCase().endsWith(".tar")) {
        ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
    } else if (zip_file.toLowerCase().endsWith(".zip")) {
        ais = asf.createArchiveInputStream(ArchiveStreamFactory.ZIP, fis);
    } else if (zip_file.toLowerCase().endsWith(".tgz") || zip_file.toLowerCase().endsWith(".tar.gz")) {
        CompressorInputStream cis = new CompressorStreamFactory()
                .createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
        ais = asf.createArchiveInputStream(new BufferedInputStream(cis));
    } else {
        try {
            fis.close();
        } catch (IOException e) {
            LOGGER.warn("Cannot close FileInputStream:", e);
        }
        throw new IllegalArgumentException("Format not supported: " + zip_file);
    }

    File output_file = new File(output_folder);
    if (!output_file.exists())
        output_file.mkdirs();

    // copy the existing entries
    ArchiveEntry nextEntry;
    while ((nextEntry = ais.getNextEntry()) != null) {
        File ftemp = new File(output_folder, nextEntry.getName());
        if (nextEntry.isDirectory()) {
            ftemp.mkdir();
        } else {
            FileOutputStream fos = FileUtils.openOutputStream(ftemp);
            IOUtils.copy(ais, fos);
            fos.close();
        }
    }
    ais.close();
    fis.close();
}

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

private static void extractArchive(Path localPath, ArchiveInputStream ais) throws IOException {
    ArchiveEntry ae;
    while ((ae = ais.getNextEntry()) != null) {
        if (ae.isDirectory()) {
            continue;
        }//w ww.j a  v a 2  s . co  m
        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.audiveris.installer.Expander.java

/**
 * Untar an input file into an output directory.
 *
 * @param inFile the input .tar file// w w  w  . j  a v  a2s  .c o m
 * @param outDir the output directory
 * @throws IOException
 * @throws FileNotFoundException
 * @throws ArchiveException
 *
 * @return The list of files with the untared content.
 */
public static List<File> unTar(final File inFile, final File outDir)
        throws FileNotFoundException, IOException, ArchiveException {
    logger.debug("Untaring {} to dir {}", inFile, outDir);
    assert inFile.getName().endsWith(TAR_EXT);

    final List<File> untaredFiles = new ArrayList<File>();
    final InputStream is = new FileInputStream(inFile);
    final TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    ArchiveEntry entry;

    while ((entry = tis.getNextEntry()) != null) {
        final File outFile = new File(outDir, entry.getName());

        if (entry.isDirectory()) {
            logger.debug("Attempting to write output dir {}", outFile);

            if (!outFile.exists()) {
                logger.debug("Attempting to create output dir {}", outFile);

                if (!outFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s", outFile.getAbsolutePath()));
                }
            }
        } else {
            logger.debug("Creating output file {}", outFile);
            streamToFile(tis, outFile);
        }

        untaredFiles.add(outFile);
    }

    tis.close();

    return untaredFiles;
}

From source file:edu.unc.lib.dl.util.ZipFileUtil.java

/**
 * Unzip to the given directory, creating subdirectories as needed, and
 * ignoring empty directories. Uses apache zip tools until java utilies are
 * updated to support utf-8.//  ww  w  .j a v  a2  s.  com
 */
public static void unzip(InputStream is, File destDir) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(is);
    try (ZipArchiveInputStream zis = new ZipArchiveInputStream(bis)) {
        ArchiveEntry entry = null;
        while ((entry = zis.getNextZipEntry()) != null) {
            if (!entry.isDirectory()) {
                File f = new File(destDir, entry.getName());

                if (!isFileInsideDirectory(f, destDir)) {
                    throw new IOException(
                            "Attempt to write to path outside of destination directory: " + entry.getName());
                }

                f.getParentFile().mkdirs();
                int count;
                byte data[] = new byte[8192];
                // write the files to the disk
                try (FileOutputStream fos = new FileOutputStream(f);
                        BufferedOutputStream dest = new BufferedOutputStream(fos, 8192)) {
                    while ((count = zis.read(data, 0, 8192)) != -1) {
                        dest.write(data, 0, count);
                    }
                }
            }
        }
    }
}

From source file:com.continuuity.weave.kafka.client.KafkaTest.java

private static File extractKafka() throws IOException, ArchiveException, CompressorException {
    File kafkaExtract = TMP_FOLDER.newFolder();
    InputStream kakfaResource = KafkaTest.class.getClassLoader().getResourceAsStream("kafka-0.7.2.tgz");
    ArchiveInputStream archiveInput = new ArchiveStreamFactory()
            .createArchiveInputStream(ArchiveStreamFactory.TAR, new CompressorStreamFactory()
                    .createCompressorInputStream(CompressorStreamFactory.GZIP, kakfaResource));

    try {/*  ww w .j  a va 2 s  .c om*/
        ArchiveEntry entry = archiveInput.getNextEntry();
        while (entry != null) {
            File file = new File(kafkaExtract, entry.getName());
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                ByteStreams.copy(archiveInput, Files.newOutputStreamSupplier(file));
            }
            entry = archiveInput.getNextEntry();
        }
    } finally {
        archiveInput.close();
    }
    return kafkaExtract;
}

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 w w  . java  2s .co 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.petpet.c3po.gatherer.FileExtractor.java

/**
 * Unpack data from the stream to specified directory.
 * /* ww  w. jav  a2 s  . 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:net.shopxx.util.CompressUtils.java

public static void extract(File srcFile, File destDir) {
    Assert.notNull(srcFile);//from   w w  w  .  j  a  v  a  2s  .  c o m
    Assert.state(srcFile.exists());
    Assert.state(srcFile.isFile());
    Assert.notNull(destDir);
    Assert.state(!destDir.exists() || destDir.isDirectory());

    destDir.mkdirs();
    ArchiveInputStream archiveInputStream = null;
    try {
        archiveInputStream = new ArchiveStreamFactory()
                .createArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile)));
        ArchiveEntry archiveEntry;
        while ((archiveEntry = archiveInputStream.getNextEntry()) != null) {
            if (archiveEntry.isDirectory()) {
                new File(destDir, archiveEntry.getName()).mkdirs();
            } else {
                OutputStream outputStream = null;
                try {
                    outputStream = new BufferedOutputStream(
                            new FileOutputStream(new File(destDir, archiveEntry.getName())));
                    IOUtils.copy(archiveInputStream, outputStream);
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e.getMessage(), e);
                } catch (IOException e) {
                    throw new RuntimeException(e.getMessage(), e);
                } finally {
                    IOUtils.closeQuietly(outputStream);
                }
            }
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (ArchiveException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(archiveInputStream);
    }
}

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

/**
 * Unpack data from the stream to specified directory.
 * //from  w  w w .  j a  v a  2 s . 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() );
            }
        }
    }
}