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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

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

/**
 * This method extracts data to a given directory.
 * //from   w w w.  java  2s .com
 * @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: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  w ww . j av a 2  s. 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 w w .  j  a  va  2 s .  c om*/
        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.bahmanm.karun.Utils.java

/**
 * Extracts a gzip'ed tar archive.//from w w  w  .  j a 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();
}

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 {//from   w  w  w .  j  ava2  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.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 {/*  w  w w .ja v a2s .  c  o  m*/
        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:deployer.TestUtils.java

public static ByteBuffer createSampleOpenShiftWebAppTarBall() throws IOException, ArchiveException {
    ByteArrayInputStream bis = new ByteArrayInputStream(createSampleAppTarBall(ArtifactType.WebApp).array());
    CompressorInputStream cis = new GzipCompressorInputStream(bis);
    ArchiveInputStream ais = new TarArchiveInputStream(cis);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(bis.available() + 2048);
    CompressorOutputStream cos = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(cos);

    ArchiveEntry nextEntry;/*  www. java  2 s  .co m*/
    while ((nextEntry = ais.getNextEntry()) != null) {
        aos.putArchiveEntry(nextEntry);
        IOUtils.copy(ais, aos);
        aos.closeArchiveEntry();
    }
    ais.close();
    cis.close();
    bis.close();

    TarArchiveEntry entry = new TarArchiveEntry(
            Paths.get(".openshift", CONFIG_DIRECTORY, "/standalone.xml").toFile());
    byte[] xmlData = SAMPLE_STANDALONE_DATA.getBytes();
    entry.setSize(xmlData.length);
    aos.putArchiveEntry(entry);
    IOUtils.write(xmlData, aos);
    aos.closeArchiveEntry();

    aos.finish();
    cos.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}

From source file:deployer.TestUtils.java

public static ByteBuffer createSampleOpenShiftWebAppTarBallWithEmptyFiles(String[] filepaths)
        throws IOException, ArchiveException {
    ByteArrayInputStream bis = new ByteArrayInputStream(createSampleAppTarBall(ArtifactType.WebApp).array());
    CompressorInputStream cis = new GzipCompressorInputStream(bis);
    ArchiveInputStream ais = new TarArchiveInputStream(cis);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(bis.available() + 2048);
    CompressorOutputStream cos = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(cos);

    ArchiveEntry nextEntry;//w ww  .  j  a  va  2  s . c  om
    while ((nextEntry = ais.getNextEntry()) != null) {
        aos.putArchiveEntry(nextEntry);
        IOUtils.copy(ais, aos);
        aos.closeArchiveEntry();
    }
    ais.close();
    cis.close();
    bis.close();

    TarArchiveEntry entry = new TarArchiveEntry(
            Paths.get(".openshift", CONFIG_DIRECTORY, "/standalone.xml").toFile());
    byte[] xmlData = SAMPLE_STANDALONE_DATA.getBytes();
    entry.setSize(xmlData.length);
    aos.putArchiveEntry(entry);
    IOUtils.write(xmlData, aos);

    for (int i = 0; i < filepaths.length; i++) {
        String filepath = filepaths[i];
        TarArchiveEntry emptyEntry = new TarArchiveEntry(Paths.get(filepath).toFile());
        byte[] emptyData = "".getBytes();
        emptyEntry.setSize(emptyData.length);
        aos.putArchiveEntry(emptyEntry);
        IOUtils.write(emptyData, aos);
        aos.closeArchiveEntry();
    }

    aos.finish();
    cos.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}

From source file:net.sf.util.zip.analyzer.ArchieveAnalyzer.java

@Override
public List<String> analyze(File f) throws Exception {
    //System.out.println("Inside ArchiveAnalyzer.analyze()");
    List<String> entries = new ArrayList<String>();
    FileInputStream fin = new FileInputStream(f);
    ArchiveInputStream newArchiveInputStream = getArchiveInputStream(f.getName(), fin);
    listZipContent(newArchiveInputStream, f.getAbsolutePath(), entries);
    newArchiveInputStream.close();
    fin.close();/*from w  ww  . j  av a2 s.  com*/
    return entries;
}

From source file:com.openkm.misc.ZipTest.java

public void testApache() throws IOException, ArchiveException {
    log.debug("testApache()");
    File zip = File.createTempFile("apache_", ".zip");

    // Create zip
    FileOutputStream fos = new FileOutputStream(zip);
    ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream("zip", fos);
    aos.putArchiveEntry(new ZipArchiveEntry("coeta"));
    aos.closeArchiveEntry();//  www. j  a v a 2s  .  com
    aos.close();

    // Read zip
    FileInputStream fis = new FileInputStream(zip);
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", fis);
    ZipArchiveEntry zae = (ZipArchiveEntry) ais.getNextEntry();
    assertEquals(zae.getName(), "coeta");
    ais.close();
}