Example usage for org.apache.commons.compress.archivers.zip ZipFile getEntry

List of usage examples for org.apache.commons.compress.archivers.zip ZipFile getEntry

Introduction

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

Prototype

public ZipArchiveEntry getEntry(String name) 

Source Link

Document

Returns a named entry - or null if no entry by that name exists.

Usage

From source file:org.apache.tika.parser.pkg.ZipContainerDetector.java

/**
 * OpenDocument files, along with EPub files and ASiC ones, have a 
 *  mimetype entry in the root of their Zip file. This entry contains
 *  the mimetype of the overall file, stored as a single string.  
 *///from  w  w  w  .  j  a va  2  s  . c om
private static MediaType detectOpenDocument(ZipFile zip) {
    try {
        ZipArchiveEntry mimetype = zip.getEntry("mimetype");
        if (mimetype != null) {
            try (InputStream stream = zip.getInputStream(mimetype)) {
                return MediaType.parse(IOUtils.toString(stream, UTF_8));
            }
        } else {
            return null;
        }
    } catch (IOException e) {
        return null;
    }
}

From source file:org.apache.tika.parser.pkg.ZipContainerDetector.java

private static MediaType detectIWork13(ZipFile zip) {
    if (zip.getEntry(IWork13PackageParser.IWORK13_COMMON_ENTRY) != null) {
        return IWork13PackageParser.IWork13DocumentType.detect(zip);
    }/*from w  w  w . j  a v  a2 s  .com*/
    return null;

}

From source file:org.apache.tika.server.CXFTestBase.java

protected String readArchiveText(InputStream inputStream) throws IOException {
    Path tempFile = writeTemporaryArchiveFile(inputStream, "zip");
    ZipFile zip = new ZipFile(tempFile.toFile());
    zip.getEntry(UnpackerResource.TEXT_FILENAME);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    IOUtils.copy(zip.getInputStream(zip.getEntry(UnpackerResource.TEXT_FILENAME)), bos);

    zip.close();//from   ww w.  ja v  a2s.com
    Files.delete(tempFile);
    return bos.toString(UTF_8.name());
}

From source file:org.apache.wookie.w3c.util.WidgetPackageUtils.java

/**
 * Returns the set of valid file paths for a given resource. All valid paths are returned, starting
 * with localized versions for supported locales before the root version (if present).
 * @param path/*w  ww.j  a v a  2s. c  om*/
 * @param locales
 * @param zip
 * @return
 * @throws Exception
 */
public static String[] locateFilePaths(String path, String[] locales, ZipFile zip) throws Exception {
    ArrayList<String> paths = new ArrayList<String>();
    if (path.startsWith("/"))
        path = path.substring(1, path.length());
    String[] pathComponents = path.split("/");
    if ("locales".equalsIgnoreCase(pathComponents[0])) {
        if (pathComponents.length < 2)
            return null;
        if (!LocalizationUtils.isValidLanguageTag(pathComponents[1]))
            return null;
    }
    // Look in localized folders first
    for (String locale : locales) {
        String localePath = "locales/" + locale.trim() + "/" + path;
        if (zip.getEntry(localePath) != null) {
            if (zip.getEntry(localePath).isDirectory())
                throw new Exception();
            paths.add(localePath);
        }
    }
    // Look in root folder
    if (zip.getEntry(path) != null && !zip.getEntry(path).isDirectory())
        paths.add(path);
    return (String[]) paths.toArray(new String[paths.size()]);
}

From source file:org.apache.wookie.w3c.util.WidgetPackageUtils.java

/**
 * Checks for the existence of the Manifest.
 * TODO not sure if this properly handles case-sensitive entries?
 * @param zipfile/* w w w .j a  v  a 2s. c o m*/
 * @return true if the zip file has a manifest
 */
public static boolean hasManifest(ZipFile zipfile) {
    return zipfile.getEntry(IW3CXMLConfiguration.MANIFEST_FILE) != null;
}

From source file:org.apache.wookie.w3c.util.WidgetPackageUtils.java

/**
 * Retrieves the Manifest entry as a String
 * @param zipFile the zip file from which to extract the manifest
 * @return a String representing the manifest contents
 * @throws IOException/*ww w. ja v a  2 s. c om*/
 */
public static String extractManifest(ZipFile zipFile) throws IOException {
    ZipArchiveEntry entry = zipFile.getEntry(IW3CXMLConfiguration.MANIFEST_FILE);
    return IOUtils.toString(zipFile.getInputStream(entry), "UTF-8");
}

From source file:org.codehaus.plexus.archiver.jar.IndexTest.java

public void testCreateArchiveWithIndexedJars() throws Exception {
    /* create a dummy jar */
    JarArchiver archiver1 = (JarArchiver) lookup(Archiver.ROLE, "jar");
    archiver1.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt");
    archiver1.setDestFile(getTestFile("target/output/archive1.jar"));
    archiver1.createArchive();//from   www.j a v  a2 s  . co m

    /* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first jar.
     */
    Manifest m = new Manifest();
    Manifest.Attribute classpathAttr = new Manifest.Attribute("Class-Path", "archive1.jar");
    m.addConfiguredAttribute(classpathAttr);

    JarArchiver archiver2 = (JarArchiver) lookup(Archiver.ROLE, "jar");
    archiver2.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt");
    archiver2.setIndex(true);
    archiver2.addConfiguredIndexJars(archiver1.getDestFile());
    archiver2.setDestFile(getTestFile("target/output/archive2.jar"));
    archiver2.addConfiguredManifest(m);
    archiver2.createArchive();

    // read the index file back and check it looks like it ought to
    org.apache.commons.compress.archivers.zip.ZipFile zf = new org.apache.commons.compress.archivers.zip.ZipFile(
            archiver2.getDestFile());
    ZipArchiveEntry indexEntry = zf.getEntry("META-INF/INDEX.LIST");
    assertNotNull(indexEntry);
    InputStream bis = bufferedInputStream(zf.getInputStream(indexEntry));

    byte buf[] = new byte[1024];
    int i = bis.read(buf);
    String res = new String(buf, 0, i);
    assertEquals("JarIndex-Version: 1.0\n\narchive2.jar\ntwo.txt\n\narchive1.jar\none.txt\n\n",
            res.replaceAll("\r\n", "\n"));
}

From source file:org.codehaus.plexus.archiver.jar.IndexTest.java

/**
 * this is pretty much a duplicate of testCreateArchiveWithIndexedJars(), but adds some extra
 * tests for files in META-INF//from www.j a  va  2  s .c o m
 */
public void testCreateArchiveWithIndexedJarsAndMetaInf() throws Exception {
    /* create a dummy jar */
    JarArchiver archiver1 = (JarArchiver) lookup(Archiver.ROLE, "jar");
    archiver1.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt");

    // add a file in the META-INF directory, as this previously didn't make it into the index
    archiver1.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "META-INF/foo");
    archiver1.setDestFile(getTestFile("target/output/archive1.jar"));
    archiver1.createArchive();

    /* create another dummy jar, with an index but nothing else in META-INF. Also checks non-leaf files. */
    JarArchiver archiver3 = (JarArchiver) lookup(Archiver.ROLE, "jar");
    archiver3.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "org/apache/maven/one.txt");
    archiver3.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "META-INF/INDEX.LIST");
    archiver3.setDestFile(getTestFile("target/output/archive3.jar"));
    archiver3.createArchive();

    /* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first two jars.
     */
    Manifest m = new Manifest();
    Manifest.Attribute classpathAttr = new Manifest.Attribute("Class-Path", "archive1.jar archive3.jar");
    m.addConfiguredAttribute(classpathAttr);

    JarArchiver archiver2 = (JarArchiver) lookup(Archiver.ROLE, "jar");
    archiver2.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt");
    archiver2.setIndex(true);
    archiver2.addConfiguredIndexJars(archiver1.getDestFile());
    archiver2.addConfiguredIndexJars(archiver3.getDestFile());
    archiver2.setDestFile(getTestFile("target/output/archive2.jar"));
    archiver2.addConfiguredManifest(m);
    archiver2.createArchive();

    // read the index file back and check it looks like it ought to
    org.apache.commons.compress.archivers.zip.ZipFile zf = new org.apache.commons.compress.archivers.zip.ZipFile(
            archiver2.getDestFile());
    ZipArchiveEntry indexEntry = zf.getEntry("META-INF/INDEX.LIST");
    assertNotNull(indexEntry);
    InputStream bis = bufferedInputStream(zf.getInputStream(indexEntry));

    byte buf[] = new byte[1024];
    int i = bis.read(buf);
    String res = new String(buf, 0, i);
    //System.out.println(res);

    assertEquals("JarIndex-Version: 1.0\n\n" + "archive2.jar\ntwo.txt\n\n"
            + "archive1.jar\nMETA-INF\none.txt\n\n" + "archive3.jar\norg\norg/apache\norg/apache/maven\n\n",
            res.replaceAll("\r\n", "\n"));
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testImplicitPermissions() throws IOException {
    File zipFile = getTestFile("target/output/zip-with-implicit-dirmode.zip");

    ZipArchiver archiver = getZipArchiver(zipFile);

    archiver.setDefaultDirectoryMode(0777);
    archiver.setDirectoryMode(0641);/*from   ww w. j  a  v a2  s  . c o  m*/
    archiver.setFileMode(0222);
    archiver.addFile(new File("pom.xml"), "fizz/buzz/pom.xml");
    archiver.setDefaultDirectoryMode(0530);
    archiver.setDirectoryMode(-1); // Not forced mode
    archiver.setFileMode(0111);
    archiver.addFile(new File("pom.xml"), "fazz/bazz/pam.xml");
    archiver.createArchive();

    assertTrue(zipFile.exists());
    ZipFile zf = new ZipFile(zipFile);
    ZipArchiveEntry fizz = zf.getEntry("fizz/");
    assertEquals(040641, fizz.getUnixMode());
    ZipArchiveEntry pom = zf.getEntry("fizz/buzz/pom.xml");
    assertEquals(0100222, pom.getUnixMode());

    ZipArchiveEntry fazz = zf.getEntry("fazz/");
    assertEquals(040530, fazz.getUnixMode());
    ZipArchiveEntry pam = zf.getEntry("fazz/bazz/pam.xml");
    assertEquals(0100111, pam.getUnixMode());
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testOverddidenPermissions() throws IOException {
    File zipFile = getTestFile("target/output/zip-with-overriden-modes.zip");

    ZipArchiver archiver = getZipArchiver(zipFile);
    archiver.setDefaultDirectoryMode(0777);
    archiver.setDirectoryMode(0641);/*from  ww w .  j  a v  a  2  s  .c o  m*/
    archiver.setFileMode(0777);
    archiver.addDirectory(new File("src/test/resources/symlinks/src"));
    archiver.createArchive();

    assertTrue(zipFile.exists());
    ZipFile zf = new ZipFile(zipFile);
    ZipArchiveEntry fizz = zf.getEntry("symDir");
    assertTrue(fizz.isUnixSymlink());
    ZipArchiveEntry symR = zf.getEntry("symR");
    assertTrue(symR.isUnixSymlink());
}