From f3582b60dbe029d21ee3376703ba58606b31a4ff Mon Sep 17 00:00:00 2001 From: Rover12421 Date: Wed, 18 Mar 2015 09:15:26 +0800 Subject: [PATCH] buildUnknownFiles optimization newFileSystem will file replication overhead --- .../src/main/java/brut/androlib/Androlib.java | 60 ++++--------------- 1 file changed, 13 insertions(+), 47 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java index d380e093..e742c8a0 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java @@ -544,7 +544,6 @@ public class Androlib { public void buildUnknownFiles(File appDir, File outFile, Map meta) throws AndrolibException { - File file; Path globalPath = Paths.get(appDir.getPath() + File.separatorChar + UNK_DIRNAME); if (meta.containsKey("unknownFiles")) { @@ -561,16 +560,20 @@ public class Androlib { // create filesystem Path path = Paths.get(outFile.getAbsolutePath()); - // loop through files inside - for (Map.Entry entry : files.entrySet()) { + try( + FileSystem fs = FileSystems.newFileSystem(path, null) + ) { + // loop through files inside + for (Map.Entry entry : files.entrySet()) { - file = new File(globalPath.toFile(), entry.getKey()); - if (file.isFile() && file.exists()) { - insertFolder(path, zip_properties, file.getParentFile(), entry.getValue(), - globalPath.toAbsolutePath()); - - insertFile(path, zip_properties, file, entry.getValue(), - globalPath.toAbsolutePath()); + File file = new File(globalPath.toFile(), entry.getKey()); + Path dest = fs.getPath(entry.getKey()); + Path destParent = dest.getParent(); + if (destParent != null && !Files.exists(destParent)) { + Files.createDirectories(destParent); + } + Path newFile = Paths.get(file.getAbsolutePath()); + Files.copy(newFile, dest, StandardCopyOption.REPLACE_EXISTING); } } } catch (IOException ex) { @@ -579,43 +582,6 @@ public class Androlib { } } - private void insertFile(Path apkPath, Map zip_properties, File insert, String method, Path location) - throws AndrolibException, IOException { - // ZipFileSystem only writes at .close() - // http://mail.openjdk.java.net/pipermail/nio-dev/2012-July/001764.html - try( - FileSystem fs = FileSystems.newFileSystem(apkPath, null) - ) { - Path root = fs.getPath("/"); - - // in order to get the path relative to the zip, we strip off the absolute path, minus what we - // already have in the zip. thus /var/files/apktool/apk/unknown/folder/file => /folder/file - Path dest = fs.getPath(root.toString(), insert.getAbsolutePath().replace(location.toString(),"")); - Path newFile = Paths.get(insert.getAbsolutePath()); - Files.copy(newFile,dest, StandardCopyOption.REPLACE_EXISTING); - fs.close(); - } - } - - private void insertFolder(Path apkPath, Map zip_properties, File insert, String method, Path location) - throws AndrolibException, IOException { - try( - FileSystem fs = FileSystems.newFileSystem(apkPath, null) - ) { - Path root = fs.getPath("/"); - Path dest = fs.getPath(root.toString(), insert.getAbsolutePath().replace(location.toString(),"")); - Path parent = dest.normalize(); - - // check for folder existing in apkFileSystem - if (parent != null && Files.notExists(parent)) { - if (!Files.isDirectory(parent, LinkOption.NOFOLLOW_LINKS)) { - Files.createDirectories(parent); - } - } - fs.close(); - } - } - public void buildApk(File appDir, File outApk) throws AndrolibException { LOGGER.info("Building apk file..."); if (outApk.exists()) {