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.taverna.scufl2.rdfxml.TestResourcesInZip.java

@Test
public void singleFile() throws Exception {
    UCFPackage resources = originalBundle.getResources();
    assertEquals(APPLICATION_VND_TAVERNA_SCUFL2_WORKFLOW_BUNDLE, resources.getPackageMediaType());
    resources.addResource("Hello there", "hello.txt", "text/plain");
    File bundleFile = tempFile();
    bundleIO.writeBundle(originalBundle, bundleFile, APPLICATION_VND_TAVERNA_SCUFL2_WORKFLOW_BUNDLE);
    assertEquals(APPLICATION_VND_TAVERNA_SCUFL2_WORKFLOW_BUNDLE, resources.getPackageMediaType());
    assertEquals(1, resources.getRootFiles().size());
    assertEquals("workflowBundle.rdf", resources.getRootFiles().get(0).getPath());

    ZipFile zipFile = new ZipFile(bundleFile);
    ZipEntry hello = zipFile.getEntry("hello.txt");
    assertEquals("hello.txt", hello.getName());
    assertEquals("Hello there", IOUtils.toString(zipFile.getInputStream(hello), "ASCII"));
}

From source file:org.apache.taverna.scufl2.rdfxml.TestResourcesInZip.java

@Test
public void differentMediaType() throws Exception {
    UCFPackage resources = originalBundle.getResources();
    resources.setPackageMediaType("application/x-something-else");
    assertEquals("application/x-something-else", resources.getPackageMediaType());

    resources.addResource("Hello there", "hello.txt", "text/plain");
    File bundleFile = tempFile();
    bundleIO.writeBundle(originalBundle, bundleFile, APPLICATION_VND_TAVERNA_SCUFL2_WORKFLOW_BUNDLE);
    assertEquals("application/x-something-else", resources.getPackageMediaType());
    assertEquals(0, resources.getRootFiles().size());
    // RDFXMLWriter does not touch the rootFile or media type if it's non-null

    ZipFile zipFile = new ZipFile(bundleFile);
    ZipEntry hello = zipFile.getEntry("hello.txt");
    assertEquals("hello.txt", hello.getName());
    assertEquals("Hello there", IOUtils.toString(zipFile.getInputStream(hello), "ASCII"));
}

From source file:be.fedict.eid.pkira.blm.model.contracts.CertificateTest.java

protected void validateNextZipEntry(ZipFile zipFile, String fileName, String expectedText) throws IOException {
    ZipEntry entry = zipFile.getEntry(fileName);

    byte[] expectedBytes = expectedText.getBytes();
    byte[] actualBytes = new byte[expectedBytes.length];
    zipFile.getInputStream(entry).read(actualBytes);

    assertEquals(entry.getName(), fileName);
    assertEquals(entry.getSize(), expectedBytes.length);
    assertEquals(actualBytes, expectedBytes);
}

From source file:eu.europa.esig.dss.asic.signature.asice.ASiCESignatureFilenameTest.java

@Override
public void signAndVerify() throws IOException {
    String containerTemporaryPath = temporaryFolder.newFile().getPath();
    getSignatureParameters().aSiC().setSignatureFileName("signatures2047.xml");
    documentToSign = sign();/*w  w w  .j  ava 2 s.co m*/
    documentToSign.save(containerTemporaryPath);
    ZipFile zip = new ZipFile(containerTemporaryPath);
    assertNotNull("Signature file name is not correct", zip.getEntry("META-INF/signatures2047.xml"));
    IOUtils.closeQuietly(zip);
}

From source file:org.wte4j.impl.word.Docx4JWordTemplateTest.java

@Test
public void writeAsOpenXMLTest() throws IOException, Docx4JException, JAXBException {
    Docx4JWordTemplate doc = new Docx4JWordTemplate();
    File file = File.createTempFile("test", "docx");
    OutputStream out = FileUtils.openOutputStream(file);
    try {//from w  ww. j  a  va  2  s. c  o m
        doc.writeAsOpenXML(out);
    } finally {
        IOUtils.closeQuietly(out);
    }
    ZipFile zipFile = new ZipFile(file);
    ZipEntry entry = zipFile.getEntry("word/document.xml");
    assertNotNull(entry);
}

From source file:org.roda.common.certification.ODFSignatureUtils.java

private static ByteArrayOutputStream addSignatureToStream(ZipFile zipFile, Element rootManifest,
        Element rootSignatures) throws Exception {

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

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

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

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

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

    zos.close();
    zipFile.close();

    return baos;
}

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

@Override
protected boolean validate(File jarFile, Map<String, String> params) throws Exception {
    ZipFile zipFile = new ZipFile(jarFile);
    StringWriter sw = new StringWriter();
    IOUtils.copy(new InputStreamReader(zipFile.getInputStream(zipFile.getEntry("META-INF/MANIFEST.MF"))), sw);
    return !sw.getBuffer().toString().contains("Digest:");
}

From source file:pl.edu.icm.cermine.bibref.AbstractBibReferenceExtractorTest.java

@Test
public void metadataExtractionTest() throws AnalysisException, JDOMException, IOException, SAXException,
        TransformationException, URISyntaxException {
    InputStream expStream = this.getClass().getResourceAsStream(EXP_FILE);
    BufferedReader expReader = new BufferedReader(new InputStreamReader(expStream));

    StringBuilder sb = new StringBuilder();
    String line;/*  ww  w  .  j  a  va 2 s. co  m*/
    while ((line = expReader.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
    }
    expStream.close();
    expReader.close();

    URL url = this.getClass().getResource(TEST_FILE);
    ZipFile zipFile = new ZipFile(new File(url.toURI()));
    InputStream inputStream = zipFile.getInputStream(zipFile.getEntry("out.xml"));
    BxDocument expDocument = new BxDocument().setPages(bxReader.read(new InputStreamReader(inputStream)));
    String[] references = getExtractor().extractBibReferences(expDocument);

    assertEquals(StringUtils.join(references, "\n"), sb.toString().trim());
}

From source file:sapience.features.streams.kml.KMLRetriever.java

/**
 * If the resource is hidden in a ZIP file, download, cache, and extract resource
 * //  w  w  w .  j  ava 2s  .c o m
 * @param url
 * @return the input stream with the given source
 * @throws IOException 
 */
private InputStream extractSource(URL url, String entry) throws IOException {
    InputStream in = null;
    OutputStream out = null;
    try {

        File f = File.createTempFile("zippedSource", ".zip");
        out = new FileOutputStream(f);
        in = url.openStream();

        IOUtils.copy(in, out);
        ZipFile zip = new ZipFile(f);
        ZipEntry ze = zip.getEntry(entry);
        return zip.getInputStream(ze);

    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }

}

From source file:de.tudarmstadt.ukp.clarin.webanno.brat.dao.DaoUtilsTest.java

@SuppressWarnings("resource")
@Test/*from w w w  .  j ava  2  s  . co m*/
public void testZipFolder() {

    File toBeZippedFiles = new File("src/test/resources");
    File zipedFiles = new File("target/" + toBeZippedFiles.getName() + ".zip");
    try {
        ZipUtils.zipFolder(toBeZippedFiles, zipedFiles);
    } catch (Exception e) {
        LOG.info("Zipping fails" + ExceptionUtils.getRootCauseMessage(e));
    }

    FileInputStream fs;
    try {
        fs = new FileInputStream(zipedFiles);
        ZipInputStream zis = new ZipInputStream(fs);
        ZipEntry zE;
        while ((zE = zis.getNextEntry()) != null) {
            for (File toBeZippedFile : toBeZippedFiles.listFiles()) {
                if ((FilenameUtils.getBaseName(toBeZippedFile.getName()))
                        .equals(FilenameUtils.getBaseName(zE.getName()))) {
                    // Extract the zip file, save it to temp and compare
                    ZipFile zipFile = new ZipFile(zipedFiles);
                    ZipEntry zipEntry = zipFile.getEntry(zE.getName());
                    InputStream is = zipFile.getInputStream(zipEntry);
                    File temp = File.createTempFile("temp", ".txt");
                    OutputStream out = new FileOutputStream(temp);
                    IOUtils.copy(is, out);
                    FileUtils.contentEquals(toBeZippedFile, temp);
                }
            }
            zis.closeEntry();
        }
    } catch (FileNotFoundException e) {
        LOG.info("zipped file not found " + ExceptionUtils.getRootCauseMessage(e));
    } catch (IOException e) {
        LOG.info("Unable to get file " + ExceptionUtils.getRootCauseMessage(e));
    }

}