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.sonar.fortify.fvdl.FortifyReportFile.java

private InputStream getInputStreamFromFprFile(File file) throws IOException {
    final ZipFile fprFile = new ZipFile(file);
    try {//from  ww w. j  a  v a2 s  . c  om
        final InputStream reportStream = fprFile
                .getInputStream(fprFile.getEntry(FortifyConstants.AUDIT_FVDL_FILE));
        return new InputStream() {
            @Override
            public int read() throws IOException {
                return reportStream.read();
            }

            @Override
            public void close() throws IOException {
                try {
                    reportStream.close();
                } finally {
                    fprFile.close();
                }
            }
        };
    } catch (IOException e) {
        fprFile.close();
        throw e;
    }
}

From source file:m3.classe.M3ClassLoader.java

private DataInputStream getStreamFromJar(String name, File path) throws MAKException {
    if (!this.m_zipProblems.contains(path)) {
        try {/*from www  .j  av  a  2s .  co  m*/
            DataInputStream dis = null;
            ZipFile file = new ZipFile(path);
            ZipEntry entry = file.getEntry(name.replace('.', '/') + '.' + "class");
            if (entry == null) {
                entry = file.getEntry(name.replace('.', '/') + '.' + "properties");
            }
            if (entry != null)
                ;
            return new DataInputStream(file.getInputStream(entry));
        } catch (IOException e) {
            Ressource.logger.warn("Failed to get " + name + " from jar " + path
                    + " - MAK will try again using another method", e);
            this.m_zipProblems.add(path);
        }
    }
    return getStreamFromJar2(name, path);
}

From source file:org.gradle.cache.internal.WrapperDistributionCleanupAction.java

private GradleVersion readGradleVersionFromBuildReceipt(ZipFile zipFile) throws Exception {
    ZipEntry zipEntry = zipFile.getEntry(BUILD_RECEIPT_ZIP_ENTRY_PATH);
    if (zipEntry == null) {
        return null;
    }//from w ww  .j a va 2  s . co  m
    InputStream in = zipFile.getInputStream(zipEntry);
    try {
        Properties properties = new Properties();
        properties.load(in);
        String versionString = properties.getProperty(GradleVersion.VERSION_NUMBER_PROPERTY);
        return GradleVersion.version(versionString);
    } finally {
        in.close();
    }
}

From source file:org.wte4j.impl.WordTemplateBuilderTest.java

@Test
public void createBasicTemplate() throws Exception {

    Map<String, Class<?>> elements = new HashMap<String, Class<?>>();
    elements.put("string", String.class);

    WteModelService service = mock(WteModelService.class);
    Map<String, String> properties = anyMapOf(String.class, String.class);
    Class<?> inputType = any(Class.class);
    when(service.listModelElements(inputType, properties)).thenReturn(elements);

    FormatterFactory factory = mock(FormatterFactory.class);
    WordTemplateBuilder<String> builder = new WordTemplateBuilder<String>(factory, service, String.class);

    byte[] templateContent = builder.createBasicTemplate();
    File templateFile = File.createTempFile("generated", "docx");
    try {/*from   www. j a  va2 s.  com*/
        FileUtils.writeByteArrayToFile(templateFile, templateContent);
        ZipFile zipFile = new ZipFile(templateFile);
        ZipEntry entry = zipFile.getEntry("word/document.xml");
        InputStream documentIn = zipFile.getInputStream(entry);
        String doucmentXml = IOUtils.toString(documentIn);
        String expected = "<w:sdt>" + "<w:sdtPr>"//
                + /*  */"<w:tag w:val=\"string\"/>"//
                + /*  */"<w:text/>"//
                + /*  */"<w:showingPlcHdr/>"//
                + /*  */"<w15:appearance w15:val=\"tags\"/>"//
                + /*  */"<w:alias w:val=\"string\"/>"//
                + "</w:sdtPr>"//
                + "<w:sdtContent>"//
                + /*  */"<w:p><w:r>" + /*       */"<w:rPr><w:rStyle w:val=\"PlaceholderText\"/></w:rPr>"
                + /*       */"<w:t>string</w:t>"//
                + /*  */"</w:r></w:p>" //
                + "</w:sdtContent>"//
                + "</w:sdt>";
        assertTrue(doucmentXml.contains(expected));
    } finally {
        templateFile.delete();
    }

}

From source file:com.liferay.mobile.sdk.core.tests.MobileSDKCoreTests.java

private void checkJar(File jar, boolean src) throws Exception {
    assertTrue(jar.exists());//from ww w  . j ava  2 s.co m

    final ZipFile zipFile = new ZipFile(jar);
    final ZipEntry manifest = zipFile.getEntry("META-INF/MANIFEST.MF");

    assertNotNull(manifest);

    final String manifestContents = CoreUtil.readStreamToString(zipFile.getInputStream(manifest));

    assertTrue(manifestContents.startsWith("Manifest-Version: 1.0"));

    boolean valid = false;

    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        final String entryName = entries.nextElement().getName();

        if (entryName.startsWith(PACKAGE.split("\\.")[0]) && entryName.endsWith(src ? ".java" : ".class")) {
            valid = true;
            break;
        }
    }

    zipFile.close();

    assertTrue(valid);
}

From source file:org.apache.archiva.remotedownload.DownloadSnapshotTest.java

@Test
public void downloadSNAPSHOT() throws Exception {

    File tmpIndexDir = new File(System.getProperty("java.io.tmpdir") + "/tmpIndex");
    if (tmpIndexDir.exists()) {
        FileUtils.deleteDirectory(tmpIndexDir);
    }/*from www .ja va 2 s .  c  om*/
    String id = Long.toString(System.currentTimeMillis());
    ManagedRepository managedRepository = new ManagedRepository();
    managedRepository.setId(id);
    managedRepository.setName("name of " + id);
    managedRepository.setLocation(System.getProperty("basedir") + "/src/test/repositories/snapshot-repo");
    managedRepository.setIndexDirectory(System.getProperty("java.io.tmpdir") + "/tmpIndex/" + id);

    ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();

    if (managedRepositoriesService.getManagedRepository(id) != null) {
        managedRepositoriesService.deleteManagedRepository(id, false);
    }

    getManagedRepositoriesService().addManagedRepository(managedRepository);

    RoleManagementService roleManagementService = getRoleManagementService(authorizationHeader);

    if (!roleManagementService.templatedRoleExists(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id)) {
        roleManagementService.createTemplatedRole(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id);
    }

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

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

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

    File file = new File("target/archiva-model-1.4-M4-SNAPSHOT.jar");
    if (file.exists()) {
        file.delete();
    }

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

    httpWagon.get(
            "/repository/" + id
                    + "/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-SNAPSHOT.jar",
            file);

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

}

From source file:com.github.swt_release_fetcher.Artifact.java

private void extractFromZip(ZipFile zipFile, String fileNameToExtract, File targetFile) throws IOException {
    ZipEntry entry = zipFile.getEntry(fileNameToExtract);
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {/*from w w  w  .ja  va  2s  .  c  o m*/
        inputStream = zipFile.getInputStream(entry);
        outputStream = new FileOutputStream(targetFile);
        IOUtils.copy(inputStream, outputStream);
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:com.liferay.mobile.sdk.core.tests.MobileSDKCoreTests.java

private void checkJarPaths(File jar, String[] paths) throws Exception {
    assertTrue(jar.exists());//  w ww .  ja va  2  s . co m

    final ZipFile jarFile = new ZipFile(jar);

    for (String path : paths) {
        ZipEntry entry = jarFile.getEntry(path);

        assertNotNull(entry);
    }

    jarFile.close();
}

From source file:org.jboss.as.test.integration.jdr.mgmt.JdrReportManagmentTestCase.java

private void validateEntryNotEmpty(String fileName, ZipFile reportZip, String reportName) {
    String entryInZip = reportName + "/" + fileName;
    ZipEntry entry = reportZip.getEntry(entryInZip);
    Assert.assertNotNull("Report entry " + fileName + " missing from JDR report " + reportZip.getName(), entry);
    Assert.assertTrue("Report entry " + fileName + " was empty or could not be determined",
            entry.getSize() > 0);/*  w  w  w.ja v a 2 s  .  c  o m*/
}

From source file:it.readbeyond.minstrel.librarian.FormatHandlerABZ.java

private List<ZipAsset> parseM3UPlaylistEntry(String path, String playlistEntry) {
    List<ZipAsset> assets = new ArrayList<ZipAsset>();
    try {//from  w w  w. j  ava2 s .c  om
        ZipFile zipFile = new ZipFile(new File(path), ZipFile.OPEN_READ);
        ZipEntry ze = zipFile.getEntry(playlistEntry);
        String text = this.getZipEntryText(zipFile, ze);
        String[] lines = text.split("\n");

        // check that the first line starts with the M3U header
        if ((lines.length > 0) && (lines[0].startsWith(ABZ_M3U_HEADER))) {
            for (int i = 1; i < lines.length; i++) {
                String line = lines[i].trim();

                // if line starts with the M3U preamble, parse it
                if (line.startsWith(ABZ_M3U_LINE_PREAMBLE)) {
                    HashMap<String, String> meta = new HashMap<String, String>();

                    // get track duration and title 
                    Matcher m = ABZ_M3U_LINE_PATTERN.matcher(line);
                    if (m.find()) {
                        meta.put("duration", m.group(1));
                        meta.put("title", m.group(2));
                    }
                    String line2 = lines[i + 1].trim();

                    // generate entry path
                    File w = new File(new File(playlistEntry).getParent(), line2);
                    line2 = w.getAbsolutePath().substring(1);

                    // add asset
                    assets.add(new ZipAsset(line2, meta));

                    // go to the next pair of lines
                    i += 1;
                }
            }
        }

        // close ZIP
        zipFile.close();
    } catch (Exception e) {
        // nothing
    }
    return assets;
}