Search in sources :

Example 61 with ZipFile

use of java.util.zip.ZipFile in project druid by druid-io.

the class CompressionUtils method unzip.

/**
   * Unzip the pulled file to an output directory. This is only expected to work on zips with lone files, and is not intended for zips with directory structures.
   *
   * @param pulledFile The file to unzip
   * @param outDir     The directory to store the contents of the file.
   *
   * @return a FileCopyResult of the files which were written to disk
   *
   * @throws IOException
   */
public static FileUtils.FileCopyResult unzip(final File pulledFile, final File outDir) throws IOException {
    if (!(outDir.exists() && outDir.isDirectory())) {
        throw new ISE("outDir[%s] must exist and be a directory", outDir);
    }
    log.info("Unzipping file[%s] to [%s]", pulledFile, outDir);
    final FileUtils.FileCopyResult result = new FileUtils.FileCopyResult();
    try (final ZipFile zipFile = new ZipFile(pulledFile)) {
        final Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
        while (enumeration.hasMoreElements()) {
            final ZipEntry entry = enumeration.nextElement();
            result.addFiles(FileUtils.retryCopy(new ByteSource() {

                @Override
                public InputStream openStream() throws IOException {
                    return new BufferedInputStream(zipFile.getInputStream(entry));
                }
            }, new File(outDir, entry.getName()), FileUtils.IS_EXCEPTION, DEFAULT_RETRY_COUNT).getFiles());
        }
    }
    return result;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) ByteSource(com.google.common.io.ByteSource) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 62 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 63 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)

Example 64 with ZipFile

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

the class JavaNavigation method getContent.

public ClassContent getContent(IJavaProject project, int rootId, String path) throws CoreException {
    IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
    if (root == null) {
        return null;
    }
    if (path.startsWith("/")) {
        //non java file
        if (root instanceof JarPackageFragmentRoot) {
            JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) root;
            ZipFile jar = null;
            try {
                jar = jarPackageFragmentRoot.getJar();
                ZipEntry entry = jar.getEntry(path.substring(1));
                if (entry != null) {
                    try (InputStream stream = jar.getInputStream(entry)) {
                        return createContent(IoUtil.readStream(stream), false);
                    } catch (IOException e) {
                        LOG.error("Can't read file content: " + entry.getName(), e);
                    }
                }
            } finally {
                if (jar != null) {
                    JavaModelManager.getJavaModelManager().closeZipFile(jar);
                }
            }
        }
        Object[] resources = root.getNonJavaResources();
        for (Object resource : resources) {
            if (resource instanceof JarEntryFile) {
                JarEntryFile file = (JarEntryFile) resource;
                if (file.getFullPath().toOSString().equals(path)) {
                    return readFileContent(file);
                }
            }
            if (resource instanceof JarEntryDirectory) {
                JarEntryDirectory directory = (JarEntryDirectory) resource;
                JarEntryFile file = findJarFile(directory, path);
                if (file != null) {
                    return readFileContent(file);
                }
            }
        }
    } else {
        return getContent(project, path);
    }
    return null;
}
Also used : JarEntryDirectory(org.eclipse.jdt.internal.core.JarEntryDirectory) ZipFile(java.util.zip.ZipFile) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) JarEntryFile(org.eclipse.jdt.internal.core.JarEntryFile) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 65 with ZipFile

use of java.util.zip.ZipFile in project buck by facebook.

the class JarDirectoryStepHelper method createManifest.

private static Manifest createManifest(ProjectFilesystem filesystem, ImmutableSortedSet<Path> entriesToJar, Optional<String> mainClass, Optional<Path> manifestFile, boolean mergeManifests) throws IOException {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    if (mergeManifests) {
        for (Path entry : entriesToJar) {
            entry = filesystem.getPathForRelativePath(entry);
            Manifest readManifest;
            if (Files.isDirectory(entry)) {
                Path manifestPath = entry.resolve(JarFile.MANIFEST_NAME);
                if (!Files.exists(manifestPath)) {
                    continue;
                }
                try (InputStream inputStream = Files.newInputStream(manifestPath)) {
                    readManifest = new Manifest(inputStream);
                }
            } else {
                // Assume a zip or jar file.
                try (ZipFile zipFile = new ZipFile(entry.toFile())) {
                    ZipEntry manifestEntry = zipFile.getEntry(JarFile.MANIFEST_NAME);
                    if (manifestEntry == null) {
                        continue;
                    }
                    try (InputStream inputStream = zipFile.getInputStream(manifestEntry)) {
                        readManifest = new Manifest(inputStream);
                    }
                }
            }
            merge(manifest, readManifest);
        }
    }
    // so that values from the user overwrite values from merged manifests.
    if (manifestFile.isPresent()) {
        Path path = filesystem.getPathForRelativePath(manifestFile.get());
        try (InputStream stream = Files.newInputStream(path)) {
            Manifest readManifest = new Manifest(stream);
            merge(manifest, readManifest);
        }
    }
    // We may have merged manifests and over-written the user-supplied main class. Add it back.
    if (mainClass.isPresent()) {
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass.get());
    }
    return manifest;
}
Also used : Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Manifest(java.util.jar.Manifest)

Aggregations

ZipFile (java.util.zip.ZipFile)637 ZipEntry (java.util.zip.ZipEntry)454 File (java.io.File)287 IOException (java.io.IOException)214 InputStream (java.io.InputStream)147 FileOutputStream (java.io.FileOutputStream)108 ZipOutputStream (java.util.zip.ZipOutputStream)92 Test (org.junit.Test)89 FileInputStream (java.io.FileInputStream)68 Enumeration (java.util.Enumeration)47 ArrayList (java.util.ArrayList)46 BufferedInputStream (java.io.BufferedInputStream)44 BufferedOutputStream (java.io.BufferedOutputStream)39 ZipInputStream (java.util.zip.ZipInputStream)35 ZipException (java.util.zip.ZipException)34 OutputStream (java.io.OutputStream)31 ClassReader (org.objectweb.asm.ClassReader)29 FileNotFoundException (java.io.FileNotFoundException)26 JarFile (java.util.jar.JarFile)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24