Search in sources :

Example 96 with ZipFile

use of java.util.zip.ZipFile in project jadx by skylot.

the class InputFile method loadFromZip.

private boolean loadFromZip(String ext) throws IOException, DecodeException {
    ZipFile zf = new ZipFile(file);
    int index = 0;
    while (true) {
        String entryName = "classes" + (index == 0 ? "" : index) + ext;
        ZipEntry entry = zf.getEntry(entryName);
        if (entry == null) {
            break;
        }
        InputStream inputStream = zf.getInputStream(entry);
        try {
            if (ext.equals(".dex")) {
                addDexFile(entryName, new Dex(inputStream));
            } else if (ext.equals(".jar")) {
                File jarFile = FileUtils.createTempFile(entryName);
                FileOutputStream fos = new FileOutputStream(jarFile);
                try {
                    IOUtils.copy(inputStream, fos);
                } finally {
                    close(fos);
                }
                addDexFile(entryName, loadFromJar(jarFile));
            } else {
                throw new JadxRuntimeException("Unexpected extension in zip: " + ext);
            }
        } finally {
            close(inputStream);
        }
        index++;
        if (index == 1) {
            index = 2;
        }
    }
    zf.close();
    return index > 0;
}
Also used : ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) Dex(com.android.dex.Dex) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 97 with ZipFile

use of java.util.zip.ZipFile in project spring-loaded by spring-projects.

the class ReloadableFileChangeListener method register.

public void register(ReloadableType rtype, File file) {
    if (file.getName().endsWith(".jar")) {
        // Compute the last mod time of the entry in the jar
        try {
            ZipFile zf = new ZipFile(file);
            String slashname = rtype.getSlashedName() + ".class";
            ZipEntry ze = zf.getEntry(slashname);
            //LastModifiedTime().toMillis();
            long lmt = ze.getTime();
            JarEntry je = new JarEntry(rtype, slashname, lmt);
            zf.close();
            Set<JarEntry> jarEntries = watchedJarContents.get(file);
            if (jarEntries == null) {
                jarEntries = new HashSet<JarEntry>();
                watchedJarContents.put(file, jarEntries);
            }
            jarEntries.add(je);
            if (GlobalConfiguration.isRuntimeLogging && log.isLoggable(Level.INFO)) {
                log.info(" watching jar file entry. Jar=" + file + "  file=" + rtype.getSlashedName() + " lmt=" + lmt);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        correspondingReloadableTypes.put(file, rtype);
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 98 with ZipFile

use of java.util.zip.ZipFile in project spring-loaded by spring-projects.

the class SuperLoader method findClass.

public Class<?> findClass(String name) throws ClassNotFoundException {
    if (SubLoader.DEBUG_LOADING) {
        System.out.println("> SuperLoader: findClass(" + name + ")");
    }
    Class<?> c = null;
    // Look in the filesystem first
    try {
        for (int i = 0; i < folders.length; i++) {
            File f = new File(folders[i], name.replace('.', '/') + ".class");
            if (f.exists()) {
                byte[] data = Utils.loadBytesFromStream(new FileInputStream(f));
                TypeRegistry tr = TypeRegistry.getTypeRegistryFor(this);
                if (tr != null) {
                    // not yet doing this - the testcase tends to do any client side rewriting for this
                    ReloadableType rtype = tr.addType(name, data);
                    data = rtype.bytesLoaded;
                }
                if (SubLoader.DEBUG_LOADING) {
                    System.out.println("  SuperLoader: found in folder: '" + folders[i] + "'");
                }
                c = defineClass(name, data, 0, data.length);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (c == null) {
        // Try the jar
        try {
            for (int i = 0; i < jars.length; i++) {
                // System.out.println("Checking jar for "+name);
                ZipFile zipfile = new ZipFile(jars[i]);
                String slashedClassName = name.replace('.', '/');
                ZipEntry zipentry = zipfile.getEntry(slashedClassName + ".class");
                if (zipentry != null) {
                    byte[] data = Utils.loadBytesFromStream(zipfile.getInputStream(zipentry));
                    TypeRegistry tr = TypeRegistry.getTypeRegistryFor(this);
                    //						data = new SpringLoadedPreProcessor().preProcess(this, slashedClassName, null, data);
                    if (tr != null) {
                        // Give the plugins a chance to rewrite stuff too
                        for (org.springsource.loaded.Plugin plugin : SpringLoadedPreProcessor.getGlobalPlugins()) {
                            if (plugin instanceof LoadtimeInstrumentationPlugin) {
                                LoadtimeInstrumentationPlugin loadtimeInstrumentationPlugin = (LoadtimeInstrumentationPlugin) plugin;
                                if (loadtimeInstrumentationPlugin.accept(slashedClassName, this, null, data)) {
                                    data = loadtimeInstrumentationPlugin.modify(slashedClassName, this, data);
                                }
                            }
                        }
                        //System.out.println("Transforming " + name);
                        data = tr.methodCallRewrite(data);
                    }
                    if (SubLoader.DEBUG_LOADING) {
                        System.out.println("  SuperLoader: found in zip: '" + jars[i] + "'");
                    }
                    c = defineClass(name, data, 0, data.length);
                    break;
                }
                zipfile.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (c == null) {
        throw new ClassNotFoundException(name);
    }
    return c;
}
Also used : LoadtimeInstrumentationPlugin(org.springsource.loaded.LoadtimeInstrumentationPlugin) ZipEntry(java.util.zip.ZipEntry) TypeRegistry(org.springsource.loaded.TypeRegistry) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ReloadableType(org.springsource.loaded.ReloadableType) ZipFile(java.util.zip.ZipFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 99 with ZipFile

use of java.util.zip.ZipFile in project spring-loaded by spring-projects.

the class TestClassloaderWithRewriting method findClass.

public Class<?> findClass(String name) throws ClassNotFoundException {
    Class<?> c = null;
    // Look in the filesystem first
    try {
        for (int i = 0; i < folders.length; i++) {
            File f = new File(folders[i], name.replace('.', '/') + ".class");
            if (f.exists()) {
                byte[] data = Utils.loadBytesFromStream(new FileInputStream(f));
                TypeRegistry tr = TypeRegistry.getTypeRegistryFor(this);
                if (tr != null) {
                    if (useRegistry) {
                        ReloadableType rt = tr.addType(name, data);
                        if (rt == null) {
                            System.out.println("Not made reloadable " + name);
                        } else {
                            return rt.getClazz();
                        }
                    }
                // not yet doing this - the testcase tends to do any client side rewriting for this
                }
                c = defineClass(name, data, 0, data.length);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Exception ex = null;
    if (c == null) {
        // Try the jar
        try {
            for (int i = 0; i < jars.length; i++) {
                // System.out.println("Checking jar for "+name);
                ZipFile zipfile = new ZipFile(jars[i]);
                String slashedClassName = name.replace('.', '/');
                ZipEntry zipentry = zipfile.getEntry(slashedClassName + ".class");
                if (zipentry != null) {
                    byte[] data = Utils.loadBytesFromStream(zipfile.getInputStream(zipentry));
                    TypeRegistry tr = TypeRegistry.getTypeRegistryFor(this);
                    if (tr != null) {
                        // Give the plugins a chance to rewrite stuff too
                        for (org.springsource.loaded.Plugin plugin : SpringLoadedPreProcessor.getGlobalPlugins()) {
                            if (plugin instanceof LoadtimeInstrumentationPlugin) {
                                LoadtimeInstrumentationPlugin loadtimeInstrumentationPlugin = (LoadtimeInstrumentationPlugin) plugin;
                                if (loadtimeInstrumentationPlugin.accept(slashedClassName, this, null, data)) {
                                    data = loadtimeInstrumentationPlugin.modify(slashedClassName, this, data);
                                }
                            }
                        }
                        // TODO make conditional?
                        if (slashedClassName.equals("net/sf/cglib/core/ReflectUtils")) {
                            // intercept call to defineclass so we can make it reloadable.  In practice this isn't necessary
                            // as the springloadedpreprocessor will get called
                            data = RewriteReflectUtilsDefineClass.rewriteReflectUtilsDefineClass(data);
                        }
                        //System.out.println("Transforming " + name);
                        data = tr.methodCallRewrite(data);
                    }
                    c = defineClass(name, data, 0, data.length);
                    break;
                }
            }
        // zipfile.close();
        } catch (Exception e) {
            ex = e;
            e.printStackTrace(System.err);
        }
    }
    if (c == null) {
        if (ex != null) {
            throw new ClassNotFoundException(name, ex);
        } else {
            throw new ClassNotFoundException(name);
        }
    }
    return c;
}
Also used : LoadtimeInstrumentationPlugin(org.springsource.loaded.LoadtimeInstrumentationPlugin) ZipEntry(java.util.zip.ZipEntry) TypeRegistry(org.springsource.loaded.TypeRegistry) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ReloadableType(org.springsource.loaded.ReloadableType) ZipFile(java.util.zip.ZipFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 100 with ZipFile

use of java.util.zip.ZipFile in project platform_frameworks_base by android.

the class PackageHelper method extractPublicFiles.

/**
     * Extract public files for the single given APK.
     */
public static long extractPublicFiles(File apkFile, File publicZipFile) throws IOException {
    final FileOutputStream fstr;
    final ZipOutputStream publicZipOutStream;
    if (publicZipFile == null) {
        fstr = null;
        publicZipOutStream = null;
    } else {
        fstr = new FileOutputStream(publicZipFile);
        publicZipOutStream = new ZipOutputStream(fstr);
        Log.d(TAG, "Extracting " + apkFile + " to " + publicZipFile);
    }
    long size = 0L;
    try {
        final ZipFile privateZip = new ZipFile(apkFile.getAbsolutePath());
        try {
            // Copy manifest, resources.arsc and res directory to public zip
            for (final ZipEntry zipEntry : Collections.list(privateZip.entries())) {
                final String zipEntryName = zipEntry.getName();
                if ("AndroidManifest.xml".equals(zipEntryName) || "resources.arsc".equals(zipEntryName) || zipEntryName.startsWith("res/")) {
                    size += zipEntry.getSize();
                    if (publicZipFile != null) {
                        copyZipEntry(zipEntry, privateZip, publicZipOutStream);
                    }
                }
            }
        } finally {
            try {
                privateZip.close();
            } catch (IOException e) {
            }
        }
        if (publicZipFile != null) {
            publicZipOutStream.finish();
            publicZipOutStream.flush();
            FileUtils.sync(fstr);
            publicZipOutStream.close();
            FileUtils.setPermissions(publicZipFile.getAbsolutePath(), FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP | FileUtils.S_IROTH, -1, -1);
        }
    } finally {
        IoUtils.closeQuietly(publicZipOutStream);
    }
    return size;
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) 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