Example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream canReadEntryData

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream canReadEntryData

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream canReadEntryData.

Prototype

public boolean canReadEntryData(ArchiveEntry ae) 

Source Link

Document

Whether this class is able to read the given entry.

Usage

From source file:de.flapdoodle.embedmongo.extract.ZipExtractor.java

@Override
public void extract(RuntimeConfig runtime, File source, File destination, Pattern file) throws IOException {
    IProgressListener progressListener = runtime.getProgressListener();
    String progressLabel = "Extract " + source;
    progressListener.start(progressLabel);

    FileInputStream fin = new FileInputStream(source);
    BufferedInputStream in = new BufferedInputStream(fin);

    ZipArchiveInputStream zipIn = new ZipArchiveInputStream(in);
    try {/*from w w  w  .  j  a va  2  s.co  m*/
        ZipArchiveEntry entry;
        while ((entry = zipIn.getNextZipEntry()) != null) {
            if (file.matcher(entry.getName()).matches()) {
                //               System.out.println("File: " + entry.getName());
                if (zipIn.canReadEntryData(entry)) {
                    //                  System.out.println("Can Read: " + entry.getName());
                    long size = entry.getSize();
                    Files.write(zipIn, size, destination);
                    destination.setExecutable(true);
                    //                  System.out.println("DONE");
                    progressListener.done(progressLabel);
                }
                break;

            } else {
                //               System.out.println("SKIP File: " + entry.getName());
            }
        }

    } finally {
        zipIn.close();
    }

}

From source file:org.apache.tika.parser.utils.ZipSalvager.java

/**
 * This streams the broken zip and rebuilds a new zip that
 * is at least a valid zip file.  The contents of the final stream
 * may be truncated, but the result should be a valid zip file.
 * <p>/* w  ww.j  a  v  a  2s .  co  m*/
 * This does nothing fancy to fix the underlying broken zip.
 *
 * @param brokenZip
 * @param salvagedZip
 */
public static void salvageCopy(InputStream brokenZip, File salvagedZip) {
    try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(salvagedZip)) {
        ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(brokenZip);
        ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry();
        while (zae != null) {
            try {
                if (!zae.isDirectory() && zipArchiveInputStream.canReadEntryData(zae)) {
                    //create a new ZAE and copy over only the name so that
                    //if there is bad info (e.g. CRC) in brokenZip's zae, that
                    //won't be propagated or cause an exception
                    outputStream.putArchiveEntry(new ZipArchiveEntry(zae.getName()));
                    //this will copy an incomplete stream...so there
                    //could be truncation of the xml/contents, but the zip file
                    //should be intact.
                    boolean successfullyCopied = false;
                    try {
                        IOUtils.copy(zipArchiveInputStream, outputStream);
                        successfullyCopied = true;
                    } catch (IOException e) {
                        //this can hit a "truncated ZipFile" IOException
                    }
                    outputStream.flush();
                    outputStream.closeArchiveEntry();
                    if (!successfullyCopied) {
                        break;
                    }
                }
                zae = zipArchiveInputStream.getNextZipEntry();
            } catch (ZipException | EOFException e) {
                break;
            }

        }
        outputStream.flush();
        outputStream.finish();

    } catch (IOException e) {
        LOG.warn("problem fixing zip", e);
    }
}

From source file:org.xwiki.xar.XarPackage.java

/**
 * Find and add the entries located in the passed XAR file.
 * //from  ww w  .  j a v a 2 s  . com
 * @param xarStream an input stream to a XAR file
 * @throws IOException when failing to read the file
 * @throws XarException when failing to parse the XAR package
 */
public void read(InputStream xarStream) throws IOException, XarException {
    ZipArchiveInputStream zis = new ZipArchiveInputStream(xarStream, "UTF-8", false);

    try {
        for (ZipArchiveEntry entry = zis.getNextZipEntry(); entry != null; entry = zis.getNextZipEntry()) {
            if (!entry.isDirectory() && zis.canReadEntryData(entry)) {
                readEntry(zis, entry);
            }
        }
    } finally {
        zis.close();
    }
}