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.apache.archiva.remotedownload.DownloadArtifactsTest.java

@Test
public void downloadWithRemoteRedirect() throws Exception {
    RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository("central");
    remoteRepository.setUrl("http://localhost:" + redirectPort);
    getRemoteRepositoriesService().updateRemoteRepository(remoteRepository);

    RoleManagementService roleManagementService = getRoleManagementService(authorizationHeader);

    if (!roleManagementService.templatedRoleExists(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
            "internal")) {
        roleManagementService.createTemplatedRole(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
                "internal");
    }/*from  www .  ja v a2  s . c om*/

    getUserService(authorizationHeader).createGuestUser();
    roleManagementService.assignRole(ArchivaRoleConstants.TEMPLATE_GUEST, "guest");

    roleManagementService.assignTemplatedRole(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal",
            "guest");

    getUserService(authorizationHeader).removeFromCache("guest");

    File file = new File("target/junit-4.9.jar");
    if (file.exists()) {
        file.delete();
    }

    HttpWagon httpWagon = new HttpWagon();
    httpWagon.connect(new Repository("foo", "http://localhost:" + port));

    httpWagon.get("/repository/internal/junit/junit/4.9/junit-4.9.jar", file);

    ZipFile zipFile = new ZipFile(file);
    List<String> entries = getZipEntriesNames(zipFile);
    ZipEntry zipEntry = zipFile.getEntry("org/junit/runners/JUnit4.class");
    assertNotNull("cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: "
            + FileUtils.readFileToString(file), zipEntry);
    zipFile.close();
    file.deleteOnExit();
}

From source file:org.jboss.pvt.harness.validators.JBossSignatureCheckValidator.java

@Override
public boolean validate(PVTConfiguration pvtConfiguration) throws PVTException {
    File eapDir = pvtConfiguration.getDistributionDirectory();

    Collection<File> jarFiles = DirUtils.listFilesRecursively(eapDir, new FileFilter() {
        public boolean accept(File pathname) {
            return pathname.isFile() && pathname.getName().endsWith(".jar");
        }/*from  w w  w  . ja  v  a2 s . c o m*/
    });

    logger.info("Jars: " + Arrays.toString(jarFiles.toArray()));

    boolean signed = mustSigned(pvtConfiguration);
    try {
        for (File jarFile : jarFiles) {
            if (jarFile.isFile()) {
                if (shouldExclude(jarFile, pvtConfiguration)) {
                    logger.warn("Skip Jar, " + jarFile);
                    continue;
                }
                ZipFile zipFile = new ZipFile(jarFile);
                StringWriter sw = new StringWriter();
                IOUtils.copy(
                        new InputStreamReader(zipFile.getInputStream(zipFile.getEntry("META-INF/MANIFEST.MF"))),
                        sw);
                signed = sw.getBuffer().toString().contains("Digest:");

                if (signed != mustSigned(pvtConfiguration)) {
                    logger.debug("Found " + (mustSigned(pvtConfiguration) ? "unsigned" : "signed") + " jar: "
                            + jarFile);
                    break;
                }
            }
        }
    } catch (IOException e) {
        throw new PVTSystemException(e);
    }

    return (signed == mustSigned(pvtConfiguration));
}

From source file:org.roda.core.plugins.plugins.characterization.ODFSignatureUtils.java

private static ByteArrayOutputStream addSignatureToStream(ZipFile zipFile, Element rootManifest,
        Element rootSignatures) throws IOException, TransformerFactoryConfigurationError, TransformerException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream())) {

        Enumeration<?> enumeration;
        for (enumeration = zipFile.entries(); enumeration.hasMoreElements();) {
            ZipEntry zeOut = (ZipEntry) enumeration.nextElement();
            String fileName = zeOut.getName();

            if (!fileName.equals(META_INF_DOCUMENTSIGNATURES_XML)
                    && !"META-INF/manifest.xml".equals(fileName)) {
                zos.putNextEntry(zeOut);
                try (InputStream is = zipFile.getInputStream(zipFile.getEntry(fileName))) {
                    zos.write(IOUtils.toByteArray(is));
                    zos.closeEntry();//from ww w  . java 2  s  .c  o m
                }
            }
        }

        ZipEntry zeDocumentSignatures = new ZipEntry(META_INF_DOCUMENTSIGNATURES_XML);
        zos.putNextEntry(zeDocumentSignatures);
        try (ByteArrayOutputStream baosXML = new ByteArrayOutputStream()) {
            writeXML(baosXML, rootSignatures, false);
            zos.write(baosXML.toByteArray());
            zos.closeEntry();
        }

        ZipEntry zeManifest = new ZipEntry("META-INF/manifest.xml");
        zos.putNextEntry(zeManifest);
        try (ByteArrayOutputStream baosManifest = new ByteArrayOutputStream()) {
            writeXML(baosManifest, rootManifest, false);
            zos.write(baosManifest.toByteArray());
            zos.closeEntry();
        }

        zipFile.close();
    }

    return baos;
}

From source file:nl.ordina.bag.etl.loader.ExtractLoader.java

protected BAGExtractLevering processFile(ZipFile zipFile, final String filename)
        throws JAXBException, IOException {
    ZipEntry entry = zipFile.getEntry(filename);
    return entry == null ? null
            : XMLMessageBuilder.getInstance(BAGExtractLevering.class).handle(zipFile.getInputStream(entry));
}

From source file:org.pentaho.osgi.platform.webjars.WebjarsURLConnectionTest.java

private void verifyNoRequireJson(ZipFile zipInputStream) throws IOException, ParseException {
    ZipEntry entry = zipInputStream.getEntry("META-INF/js/require.json");
    assertNull(entry);//from  ww w .  j a v a  2  s.  c om
}

From source file:org.commonjava.maven.galley.filearc.internal.ZipDownload.java

@Override
public DownloadJob call() {
    final File src = getZipFile();

    if (src.isDirectory()) {
        return this;
    }/*from  ww  w .j  a v a  2  s .com*/

    ZipFile zf = null;
    InputStream in = null;
    OutputStream out = null;
    try {
        zf = isJarOperation() ? new JarFile(src) : new ZipFile(src);

        final ZipEntry entry = zf.getEntry(getFullPath());
        if (entry != null) {
            if (entry.isDirectory()) {
                error = new TransferException("Cannot read stream. Source is a directory: %s!%s",
                        getLocation().getUri(), getFullPath());
            } else {
                in = zf.getInputStream(entry);
                out = getTransfer().openOutputStream(TransferOperation.DOWNLOAD, true, eventMetadata);

                copy(in, out);

                return this;
            }
        } else {
            error = new TransferException("Cannot find entry: %s in: %s", getFullPath(),
                    getLocation().getUri());
        }
    } catch (final IOException e) {
        error = new TransferException("Failed to copy from: %s to: %s. Reason: %s", e, src, getTransfer(),
                e.getMessage());
    } finally {
        closeQuietly(in);
        if (zf != null) {
            //noinspection EmptyCatchBlock
            try {
                zf.close();
            } catch (final IOException e) {
            }
        }

        closeQuietly(out);
    }

    if (error != null) {
        logger.error("Failed to download: {}. Reason: {}", this, error);
    }

    return null;
}

From source file:org.openoffice.plugin.core.model.UnoPackageTest.java

private String getManifestContent() throws ZipException, IOException {
    ZipFile zip = new ZipFile(tmpFile);
    try {/*w  w w .  ja  va  2s  . com*/
        ZipEntry entry = zip.getEntry("META-INF/manifest.xml");
        InputStream istream = zip.getInputStream(entry);
        return IOUtils.toString(istream);
    } finally {
        zip.close();
    }
}

From source file:com.streamsets.datacollector.bundles.TestSupportBundleManager.java

@Test
public void testSimpleSupportBundleCreation() throws Exception {
    ZipFile bundle = zipFile(ImmutableList.of(SimpleGenerator.class.getSimpleName()), BundleType.SUPPORT);
    ZipEntry entry;//from  www. ja va  2  s.co m

    // Check we have expected files
    entry = bundle.getEntry("metadata.properties");
    assertNotNull(entry);

    entry = bundle.getEntry("generators.properties");
    assertNotNull(entry);

    entry = bundle.getEntry("failed_generators.properties");
    assertNotNull(entry);

    entry = bundle.getEntry("com.streamsets.datacollector.bundles.content.SimpleGenerator/file.txt");
    assertNotNull(entry);
}

From source file:org.pentaho.osgi.platform.webjars.WebjarsURLConnectionTest.java

private void verifyBlueprint(ZipFile zipInputStream, String expectedPath) throws IOException {
    ZipEntry entry = zipInputStream.getEntry("OSGI-INF/blueprint/blueprint.xml");
    assertNotNull(entry);/*  w w w. j  a  v a 2  s . com*/

    String bpFile = IOUtils.toString(zipInputStream.getInputStream(entry), "UTF-8");

    Pattern distPattern = Pattern.compile("<bean id=\"resourceMappingDist\".*>.*"
            + "<property name=\"alias\" value=\"\\/" + expectedPath + "\"\\/>.*"
            + "<property name=\"path\" value=\"\\/META-INF\\/resources\\/dist-gen\"\\/>.*" + "<\\/bean>",
            Pattern.DOTALL);

    Matcher matcher = distPattern.matcher(bpFile);

    assertTrue("blueprint.xml does not include path for minified " + expectedPath, matcher.find());

    distPattern = Pattern.compile("<bean id=\"resourceMappingSrc\".*>.*"
            + "<property name=\"alias\" value=\"\\/webjar-src\\/" + expectedPath + "\"\\/>.*"
            + "<property name=\"path\" value=\"\\/META-INF\\/resources\\/webjars\\/" + expectedPath + "\"\\/>.*"
            + "<\\/bean>", Pattern.DOTALL);

    matcher = distPattern.matcher(bpFile);

    assertTrue("blueprint.xml does not include path for " + expectedPath + " sources", matcher.find());
}

From source file:com.koushikdutta.superuser.MainActivity.java

File extractSu() throws IOException, InterruptedException {
    ZipFile zf = new ZipFile(getPackageCodePath());
    ZipEntry su = zf.getEntry("assets/" + getArch() + "/su");
    InputStream zin = zf.getInputStream(su);
    File ret = getFileStreamPath("su");
    FileOutputStream fout = new FileOutputStream(ret);
    StreamUtility.copyStream(zin, fout);
    zin.close();//from  w  ww .j  av  a2 s.  c o  m
    zf.close();
    fout.close();
    return ret;
}