custom theme: fix libziparchive loading of custom themes

Change-Id: Ia23a9dcd24fcbb61cb5e1df366a4325d20d777b2
diff --git a/gui/pages.cpp b/gui/pages.cpp
index 3145c15..bc4a936 100755
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -1225,15 +1225,13 @@
 	} else {
 		LOGINFO("PageManager::LoadFileToBuffer loading filename: '%s' from zip\n", filename.c_str());
 		ZipEntry binary_entry;
-		if (FindEntry(package, filename, &binary_entry) == 0) {
-		// if (!package->EntryExists(filename)) {
+		if (FindEntry(package, filename, &binary_entry) != 0) {
 			LOGERR("Unable to locate '%s' in zip file\n", filename.c_str());
 			return NULL;
 		}
 
 		// Allocate the buffer for the file
 		len = binary_entry.uncompressed_length;
-		// len = package->GetUncompressedSize(filename);
 		buffer = (char*) malloc(len + 1);
 		if (!buffer)
 			return NULL;
@@ -1241,7 +1239,6 @@
 		int32_t err =
 			ExtractToMemory(package, &binary_entry, reinterpret_cast<uint8_t*>(buffer), len);
 		if (err != 0) {
-		// if (!package->ExtractToBuffer(filename, (unsigned char*) buffer)) {
 			LOGERR("Unable to extract '%s'\n", filename.c_str());
 			free(buffer);
 			return NULL;
@@ -1311,7 +1308,7 @@
 		TWFunc::removeDir(TWRES "customlanguages", true);
 	if (package) {
 		TWFunc::Recursive_Mkdir(TWRES "customlanguages");
-		ExtractPackageRecursive(package, "", TWRES "customlanguages", nullptr, nullptr);
+		ExtractPackageRecursive(package, "/", TWRES "customlanguages", nullptr, nullptr);
 
 		// package->ExtractRecursive("languages", TWRES "customlanguages/");
 		LoadLanguageListDir(TWRES "customlanguages/");
@@ -1374,15 +1371,16 @@
 		tw_y_offset = 0;
 		tw_w_offset = 0;
 		tw_h_offset = 0;
-		if (!TWFunc::Path_Exists(package))
+		if (!TWFunc::Path_Exists(package)) {
 			return -1;
+		}
 
 		ZipArchiveHandle Zip;
 		int err = OpenArchive(package.c_str(), &Zip);
 
 		if (err != 0)
 			return -1;
-		
+
 		ctx.zip = Zip;
 		mainxmlfilename = "ui.xml";
 		LoadLanguageList(ctx.zip);
diff --git a/gui/resources.cpp b/gui/resources.cpp
index cf677f6..744a853 100755
--- a/gui/resources.cpp
+++ b/gui/resources.cpp
@@ -58,13 +58,12 @@
 
 	std::string src = folderName + "/" + fileName + fileExtn;
 	ZipEntry binary_entry;
-	if (FindEntry(pZip, src, &binary_entry) != 0) {
+	if (FindEntry(pZip, src, &binary_entry) == 0) {
 		android::base::unique_fd fd(
 			open(destFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0666));
 		if (fd == -1) {
 			return -1;
 		}
-		// if (!pZip->ExtractEntry(src, destFile, 0666))
 		int32_t err = ExtractEntryToFile(pZip, &binary_entry, fd);
 		if (err != 0)
 			return -1;
diff --git a/install/ZipUtil.cpp b/install/ZipUtil.cpp
index c7b6b3d..75acad9 100755
--- a/install/ZipUtil.cpp
+++ b/install/ZipUtil.cpp
@@ -54,9 +54,10 @@
     if (!zip_path.empty() && zip_path.back() != '/') {
         prefix_path += '/';
     }
-    const std::string zip_prefix(prefix_path.c_str());
 
+    const std::string zip_prefix(prefix_path.c_str());
     int ret = StartIteration(zip, &cookie, zip_prefix, nullptr);
+
     if (ret != 0) {
         LOG(ERROR) << "failed to start iterating zip entries.";
         return false;
diff --git a/twrp.cpp b/twrp.cpp
index 96446b2..5109914 100644
--- a/twrp.cpp
+++ b/twrp.cpp
@@ -85,15 +85,10 @@
 				DataManager::SetValue("tw_crypto_user_id", "0");
 			if (gui_startPage("decrypt", 1, 1) != 0) {
 				LOGERR("Failed to start decrypt GUI page.\n");
-			} else {
-				// Check for and load custom theme if present
-				TWFunc::check_selinux_support();
-				gui_loadCustomResources();
 			}
 		}
 	} else if (datamedia) {
 		PartitionManager.Update_System_Details();
-		TWFunc::check_selinux_support();
 		if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
 			LOGINFO("Failed to get default contexts and file mode for storage files.\n");
 		} else {
@@ -221,6 +216,9 @@
 #endif
 
 	Decrypt_Page(skip_decryption, datamedia);
+	// Check for and load custom theme if present
+	TWFunc::check_selinux_support();
+	gui_loadCustomResources();
 	PartitionManager.Output_Partition_Logging();
 
 	// Fixup the RTC clock on devices which require it