Search in sources :

Example 21 with ZipEntry

use of java.util.zip.ZipEntry in project cogtool by cogtool.

the class ZipUtil method zipDirectory.

private static void zipDirectory(File dir, String base, ZipOutputStream zout) throws IOException {
    // list all
    File[] files = dir.listFiles();
    base += dir.getName() + "/";
    if (files.length > 0) {
        // if there are files in this directory, add them (this dir is implicit)
        for (File file : files) {
            if (file.isDirectory()) {
                // recur on directories
                zipDirectory(file, base, zout);
            } else {
                // add files to the stream
                zipOneFile(file, base, zout);
            }
        }
    } else {
        // we have to add this dir explicitly as an entry
        // Build proper entry name
        String name = base + dir.getName() + '/';
        // Create a ZipEntry
        ZipEntry entry = new ZipEntry(name);
        entry.setTime(dir.lastModified());
        // Put the entry into the zip (actually, this just writes the header)
        zout.putNextEntry(entry);
        // Close off the entry (actually, this just writes more header info)
        zout.closeEntry();
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 22 with ZipEntry

use of java.util.zip.ZipEntry 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 23 with ZipEntry

use of java.util.zip.ZipEntry 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 24 with ZipEntry

use of java.util.zip.ZipEntry 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 25 with ZipEntry

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

the class ZipFileStructureProvider method createContainer.

/**
     * Creates a new container zip entry with the specified name, iff
     * it has not already been created.
     */
protected void createContainer(IPath pathname) {
    if (directoryEntryCache.containsKey(pathname)) {
        return;
    }
    ZipEntry parent;
    if (pathname.segmentCount() == 1) {
        parent = root;
    } else {
        parent = (ZipEntry) directoryEntryCache.get(pathname.removeLastSegments(1));
    }
    ZipEntry newEntry = new ZipEntry(pathname.toString());
    directoryEntryCache.put(pathname, newEntry);
    addToChildren(parent, newEntry);
}
Also used : ZipEntry(java.util.zip.ZipEntry)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1367 ZipFile (java.util.zip.ZipFile)479 File (java.io.File)469 IOException (java.io.IOException)361 ZipOutputStream (java.util.zip.ZipOutputStream)321 ZipInputStream (java.util.zip.ZipInputStream)300 InputStream (java.io.InputStream)282 FileOutputStream (java.io.FileOutputStream)278 FileInputStream (java.io.FileInputStream)270 Test (org.junit.Test)124 BufferedInputStream (java.io.BufferedInputStream)122 JarFile (java.util.jar.JarFile)122 BufferedOutputStream (java.io.BufferedOutputStream)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)97 ArrayList (java.util.ArrayList)84 ByteArrayInputStream (java.io.ByteArrayInputStream)78 OutputStream (java.io.OutputStream)67 JarOutputStream (java.util.jar.JarOutputStream)59 Path (java.nio.file.Path)56 Enumeration (java.util.Enumeration)56