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.digidoc4j.impl.bdoc.BDocContainerTest.java

@Test
public void signatureFileNamesShouldBeInSequence() throws Exception {
    Container container = createContainerWithFile("testFiles/test.txt", "text/plain");
    signContainer(container);// www.  j  a v a 2 s.  co m
    signContainer(container);
    signContainer(container);
    String containerPath = testFolder.newFile().getPath();
    container.saveAsFile(containerPath);
    ZipFile zip = new ZipFile(containerPath);
    assertNotNull(zip.getEntry("META-INF/signatures0.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures1.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures2.xml"));
}

From source file:com.edgenius.wiki.service.impl.ThemeServiceImpl.java

private String getMetafile(File zipFile, String fileName) {
    String content = "";
    ZipFile zip = null;
    try {//  ww  w.ja  va2s.c  o m
        zip = new ZipFile(zipFile);
        ZipEntry entry = zip.getEntry(fileName);
        if (entry != null) {
            content = IOUtils.toString(zip.getInputStream(entry));
        }
    } catch (Exception e) {
        log.info("backup/restore file comment not available:" + zipFile.getAbsolutePath());
    } finally {
        if (zip != null)
            try {
                zip.close();
            } catch (Exception e) {
            }
    }

    return content;
}

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

@Test
public void saveExistingContainer() throws Exception {
    Container container = open("testFiles/asics_testing_two_signatures.bdoc");
    String containerPath = testFolder.newFile("test-container.asice").getPath();
    container.saveAsFile(containerPath);
    Container savedContainer = open(containerPath);
    assertTrue(savedContainer.validate().isValid());
    assertEquals(1, savedContainer.getDataFiles().size());
    assertEquals(2, savedContainer.getSignatures().size());
    ZipFile zip = new ZipFile(containerPath);
    assertNotNull(zip.getEntry("mimetype"));
    assertNotNull(zip.getEntry("test.txt"));
    assertNotNull(zip.getEntry("META-INF/manifest.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures0.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures1.xml"));
}

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

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

    Signature signature1 = SignatureBuilder.aSignature(container).withSignatureId("SIGNATURE-1")
            .withSignatureToken(PKCS12_SIGNER).invokeSigning();
    container.addSignature(signature1);/*from   ww  w  .  j a  v  a2  s. c o  m*/

    Signature signature2 = SignatureBuilder.aSignature(container).withSignatureId("SIGNATURE-2")
            .withSignatureToken(PKCS12_SIGNER).invokeSigning();
    container.addSignature(signature2);

    container.saveAsFile("setsSignatureId.bdoc");

    container = open("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.bdval.BDVModel.java

/**
 * Loads a BDVal model from disk. BDVal models are generated with the
 * {@link org.bdval.DiscoverAndValidate} tools (BDVal).
 *
 * @param options specific options to use when loading the model
 * @throws IOException            if there is a problem accessing the model
 * @throws ClassNotFoundException if the type of the model is not recognized
 *//*from w  w  w.ja  v a2 s.  c om*/
public void load(final DAVOptions options) throws IOException, ClassNotFoundException {
    final boolean zipExists = new File(zipFilename).exists();
    if (LOG.isDebugEnabled()) {
        LOG.debug("model zip file exists: " + BooleanUtils.toStringYesNo(zipExists));
    }
    properties.clear();
    properties.setDelimiterParsingDisabled(true);
    // check to see if a zip file exists - if it doesn't we assume it's an old binary format
    if (zipModel && zipExists) {
        LOG.info("Reading model from filename: " + zipFilename);

        final ZipFile zipFile = new ZipFile(zipFilename);
        try {
            final ZipEntry propertyEntry = zipFile.getEntry(FilenameUtils.getName(modelPropertiesFilename));
            // load properties
            properties.clear();
            properties.addAll(loadProperties(zipFile.getInputStream(propertyEntry), options));

            // the platform is more than one entry in the zip, so here we pass the whole zip
            trainingPlatform = options.trainingPlatform = loadPlatform(zipFile);

            if (isConsensusModel()) {
                int index = 0;
                final ObjectList<String> modelJurorFilePrefixes = new ObjectArrayList<String>();
                String nextFilename;
                while ((nextFilename = (String) properties
                        .getProperty("bdval.consensus.model." + Integer.toString(index))) != null) {
                    modelJurorFilePrefixes.add(nextFilename);
                    index++;
                }

                delegate = new ConsensusBDVModel(modelFilenamePrefix,
                        modelJurorFilePrefixes.toArray(new String[modelJurorFilePrefixes.size()]));
                delegate.load(options);
                setGeneList(convertTrainingPlatformToGeneList(options));
                return;
            } else {
                probesetScaleMeanMap = options.probesetScaleMeanMap = loadMeansMap(
                        zipFile.getInputStream(zipFile.getEntry(FilenameUtils.getName(meansMapFilename))));
                probesetScaleRangeMap = options.probesetScaleRangeMap = loadRangeMap(
                        zipFile.getInputStream(zipFile.getEntry(FilenameUtils.getName(rangeMapFilename))));
                setGeneList(convertTrainingPlatformToGeneList(options));
            }

            final String modelParameters = properties.getString("training.classifier.parameters");

            LOG.info("Loading model " + modelFilename);
            final InputStream modelStream = zipFile
                    .getInputStream(zipFile.getEntry(FilenameUtils.getName(modelFilename)));
            helper = ClassificationModel.load(modelStream, modelParameters);
            LOG.info("Model loaded.");

            options.classiferClass = helper.classifier.getClass();
            // we don't have a way to inspect the saved model for parameters used during training:
            options.classifierParameters = ClassificationModel.splitModelParameters(modelParameters);
        } finally {
            try {
                zipFile.close();
            } catch (IOException e) { // NOPMD
                // ignore since there is not much we can do anyway
            }
        }
    } else {
        final File propertyFile = new File(modelFilenamePrefix + "." + ModelFileExtension.props.toString());
        LOG.debug("Loading properties from " + propertyFile.getAbsolutePath());
        final Properties properties = loadProperties(FileUtils.openInputStream(propertyFile), options);

        trainingPlatform = options.trainingPlatform = (GEOPlatformIndexed) BinIO.loadObject(platformFilename);

        if (isConsensusModel()) {
            int index = 0;
            final ObjectList<String> modelJurorFilePrefixes = new ObjectArrayList<String>();
            String nextFilename = null;
            while ((nextFilename = (String) properties
                    .getProperty("bdval.consensus.model." + Integer.toString(index))) != null) {
                modelJurorFilePrefixes.add(nextFilename);
                index++;
            }

            delegate = new ConsensusBDVModel(modelFilenamePrefix,
                    modelJurorFilePrefixes.toArray(new String[modelJurorFilePrefixes.size()]));
            delegate.load(options);
            setGeneList(convertTrainingPlatformToGeneList(options));
            return;
        } else {
            probesetScaleMeanMap = options.probesetScaleMeanMap = (Object2DoubleMap<MutableString>) BinIO
                    .loadObject(modelFilenamePrefix + ".means");
            if (LOG.isDebugEnabled()) {
                LOG.debug("Number of entries in means map = " + probesetScaleMeanMap.size());
            }
            probesetScaleRangeMap = options.probesetScaleRangeMap = (Object2DoubleMap<MutableString>) BinIO
                    .loadObject(modelFilenamePrefix + ".ranges");
            if (LOG.isDebugEnabled()) {
                LOG.debug("Number of entries in range map = " + probesetScaleRangeMap.size());
            }
            setGeneList(convertTrainingPlatformToGeneList(options));
        }

        final String modelParameters = properties.getString("training.classifier.parameters");

        LOG.info("Loading model " + modelFilename);
        helper = ClassificationModel.load(modelFilename, modelParameters);
        LOG.info("Model loaded.");

        options.classiferClass = helper.classifier.getClass();
        // we don't have a way to inspect the saved model for parameters used during training:
        options.classifierParameters = ClassificationModel.splitModelParameters(modelParameters);
    }
}

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

@Test
public void setsDefaultSignatureId() throws Exception {
    Container container = createContainerWithFile("testFiles/test.txt", "text/plain");
    signContainer(container);//w w w  .ja v  a2 s  . com
    signContainer(container);
    container.save("testSetsDefaultSignatureId.bdoc");

    container = open("testSetsDefaultSignatureId.bdoc");
    String signature1Id = container.getSignatures().get(0).getId();
    String signature2Id = container.getSignatures().get(1).getId();
    assertFalse(StringUtils.equals(signature1Id, signature2Id));
    assertTrue(signature1Id.startsWith("id-"));
    assertTrue(signature2Id.startsWith("id-"));

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

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

@Test
public void zipFileComment() throws Exception {
    Container container = createContainerWithFile("testFiles/test.txt", "text/plain");
    signContainer(container);/* w ww. j  a  va 2 s .  c  o m*/
    container.save("testZipFileComment.bdoc");

    String expectedComment = Helper.createBDocUserAgent(SignatureProfile.LT_TM);
    ZipFile zipFile = new ZipFile("testZipFileComment.bdoc");
    assertEquals(expectedComment, zipFile.getEntry("mimetype").getComment());
    assertEquals(expectedComment, zipFile.getEntry("META-INF/manifest.xml").getComment());
    assertEquals(expectedComment, zipFile.getEntry("META-INF/manifest.xml").getComment());
    assertEquals(expectedComment, zipFile.getEntry("META-INF/signatures0.xml").getComment());
    assertEquals(expectedComment, zipFile.getEntry("test.txt").getComment());
}

From source file:com.aimfire.main.MainActivity.java

private void displayVersion() {
    try {/*  ww w .j  a v a2s.c  o  m*/
        ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);
        ZipFile zf = new ZipFile(ai.sourceDir);
        ZipEntry ze = zf.getEntry("classes.dex");
        long time = ze.getTime();
        String s = SimpleDateFormat.getInstance().format(new java.util.Date(time));
        zf.close();

        if (BuildConfig.DEBUG)
            printDebugMsg("App creation time: " + s + "\n");

    } catch (Exception e) {
    }
}

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

@Test
public void whenSigningExistingContainer_withTwoSignatures_shouldCreateSignatureFileName_signatures2()
        throws Exception {
    ZipFile zip = new ZipFile("testFiles/asics_testing_two_signatures.bdoc");
    assertNotNull(zip.getEntry("META-INF/signatures0.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures1.xml"));
    Container container = open("testFiles/asics_testing_two_signatures.bdoc");
    signContainer(container);//from w w w. j  a  va 2s.  c o  m
    String containerPath = testFolder.newFile().getPath();
    container.saveAsFile(containerPath);
    zip = new ZipFile(containerPath);
    assertNotNull(zip.getEntry("META-INF/signatures0.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures1.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures2.xml"));
}

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

@Test
public void whenSigningExistingContainer_with_signatures1_xml_shouldCreateSignatureFileName_signatures2()
        throws Exception {
    ZipFile zip = new ZipFile("testFiles/DigiDocService_spec_est.pdf-TM-j.bdoc");
    assertNull(zip.getEntry("META-INF/signatures0.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures1.xml"));
    Container container = open("testFiles/DigiDocService_spec_est.pdf-TM-j.bdoc");
    signContainer(container);//w  ww.ja va 2 s .  c  om
    String containerPath = testFolder.newFile().getPath();
    container.saveAsFile(containerPath);
    zip = new ZipFile(containerPath);
    assertNull(zip.getEntry("META-INF/signatures0.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures1.xml"));
    assertNotNull(zip.getEntry("META-INF/signatures2.xml"));
}