From 984f8118d6f39b68c2bdb576871291a0cbb680db Mon Sep 17 00:00:00 2001 From: Appcelerator Build Date: Fri, 20 Nov 2020 22:17:42 -0500 Subject: [PATCH] fix(android): java deployFromZip() and deployFromAssets() (#12283) - Regression caused by TIMOB-28246 change. - Nobody uses these methods. Co-authored-by: Joshua Quick --- .../titanium/util/TiFileHelper.java | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/android/titanium/src/java/org/appcelerator/titanium/util/TiFileHelper.java b/android/titanium/src/java/org/appcelerator/titanium/util/TiFileHelper.java index 0e65e81a6f1..f0eabe70bd4 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/util/TiFileHelper.java +++ b/android/titanium/src/java/org/appcelerator/titanium/util/TiFileHelper.java @@ -406,8 +406,8 @@ public void deployFromAssets(File dest) throws IOException AssetManager am = ctx.getAssets(); walkAssets(am, "", paths); - // TODO clean old dir - deleteTree(dest); + // Delete all files and subdirectories under given directory. + emptyDirectory(dest); // copy from assets to dest dir BufferedInputStream bis = null; @@ -462,7 +462,7 @@ public void deployFromAssets(File dest) throws IOException public void deployFromZip(File fname, File dest) throws IOException { - deleteTree(dest); + emptyDirectory(dest); ZipInputStream zis = null; ZipEntry ze = null; @@ -573,6 +573,40 @@ public boolean deleteTree(File file) throws SecurityException return (wasDeleted && file.delete()); } + /** + * Deletes all files and subdirectories under the given directory while leaving the given directory intact. + * If given directory does not exist, then it is created. + * @param directory The directory to be emptied. Can be null. + * @return + * Returns true if successfully deleted all files and folders under given directory. + * Returns false if at least 1 deletion failed or if given a null argument. + * @exception SecurityException Thrown if don't have permission to delete at least 1 file in the tree. + */ + public boolean emptyDirectory(File directory) throws SecurityException + { + // Validate argument. + if (directory == null) { + return false; + } + + // If directory does not exist, then create it and stop here. + if (!directory.exists()) { + return directory.mkdirs(); + } + + // Do not continue if referencing a file instead of a directory. + if (!directory.isDirectory()) { + return false; + } + + // Delete all file and subdirectories under given directory. + boolean wasSuccessful = true; + for (File nextFile : directory.listFiles()) { + wasSuccessful = deleteTree(nextFile) && wasSuccessful; + } + return wasSuccessful; + } + public File getTempFile(String suffix, boolean destroyOnExit) throws IOException { TiApplication tiApp = TiApplication.getInstance();