private string GetFromApkToFile(string apkFileName, string getFilename, string destinationFile = null) { string tempFile; if (destinationFile != null) { tempFile = destinationFile; } else { tempFile = Java.IO.File.CreateTempFile(getFilename, "", _context.ExternalCacheDir).AbsolutePath; } using (var apk = new ZipFileProvider(apkFileName, FileCacheMode.None, false, QuestomAssets.Utils.FileUtils.GetTempDirectory())) { using (var fs = File.Open(tempFile, FileMode.Create, FileAccess.ReadWrite)) { using (var readStream = apk.GetReadStream(getFilename, true)) readStream.CopyTo(fs); } } return(tempFile); }
private void ExtractAssetsFromApkToExternalStorage(string apkFilename, List <string> excludePaths = null, bool fromBakFiles = false) { UpdateStatus("Extracting assets files from the APK to external storage..."); using (var apk = new ZipFileProvider(apkFilename, FileCacheMode.None, false, QuestomAssets.Utils.FileUtils.GetTempDirectory())) { foreach (var assetFilename in apk.FindFiles(APK_ASSETS_PATH + "*")) { string relativeFilename = assetFilename.Substring(APK_ASSETS_PATH.Length); if (excludePaths != null) { if (excludePaths.Any(x => relativeFilename.StartsWith(x))) { Log.LogMsg($"The asset file {assetFilename} ({relativeFilename}) is not included in assets that should be extracted, skipping."); continue; } } Log.LogMsg($"Extracting {assetFilename}..."); string targetFile = Path.Combine(Constants.ASSETS_RELOC_PATH, relativeFilename); string dirName = Path.GetDirectoryName(targetFile); try { if (!Directory.Exists(dirName)) { Log.LogMsg($"Assets target directory doesn't exist, creating {dirName}"); Directory.CreateDirectory(dirName); } } catch (Exception ex) { Log.LogErr($"Unable to create directory {dirName}!", ex); UpdateStatus("Failed to create assets directory in external storage!"); throw new ModException($"Unable to create directory {dirName}!", ex); } try { using (var readStream = apk.GetReadStream(assetFilename, true)) { if (targetFile.EndsWith(".bobak")) { targetFile = targetFile.Substring(0, targetFile.Length - 6); } using (var fs = File.Open(targetFile, FileMode.Create, FileAccess.Write)) { readStream.CopyTo(fs); } } if (!assetFilename.EndsWith(".bobak")) { apk.Rename(assetFilename, assetFilename + ".bobak"); } } catch (Exception ex) { Log.LogErr($"Failed to extract {assetFilename} to {targetFile}", ex); UpdateStatus("Failed extracting an asset from the APK to external storage!"); throw new ModException($"Failed to extract {assetFilename} to {targetFile}", ex); } } apk.Save(); } }