Search in sources :

Example 16 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 17 with ZipFile

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

the class DexJarAnalysisStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) throws InterruptedException {
    try (ZipFile zf = new ZipFile(filesystem.resolve(dexPath).toFile())) {
        ZipEntry classesDexEntry = zf.getEntry("classes.dex");
        if (classesDexEntry == null) {
            throw new RuntimeException("could not find classes.dex in jar");
        }
        long uncompressedSize = classesDexEntry.getSize();
        if (uncompressedSize == -1) {
            throw new RuntimeException("classes.dex size should be known");
        }
        filesystem.writeContentsToPath(String.format("jar:%s dex:%s", filesystem.getFileSize(dexPath), uncompressedSize), dexMetaPath);
        return StepExecutionResult.SUCCESS;
    } catch (IOException e) {
        context.logError(e, "There was an error in smart dexing step.");
        return StepExecutionResult.ERROR;
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 18 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)

Example 19 with ZipFile

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

the class AgentUtil method getJarSignature.

public static String getJarSignature(String packagePath) throws IOException {
    Pattern signatureFilePattern = Pattern.compile("META-INF/[A-Z]+\\.SF");
    ZipFile packageZip = null;
    try {
        packageZip = new ZipFile(packagePath);
        // For each file in the zip.
        for (ZipEntry entry : Collections.list(packageZip.entries())) {
            // Ignore non-signature files.
            if (!signatureFilePattern.matcher(entry.getName()).matches()) {
                continue;
            }
            BufferedReader sigContents = null;
            try {
                sigContents = new BufferedReader(new InputStreamReader(packageZip.getInputStream(entry)));
                // For each line in the signature file.
                while (true) {
                    String line = sigContents.readLine();
                    if (line == null || line.equals("")) {
                        throw new IllegalArgumentException("Failed to find manifest digest in " + entry.getName());
                    }
                    String prefix = "SHA1-Digest-Manifest: ";
                    if (line.startsWith(prefix)) {
                        return line.substring(prefix.length());
                    }
                }
            } finally {
                if (sigContents != null) {
                    sigContents.close();
                }
            }
        }
    } finally {
        if (packageZip != null) {
            packageZip.close();
        }
    }
    throw new IllegalArgumentException("Failed to find signature file.");
}
Also used : Pattern(java.util.regex.Pattern) ZipFile(java.util.zip.ZipFile) InputStreamReader(java.io.InputStreamReader) ZipEntry(java.util.zip.ZipEntry) BufferedReader(java.io.BufferedReader)

Example 20 with ZipFile

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

the class DxAnalysisMain method loadAllClasses.

private static ImmutableMap<String, ClassNode> loadAllClasses(String zipFileName) throws IOException {
    ImmutableMap.Builder<String, ClassNode> allClassesBuilder = ImmutableMap.builder();
    try (ZipFile inJar = new ZipFile(zipFileName)) {
        for (ZipEntry entry : Collections.list(inJar.entries())) {
            if (!entry.getName().endsWith(".class")) {
                continue;
            }
            // Skip external libraries.
            if (entry.getName().startsWith("junit/") || entry.getName().startsWith("org/junit/") || entry.getName().startsWith("com/google/common/")) {
                continue;
            }
            byte[] rawClass = ByteStreams.toByteArray(inJar.getInputStream(entry));
            ClassNode klass = new ClassNode();
            new ClassReader(rawClass).accept(klass, ClassReader.EXPAND_FRAMES);
            allClassesBuilder.put(klass.name, klass);
        }
    }
    return allClassesBuilder.build();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ClassReader(org.objectweb.asm.ClassReader) ImmutableMap(com.google.common.collect.ImmutableMap)

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