Search in sources :

Example 11 with ZipFile

use of java.util.zip.ZipFile in project deeplearning4j by deeplearning4j.

the class ClassPathResource method getFile.

/**
     * Returns requested ClassPathResource as File object
     *
     * Please note: if this method called from compiled jar, temporary file will be created to provide File access
     *
     * @return File requested at constructor call
     * @throws FileNotFoundException
     */
public File getFile() throws FileNotFoundException {
    URL url = this.getUrl();
    if (isJarURL(url)) {
        /*
                This is actually request for file, that's packed into jar. Probably the current one, but that doesn't matters.
             */
        try {
            url = extractActualUrl(url);
            File file = File.createTempFile("datavec_temp", "file");
            file.deleteOnExit();
            ZipFile zipFile = new ZipFile(url.getFile());
            ZipEntry entry = zipFile.getEntry(this.resourceName);
            if (entry == null) {
                if (this.resourceName.startsWith("/")) {
                    entry = zipFile.getEntry(this.resourceName.replaceFirst("/", ""));
                    if (entry == null) {
                        throw new FileNotFoundException("Resource " + this.resourceName + " not found");
                    }
                } else
                    throw new FileNotFoundException("Resource " + this.resourceName + " not found");
            }
            long size = entry.getSize();
            InputStream stream = zipFile.getInputStream(entry);
            FileOutputStream outputStream = new FileOutputStream(file);
            byte[] array = new byte[1024];
            int rd = 0;
            long bytesRead = 0;
            do {
                rd = stream.read(array);
                outputStream.write(array, 0, rd);
                bytesRead += rd;
            } while (bytesRead < size);
            outputStream.flush();
            outputStream.close();
            stream.close();
            zipFile.close();
            return file;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        try {
            URI uri = new URI(url.toString().replaceAll(" ", "%20"));
            return new File(uri.getSchemeSpecificPart());
        } catch (URISyntaxException e) {
            return new File(url.getFile());
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) ZipFile(java.util.zip.ZipFile) ZipFile(java.util.zip.ZipFile)

Example 12 with ZipFile

use of java.util.zip.ZipFile in project deeplearning4j by deeplearning4j.

the class ClassPathResource method getInputStream.

/**
     * Returns requested ClassPathResource as InputStream object
     *
     * @return File requested at constructor call
     * @throws FileNotFoundException
     */
public InputStream getInputStream() throws FileNotFoundException {
    URL url = this.getUrl();
    if (isJarURL(url)) {
        try {
            url = extractActualUrl(url);
            ZipFile zipFile = new ZipFile(url.getFile());
            ZipEntry entry = zipFile.getEntry(this.resourceName);
            if (entry == null) {
                if (this.resourceName.startsWith("/")) {
                    entry = zipFile.getEntry(this.resourceName.replaceFirst("/", ""));
                    if (entry == null) {
                        throw new FileNotFoundException("Resource " + this.resourceName + " not found");
                    }
                } else
                    throw new FileNotFoundException("Resource " + this.resourceName + " not found");
            }
            InputStream stream = zipFile.getInputStream(entry);
            return stream;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        File srcFile = this.getFile();
        return new FileInputStream(srcFile);
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException)

Example 13 with ZipFile

use of java.util.zip.ZipFile in project che by eclipse.

the class Util method getJdkLevel.

/**
     * Get the jdk level of this root.
     * The value can be:
     * <ul>
     * <li>major<<16 + minor : see predefined constants on ClassFileConstants </li>
     * <li><code>0</null> if the root is a source package fragment root or if a Java model exception occured</li>
     * </ul>
     * Returns the jdk level
     */
public static long getJdkLevel(Object targetLibrary) {
    try {
        ClassFileReader reader = null;
        if (targetLibrary instanceof IFolder) {
            // only internal classfolders are allowed
            IFile classFile = findFirstClassFile((IFolder) targetLibrary);
            if (classFile != null)
                reader = Util.newClassFileReader(classFile);
        } else {
            // root is a jar file or a zip file
            ZipFile jar = null;
            try {
                IPath path = null;
                if (targetLibrary instanceof IResource) {
                    path = ((IResource) targetLibrary).getFullPath();
                } else if (targetLibrary instanceof File) {
                    File f = (File) targetLibrary;
                    if (!f.isDirectory()) {
                        path = new Path(((File) targetLibrary).getPath());
                    }
                }
                if (path != null) {
                    jar = JavaModelManager.getJavaModelManager().getZipFile(path);
                    for (Enumeration e = jar.entries(); e.hasMoreElements(); ) {
                        ZipEntry member = (ZipEntry) e.nextElement();
                        String entryName = member.getName();
                        if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) {
                            reader = ClassFileReader.read(jar, entryName);
                            break;
                        }
                    }
                }
            } catch (CoreException e) {
            // ignore
            } finally {
                JavaModelManager.getJavaModelManager().closeZipFile(jar);
            }
        }
        if (reader != null) {
            return reader.getVersion();
        }
    } catch (CoreException e) {
    // ignore
    } catch (ClassFormatException e) {
    // ignore
    } catch (IOException e) {
    // ignore
    }
    return 0;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) Enumeration(java.util.Enumeration) IPath(org.eclipse.core.runtime.IPath) IClassFileReader(org.eclipse.jdt.core.util.IClassFileReader) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ClassFormatException(org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException) ZipFile(java.util.zip.ZipFile) CoreException(org.eclipse.core.runtime.CoreException) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.jdt.internal.core.ClassFile) ZipFile(java.util.zip.ZipFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 14 with ZipFile

use of java.util.zip.ZipFile in project che by eclipse.

the class InstallExtension method getExtensionFromFile.

private static Extension getExtensionFromFile(Path zipPath) throws IOException {
    try (ZipFile zipFile = new ZipFile(zipPath.toString())) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        ZipEntry gwtXmlEntry = null;
        ZipEntry pomEntry = null;
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                if (entry.getName().endsWith(GwtXmlUtils.GWT_MODULE_XML_SUFFIX)) {
                    gwtXmlEntry = entry;
                } else if (entry.getName().endsWith("pom.xml")) {
                    pomEntry = entry;
                }
            }
            // have both entries
            if (pomEntry != null && gwtXmlEntry != null) {
                break;
            }
        }
        // TODO consider Codenvy extension validator
        if (pomEntry == null) {
            return null;
        }
        String gwtModuleName = null;
        if (gwtXmlEntry != null) {
            gwtModuleName = gwtXmlEntry.getName().replace(File.separatorChar, '.');
            gwtModuleName = gwtModuleName.substring(0, gwtModuleName.length() - GwtXmlUtils.GWT_MODULE_XML_SUFFIX.length());
        }
        Model pom = Model.readFrom(zipFile.getInputStream(pomEntry));
        return new Extension(gwtModuleName, MavenUtils.getGroupId(pom), pom.getArtifactId(), MavenUtils.getVersion(pom));
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) Model(org.eclipse.che.ide.maven.tools.Model)

Example 15 with ZipFile

use of java.util.zip.ZipFile in project che by eclipse.

the class ClasspathJar method readPackages.

private SimpleSet readPackages() {
    try {
        if (this.zipFile == null) {
            if (org.eclipse.jdt.internal.core.JavaModelManager.ZIP_ACCESS_VERBOSE) {
                System.out.println("(" + Thread.currentThread() + ") [ClasspathJar.isPackage(String)] Creating ZipFile on " + this.zipFilename);
            //$NON-NLS-1$	//$NON-NLS-2$
            }
            this.zipFile = new ZipFile(this.zipFilename);
            this.closeZipFileAtEnd = true;
        }
        return findPackageSet(this);
    } catch (Exception e) {
        // assume for this build the zipFile is empty
        return new SimpleSet();
    }
}
Also used : SimpleSet(org.eclipse.jdt.internal.compiler.util.SimpleSet) ZipFile(java.util.zip.ZipFile) ClassFormatException(org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException)

Aggregations

ZipFile (java.util.zip.ZipFile)580 ZipEntry (java.util.zip.ZipEntry)414 File (java.io.File)261 IOException (java.io.IOException)189 InputStream (java.io.InputStream)127 FileOutputStream (java.io.FileOutputStream)98 ZipOutputStream (java.util.zip.ZipOutputStream)89 Test (org.junit.Test)88 FileInputStream (java.io.FileInputStream)57 ArrayList (java.util.ArrayList)44 Enumeration (java.util.Enumeration)42 BufferedInputStream (java.io.BufferedInputStream)38 BufferedOutputStream (java.io.BufferedOutputStream)35 ZipException (java.util.zip.ZipException)32 ClassReader (org.objectweb.asm.ClassReader)29 OutputStream (java.io.OutputStream)27 JarFile (java.util.jar.JarFile)26 ZipInputStream (java.util.zip.ZipInputStream)26 FileNotFoundException (java.io.FileNotFoundException)23 Path (java.nio.file.Path)23