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:io.apiman.common.plugin.PluginClassLoader.java

/**
 * @see java.lang.ClassLoader#findResource(java.lang.String)
 *///from   www  .  j  a v a 2 s .  c  o  m
@Override
protected URL findResource(String name) {
    Enumeration<? extends ZipEntry> entries = this.pluginArtifactZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        if (zipEntry.getName().equalsIgnoreCase(name)
                || zipEntry.getName().equalsIgnoreCase("WEB-INF/classes/" + name)) {
            try {
                return extractResource(zipEntry);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    ZipEntry entry;
    File file;
    for (ZipFile zipFile : this.dependencyZips) {
        entry = zipFile.getEntry(name);
        if (entry != null) {
            try {
                file = new File(zipFile.getName());
                URL zipUrl = file.toURI().toURL();
                URL entryUrl = new URL("jar:" + zipUrl + "!/" + name);
                return entryUrl;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }

    return super.findResource(name);
}

From source file:org.kles.m3.M3ClassLoader.java

private DataInputStream getStreamFromJar(String name, File path) throws MAKException {
    if (!this.m_zipProblems.contains(path)) {
        try {/*from  w  w w. j  av  a2 s.c o 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) {
            logger.log(Level.SEVERE, "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:net.wasdev.maven.plugins.swaggerdocgen.SwaggerProcessor.java

public String getDocument() {
    ZipFile warZipFile = null;
    try {//w  w  w . j  a  va  2  s.  c  o  m
        warZipFile = new ZipFile(warFile);

        // Search for META-INF/swagger.json or META-INF/swagger.yaml in the
        // WAR
        ZipEntry entry = warZipFile.getEntry(DEFAULT_SWAGGER_JSON_LOCATION);
        if (entry == null) {
            entry = warZipFile.getEntry(DEFAULT_SWAGGER_YAML_LOCATION);
        }
        if (entry != null) {
            InputStream swaggerStream = warZipFile.getInputStream(entry);
            String swaggerDoc = getSwaggerDocFromStream(swaggerStream);
            // Swagger swaggerModel = new SwaggerParser().parse(swaggerDoc,
            // null, false);
            Swagger swaggerModel = new SwaggerParser().parse(swaggerDoc, null);

            return createYAMLfromPojo(swaggerModel);
        }

        // Search for META-INF/stub/swagger.json or
        // META-INF/stub/swagger.yaml in the WAR
        Swagger swaggerStubModel = null;
        entry = warZipFile.getEntry(DEFAULT_SWAGGER_JSON_STUB_LOCATION);
        if (entry == null) {
            entry = warZipFile.getEntry(DEFAULT_SWAGGER_YAML_STUB_LOCATION);
        }
        if (entry != null) {
            InputStream swaggerStream = warZipFile.getInputStream(entry);
            String swaggerDoc = getSwaggerDocFromStream(swaggerStream);
            swaggerStubModel = new SwaggerParser().parse(swaggerDoc, null);
        }
        // Scan the WAR for annotations and merge with the stub document.
        return getSwaggerDocFromAnnotatedClasses(warZipFile, swaggerStubModel);
    } catch (IOException ioe) {
        logger.severe("Failed to generate the Swagger document.");
    } finally {
        tryToClose(warZipFile);
    }
    return null;
}

From source file:io.apiman.common.plugin.PluginClassLoader.java

/**
 * @see java.lang.ClassLoader#findResources(java.lang.String)
 *//*from  ww w.  ja va2 s  .  co  m*/
@Override
protected Enumeration<URL> findResources(String name) throws IOException {
    List<URL> resources = new ArrayList<>();

    // Search for the artifact in the plugin WAR itself
    Enumeration<? extends ZipEntry> entries = this.pluginArtifactZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        if (zipEntry.getName().equalsIgnoreCase(name)) {
            try {
                resources.add(extractResource(zipEntry));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    // Now also add any resources found in dependencies
    ZipEntry entry;
    File file;
    for (ZipFile zipFile : this.dependencyZips) {
        entry = zipFile.getEntry(name);
        if (entry != null) {
            try {
                file = new File(zipFile.getName());
                URL zipUrl = file.toURI().toURL();
                URL entryUrl = new URL("jar:" + zipUrl + "!/" + name);
                resources.add(entryUrl);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }

    // Return the discovered resources as an Enumeration
    final Iterator<URL> iterator = resources.iterator();
    return new Enumeration<URL>() {
        @Override
        public boolean hasMoreElements() {
            return iterator.hasNext();
        }

        @Override
        public URL nextElement() {
            return iterator.next();
        }
    };
}

From source file:abfab3d.io.input.STSReader.java

/**
 * Load a STS file into a grid./* www.  j ava2  s  . c  om*/
 *
 * @param file The zip file
 * @return
 * @throws java.io.IOException
 */
public AttributeGrid loadGrid(String file) throws IOException {

    ZipFile zip = null;

    try {
        zip = new ZipFile(file);

        ZipEntry entry = zip.getEntry("manifest.xml");
        if (entry == null) {
            throw new IOException("Cannot find manifest.xml in top level");
        }

        InputStream is = zip.getInputStream(entry);
        mf = parseManifest(is);

        if (mf == null) {
            throw new IOException("Could not parse manifest file");
        }
        /*
                    AttributeGrid ret_val = new ArrayAttributeGridByte(mf.getGridSizeX(),mf.getGridSizeY(),mf.getGridSizeZ(),mf.getVoxelSize(),mf.getVoxelSize());
                
                    double[] bounds = new double[6];
                
                    bounds[0] = mf.getOriginX();
                    bounds[1] = mf.getOriginX() + mf.getGridSizeX() * mf.getVoxelSize();
                    bounds[2] = mf.getOriginY();
                    bounds[3] = mf.getOriginY() + mf.getGridSizeY() * mf.getVoxelSize();
                    bounds[4] = mf.getOriginZ();
                    bounds[5] = mf.getOriginZ() + mf.getGridSizeZ() * mf.getVoxelSize();
                    ret_val.setGridBounds(bounds);
                
                    List<Channel> channels = mf.getChannels();
                
                    for(Channel chan : channels) {
        if (chan.getType().getId() == Channel.Type.DENSITY.getId()) {
            SlicesReader sr = new SlicesReader();
            sr.readSlices(ret_val,zip,chan.getSlices(),0,0,mf.getGridSizeY());
        }
                    }
        */
        return null;
    } finally {
        if (zip != null)
            zip.close();
    }
}

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

void doRecoveryInstall() {
    final ProgressDialog dlg = new ProgressDialog(this);
    dlg.setTitle(R.string.installing);//from w ww.  j  a v a  2s  .c  om
    dlg.setMessage(getString(R.string.installing_superuser));
    dlg.setIndeterminate(true);
    dlg.show();
    new Thread() {
        void doEntry(ZipOutputStream zout, String entryName, String dest) throws IOException {
            ZipFile zf = new ZipFile(getPackageCodePath());
            ZipEntry ze = zf.getEntry(entryName);
            zout.putNextEntry(new ZipEntry(dest));
            InputStream in;
            StreamUtility.copyStream(in = zf.getInputStream(ze), zout);
            zout.closeEntry();
            in.close();
            zf.close();
        }

        public void run() {
            try {
                File zip = getFileStreamPath("superuser.zip");
                ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zip));
                doEntry(zout, "assets/update-binary", "META-INF/com/google/android/update-binary");
                doEntry(zout, "assets/install-recovery.sh", "install-recovery.sh");
                zout.close();

                ZipFile zf = new ZipFile(getPackageCodePath());
                ZipEntry ze = zf.getEntry("assets/" + getArch() + "/reboot");
                InputStream in;
                FileOutputStream reboot;
                StreamUtility.copyStream(in = zf.getInputStream(ze),
                        reboot = openFileOutput("reboot", MODE_PRIVATE));
                reboot.close();
                in.close();

                final File su = extractSu();

                String command = String.format("cat %s > /cache/superuser.zip\n", zip.getAbsolutePath())
                        + String.format("cat %s > /cache/su\n", su.getAbsolutePath())
                        + String.format("cat %s > /cache/Superuser.apk\n", getPackageCodePath())
                        + "mkdir /cache/recovery\n"
                        + "echo '--update_package=CACHE:superuser.zip' > /cache/recovery/command\n"
                        + "chmod 644 /cache/superuser.zip\n" + "chmod 644 /cache/recovery/command\n" + "sync\n"
                        + String.format("chmod 755 %s\n", getFileStreamPath("reboot").getAbsolutePath())
                        + "reboot recovery\n";
                Process p = Runtime.getRuntime().exec("su");
                p.getOutputStream().write(command.getBytes());
                p.getOutputStream().close();
                File rebootScript = getFileStreamPath("reboot.sh");
                StreamUtility.writeFile(rebootScript,
                        "reboot recovery ; " + getFileStreamPath("reboot").getAbsolutePath() + " recovery ;");
                p.waitFor();
                Runtime.getRuntime().exec(new String[] { "su", "-c", ". " + rebootScript.getAbsolutePath() });
                if (p.waitFor() != 0)
                    throw new Exception("non zero result");
            } catch (Exception ex) {
                ex.printStackTrace();
                dlg.dismiss();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setPositiveButton(android.R.string.ok, null);
                        builder.setTitle(R.string.install);
                        builder.setMessage(R.string.install_error);
                        builder.create().show();
                    }
                });
            }
        }
    }.start();
}

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

private Boolean writeArchive(final File dest, final String path) {
    final boolean isJar = isJarOperation();
    FileOutputStream fos = null;//  ww  w .j  a  v a 2 s .c o  m
    ZipOutputStream zos = null;
    ZipFile zf = null;
    try {
        fos = new FileOutputStream(dest);
        zos = isJar ? new JarOutputStream(fos) : new ZipOutputStream(fos);

        zf = isJar ? new JarFile(dest) : new ZipFile(dest);

        final ZipEntry entry = zf.getEntry(path);
        zos.putNextEntry(entry);
        copy(stream, zos);

        return true;
    } catch (final IOException e) {
        error = new TransferException("Failed to write path: %s to NEW archive: %s. Reason: %s", e, path, dest,
                e.getMessage());
    } finally {
        closeQuietly(zos);
        closeQuietly(fos);
        if (zf != null) {
            //noinspection EmptyCatchBlock
            try {
                zf.close();
            } catch (final IOException e) {
            }
        }
        closeQuietly(stream);
    }

    return false;
}

From source file:org.gitools.matrix.format.TdmMatrixFormat.java

private MTabixIndex readMtabixIndex(IResourceLocator resourceLocator, IProgressMonitor progressMonitor)
        throws IOException, URISyntaxException {

    // Check if we are using mtabix
    URL dataURL = resourceLocator.getURL();

    URL indexURL = null;//from  w  w  w  .ja v a 2s  .c o m

    if (!dataURL.getPath().endsWith("zip")) {
        IResourceLocator mtabix = resourceLocator.getReferenceLocator(resourceLocator.getName() + ".gz.mtabix");
        indexURL = mtabix.getURL();
    } else {
        //ZipFile zipFile = new ZipFile(new File(dataURL.toURI()));
        ZipFile zipFile = new ZipFile(resourceLocator.getReadFile());
        ZipEntry entry = zipFile.getEntry(resourceLocator.getName() + ".gz.mtabix");

        if (entry == null) {
            return null;
        }

        // Copy index to a temporal file
        File indexFile = File.createTempFile("gitools-cache-", "zip_mtabix");
        indexFile.deleteOnExit();
        IOUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(indexFile));
        indexURL = indexFile.toURL();

        // Copy data to a temporal file
        File dataFile = File.createTempFile("gitools-cache-", "zip_bgz");
        dataFile.deleteOnExit();

        InputStream dataStream = resourceLocator.getParentLocator().openInputStream(progressMonitor);

        IOUtils.copy(dataStream, new FileOutputStream(dataFile));
        dataURL = dataFile.toURL();

    }

    File dataFile = new File(dataURL.toURI());
    File indexFile = new File(indexURL.toURI());

    if (!indexFile.exists()) {
        return null;
    }

    MTabixConfig mtabixConfig = new MTabixConfig(dataFile, indexFile, new DefaultKeyParser(1, 0));
    MTabixIndex index = new MTabixIndex(mtabixConfig);
    index.loadIndex();

    return index;

}

From source file:com.shazam.fork.io.ClassesDexFileExtractor.java

private void dumpDexFilesFromApk(File apkFile, File outputFolder) {
    ZipFile zip = null;
    InputStream classesDexInputStream = null;
    FileOutputStream fileOutputStream = null;

    try {/*  w ww.j a v a2  s .co  m*/
        zip = new ZipFile(apkFile);
        int index = 1;
        String currentDex;
        while (true) {
            currentDex = CLASSES_PREFIX + (index > 1 ? index : "") + DEX_EXTENSION;
            ZipEntry classesDex = zip.getEntry(currentDex);
            if (classesDex != null) {
                File dexFileDestination = new File(outputFolder, currentDex);
                classesDexInputStream = zip.getInputStream(classesDex);
                fileOutputStream = new FileOutputStream(dexFileDestination);
                copyLarge(classesDexInputStream, fileOutputStream);
                index++;
            } else {
                break;
            }
        }
    } catch (IOException e) {
        throw new DexFileExtractionException(
                "Error when trying to scan " + apkFile.getAbsolutePath() + " for test classes.", e);
    } finally {
        closeQuietly(classesDexInputStream);
        closeQuietly(fileOutputStream);
        closeQuietly(zip);
    }
}

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

private String getOPFPath(File f) {
    String ret = null;//from  w ww.  jav  a2  s . co  m
    try {
        ZipFile zipFile = new ZipFile(f, ZipFile.OPEN_READ);
        ZipEntry containerEntry = zipFile.getEntry(EPUB_CONTAINER_PATH);
        if (containerEntry != null) {
            InputStream is = zipFile.getInputStream(containerEntry);
            parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
            parser.setInput(is, null);
            parser.nextTag();
            ret = parseContainer();
            is.close();
        }
        zipFile.close();
    } catch (Exception e) {
        // nop
    }
    return ret;
}