Example usage for java.util.zip ZipFile getEntry

List of usage examples for java.util.zip ZipFile getEntry

Introduction

In this page you can find the example usage for java.util.zip ZipFile getEntry.

Prototype

public ZipEntry getEntry(String name) 

Source Link

Document

Returns the zip file entry for the specified name, or null if not found.

Usage

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Unpacks a single entry from a ZIP file.
 *
 * @param zf//from www .j av a2s  .  com
 *          ZIP file.
 * @param name
 *          entry name.
 * @return contents of the entry or <code>null</code> if it was not found.
 */
private static byte[] doUnpackEntry(ZipFile zf, String name) throws IOException {
    ZipEntry ze = zf.getEntry(name);
    if (ze == null) {
        return null; // entry not found
    }

    InputStream is = zf.getInputStream(ze);
    try {
        return IOUtils.toByteArray(is);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Checks if the ZIP file contains the given entry.
 *
 * @param zip/*from   www.j  a  v a 2  s  .c o m*/
 *          ZIP file.
 * @param name
 *          entry name.
 * @return <code>true</code> if the ZIP file contains the given entry.
 */
public static boolean containsEntry(File zip, String name) {
    ZipFile zf = null;
    try {
        zf = new ZipFile(zip);
        return zf.getEntry(name) != null;
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    } finally {
        closeQuietly(zf);
    }
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Reads the given ZIP file and executes the given action for a single entry.
 *
 * @param zip/*  w ww . j  a v a  2 s .c o m*/
 *          input ZIP file.
 * @param name
 *          entry name.
 * @param action
 *          action to be called for this entry.
 * @return <code>true</code> if the entry was found, <code>false</code> if the
 *         entry was not found.
 *
 * @see ZipEntryCallback
 */
public static boolean handle(File zip, String name, ZipEntryCallback action) {
    ZipFile zf = null;
    try {
        zf = new ZipFile(zip);

        ZipEntry ze = zf.getEntry(name);
        if (ze == null) {
            return false; // entry not found
        }

        InputStream in = new BufferedInputStream(zf.getInputStream(ze));
        try {
            action.process(in, ze);
        } finally {
            IOUtils.closeQuietly(in);
        }
        return true;
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    } finally {
        closeQuietly(zf);
    }
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Checks if the ZIP file contains any of the given entries.
 *
 * @param zip/*from   w  ww .java  2 s .  co  m*/
 *          ZIP file.
 * @param names
 *          entry names.
 * @return <code>true</code> if the ZIP file contains any of the given
 *         entries.
 */
public static boolean containsAnyEntry(File zip, String[] names) {
    ZipFile zf = null;
    try {
        zf = new ZipFile(zip);
        for (int i = 0; i < names.length; i++) {
            if (zf.getEntry(names[i]) != null) {
                return true;
            }
        }
        return false;
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    } finally {
        closeQuietly(zf);
    }
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Scans the given ZIP file and executes the given action for each given entry.
 * <p>/*from  www  .j a  va2 s .c o m*/
 * Only the meta-data without the actual data is read. If you want to stop the loop then throw a ZipBreakException.
 *
 * @param zip
 *          input ZIP file.
 * @param entryNames
 *          names of entries to iterate
 * @param action
 *          action to be called for each entry.
 *
 * @see ZipInfoCallback
 * @see #iterate(File, String[], ZipEntryCallback)
 */
public static void iterate(File zip, String[] entryNames, ZipInfoCallback action) {
    ZipFile zf = null;
    try {
        zf = new ZipFile(zip);

        for (int i = 0; i < entryNames.length; i++) {
            ZipEntry e = zf.getEntry(entryNames[i]);
            if (e == null) {
                continue;
            }
            try {
                action.process(e);
            } catch (IOException ze) {
                throw new ZipException("Failed to process zip entry '" + e.getName() + " with action " + action,
                        ze);
            } catch (ZipBreakException ex) {
                break;
            }
        }
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    } finally {
        closeQuietly(zf);
    }
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Reads the given ZIP file and executes the given action for each given entry.
 * <p>//www  .  j a  v  a2s.  c o  m
 * For each given entry the corresponding input stream is also passed to the action. If you want to stop the loop then throw a ZipBreakException.
 *
 * @param zip
 *          input ZIP file.
 * @param entryNames
 *          names of entries to iterate
 * @param action
 *          action to be called for each entry.
 *
 * @see ZipEntryCallback
 * @see #iterate(File, String[], ZipInfoCallback)
 */
public static void iterate(File zip, String[] entryNames, ZipEntryCallback action) {
    ZipFile zf = null;
    try {
        zf = new ZipFile(zip);

        for (int i = 0; i < entryNames.length; i++) {
            ZipEntry e = zf.getEntry(entryNames[i]);
            if (e == null) {
                continue;
            }
            InputStream is = zf.getInputStream(e);
            try {
                action.process(is, e);
            } catch (IOException ze) {
                throw new ZipException("Failed to process zip entry '" + e.getName() + " with action " + action,
                        ze);
            } catch (ZipBreakException ex) {
                break;
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    } catch (IOException e) {
        throw ZipExceptionUtil.rethrow(e);
    } finally {
        closeQuietly(zf);
    }
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Compares two ZIP entries (byte-by-byte). .
 *
 * @param zf1/*from  ww  w .ja  va2s  .c om*/
 *          first ZIP file.
 * @param zf2
 *          second ZIP file.
 * @param path1
 *          name of the first entry.
 * @param path2
 *          name of the second entry.
 * @return <code>true</code> if the contents of the entries were same.
 */
private static boolean doEntryEquals(ZipFile zf1, ZipFile zf2, String path1, String path2) throws IOException {
    InputStream is1 = null;
    InputStream is2 = null;
    try {
        ZipEntry e1 = zf1.getEntry(path1);
        ZipEntry e2 = zf2.getEntry(path2);

        if (e1 == null && e2 == null) {
            return true;
        }

        if (e1 == null || e2 == null) {
            return false;
        }

        is1 = zf1.getInputStream(e1);
        is2 = zf2.getInputStream(e2);
        if (is1 == null && is2 == null) {
            return true;
        }
        if (is1 == null || is2 == null) {
            return false;
        }

        return IOUtils.contentEquals(is1, is2);
    } finally {
        IOUtils.closeQuietly(is1);
        IOUtils.closeQuietly(is2);
    }
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Unpacks a single file from a ZIP archive to a file.
 *
 * @param zf//w  w  w .j  a  va  2  s  .c om
 *          ZIP file.
 * @param name
 *          entry name.
 * @param file
 *          target file to be created or overwritten.
 * @return <code>true</code> if the entry was found and unpacked,
 *         <code>false</code> if the entry was not found.
 */
private static boolean doUnpackEntry(ZipFile zf, String name, File file) throws IOException {
    if (log.isTraceEnabled()) {
        log.trace("Extracting '" + zf.getName() + "' entry '" + name + "' into '" + file + "'.");
    }

    ZipEntry ze = zf.getEntry(name);
    if (ze == null) {
        return false; // entry not found
    }

    InputStream in = new BufferedInputStream(zf.getInputStream(ze));
    try {
        FileUtil.copy(in, file);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return true;
}

From source file:org.digidoc4j.impl.BDocContainerTest.java

@Test
public void setsSignatureId() throws Exception {
    BDocContainer container = new BDocContainer();
    container.addDataFile("testFiles/test.txt", "text/plain");

    SignatureParameters signatureParameters = new SignatureParameters();
    signatureParameters.setSignatureId("SIGNATURE-1");
    container.setSignatureParameters(signatureParameters);
    container.sign(PKCS12_SIGNER);// w w  w.j a v  a 2s. com

    signatureParameters.setSignatureId("SIGNATURE-2");
    container.setSignatureParameters(signatureParameters);
    container.sign(PKCS12_SIGNER);
    container.save("setsSignatureId.bdoc");

    container = new BDocContainer("setsSignatureId.bdoc");
    assertEquals("SIGNATURE-1", container.getSignature(0).getId());
    assertEquals("SIGNATURE-2", container.getSignature(1).getId());

    ZipFile zip = new ZipFile("setsSignatureId.bdoc");
    assertNotNull(zip.getEntry("META-INF/signatures0.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures1.xml"));
}

From source file:org.digidoc4j.impl.BDocContainerTest.java

@Test
public void setsDefaultSignatureId() throws Exception {
    BDocContainer container = new BDocContainer();
    container.addDataFile("testFiles/test.txt", "text/plain");
    container.sign(PKCS12_SIGNER);//from w w  w. j  a v a  2  s  .co m
    container.sign(PKCS12_SIGNER);
    container.save("testSetsDefaultSignatureId.bdoc");

    container = new BDocContainer("testSetsDefaultSignatureId.bdoc");
    assertEquals("S0", container.getSignature(0).getId());
    assertEquals("S1", container.getSignature(1).getId());

    ZipFile zip = new ZipFile("testSetsDefaultSignatureId.bdoc");
    assertNotNull(zip.getEntry("META-INF/signatures0.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures1.xml"));
}