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;
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations