public void unzip(string srcFilePath, string destPath, int type) { using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { // DEBUG here to copy file from dll to isostore ... // this is only really needed if you want to test with a file in your package/project StreamResourceInfo fileResourceStreamInfo = Application.GetResourceStream(new Uri(srcFilePath, UriKind.Relative)); if (fileResourceStreamInfo != null) { using (BinaryReader br = new BinaryReader(fileResourceStreamInfo.Stream)) { byte[] data = br.ReadBytes((int)fileResourceStreamInfo.Stream.Length); // This will truncate/overwrite an existing file, or using (IsolatedStorageFileStream outFile = appStorage.OpenFile(srcFilePath, FileMode.Create)) { using (var writer = new BinaryWriter(outFile)) { writer.Write(data); } } } } if(type == Replace) { DeleteDirectoryRecursively(appStorage, destPath); } IsolatedStorageFileStream zipStream = null; ZipArchive zipArch = null; try { zipStream = new IsolatedStorageFileStream(srcFilePath, FileMode.Open, FileAccess.Read, appStorage); } catch (Exception) { Debug.WriteLine("File not found :: " + srcFilePath); return; } if (zipStream != null) { zipArch = new ZipArchive(zipStream); } if (zipArch != null) { int totalFiles = zipArch.FileNames.Count(); int current = 0; try { foreach (string filename in zipArch.FileNames) { string destFilePath = destPath + "/" + filename; string directoryName = getDirectoryName(destFilePath); //Debug.WriteLine("upacking file : " + filename + " to : " + destFilePath); if (!appStorage.DirectoryExists(directoryName)) { appStorage.CreateDirectory(directoryName); } using (Stream readStream = zipArch.GetFileStream(filename)) { if (readStream != null) { if(type == Merge) { // only write file if it doesn't exist already if (!appStorage.FileExists(destFilePath)) { using (FileStream outStream = new IsolatedStorageFileStream(destFilePath, FileMode.OpenOrCreate, FileAccess.Write, appStorage)) { WriteStreamToPath(readStream, outStream); } } FileUnzipProgress progEvt = new FileUnzipProgress(totalFiles, current++); } else { using (FileStream outStream = new IsolatedStorageFileStream(destFilePath, FileMode.OpenOrCreate, FileAccess.Write, appStorage)) { WriteStreamToPath(readStream, outStream); FileUnzipProgress progEvt = new FileUnzipProgress(totalFiles, current++); } } } } } zipStream.Close(); } catch (Exception) { Debug.WriteLine("File not found :: " + srcFilePath); } } } }
public void unzip(string srcFilePath, string destPath, int type) { using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { // DEBUG here to copy file from dll to isostore ... // this is only really needed if you want to test with a file in your package/project StreamResourceInfo fileResourceStreamInfo = Application.GetResourceStream(new Uri(srcFilePath, UriKind.Relative)); if (fileResourceStreamInfo != null) { using (BinaryReader br = new BinaryReader(fileResourceStreamInfo.Stream)) { byte[] data = br.ReadBytes((int)fileResourceStreamInfo.Stream.Length); // This will truncate/overwrite an existing file, or using (IsolatedStorageFileStream outFile = appStorage.OpenFile(srcFilePath, FileMode.Create)) { using (var writer = new BinaryWriter(outFile)) { writer.Write(data); } } } } if (type == Replace) { DeleteDirectoryRecursively(appStorage, destPath); } IsolatedStorageFileStream zipStream = null; ZipArchive zipArch = null; try { zipStream = new IsolatedStorageFileStream(srcFilePath, FileMode.Open, FileAccess.Read, appStorage); } catch (Exception) { Debug.WriteLine("File not found :: " + srcFilePath); return; } if (zipStream != null) { zipArch = new ZipArchive(zipStream); } if (zipArch != null) { int totalFiles = zipArch.FileNames.Count(); int current = 0; try { foreach (string filename in zipArch.FileNames) { string destFilePath = destPath + "/" + filename; string directoryName = getDirectoryName(destFilePath); //Debug.WriteLine("upacking file : " + filename + " to : " + destFilePath); if (!appStorage.DirectoryExists(directoryName)) { appStorage.CreateDirectory(directoryName); } using (Stream readStream = zipArch.GetFileStream(filename)) { if (readStream != null) { if (type == Merge) { using (FileStream outStream = new IsolatedStorageFileStream(destFilePath, FileMode.OpenOrCreate, FileAccess.Write, appStorage)) { WriteStreamToPath(readStream, outStream); } FileUnzipProgress progEvt = new FileUnzipProgress(totalFiles, current++); } else { using (FileStream outStream = new IsolatedStorageFileStream(destFilePath, FileMode.OpenOrCreate, FileAccess.Write, appStorage)) { WriteStreamToPath(readStream, outStream); FileUnzipProgress progEvt = new FileUnzipProgress(totalFiles, current++); } } } } } zipStream.Close(); } catch (Exception) { Debug.WriteLine("File not found :: " + srcFilePath); } } } }
public void unzip(string options) { string[] optionStrings; string srcFilePath; string destPath = ""; string callbackId = CurrentCommandCallbackId; try { optionStrings = JSON.JsonHelper.Deserialize <string[]>(options); srcFilePath = optionStrings[0]; destPath = optionStrings[1]; callbackId = optionStrings[2]; } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId); return; } using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { // DEBUG here to copy file from dll to isostore ... // this is only really needed if you want to test with a file in your package/project StreamResourceInfo fileResourceStreamInfo = Application.GetResourceStream(new Uri(srcFilePath, UriKind.Relative)); if (fileResourceStreamInfo != null) { using (BinaryReader br = new BinaryReader(fileResourceStreamInfo.Stream)) { byte[] data = br.ReadBytes((int)fileResourceStreamInfo.Stream.Length); // This will truncate/overwrite an existing file, or using (IsolatedStorageFileStream outFile = appStorage.OpenFile(srcFilePath, FileMode.Create)) { using (var writer = new BinaryWriter(outFile)) { writer.Write(data); } } } } IsolatedStorageFileStream zipStream = null; ZipArchive zipArch = null; try { zipStream = new IsolatedStorageFileStream(srcFilePath, FileMode.Open, FileAccess.Read, appStorage); } catch (Exception) { Debug.WriteLine("File not found :: " + srcFilePath); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR), callbackId); return; } if (zipStream != null) { zipArch = new ZipArchive(zipStream); } if (zipArch != null) { int totalFiles = zipArch.FileNames.Count(); int current = 0; try { foreach (string filename in zipArch.FileNames) { string destFilePath = destPath + "/" + filename; string directoryName = getDirectoryName(destFilePath); //Debug.WriteLine("upacking file : " + filename + " to : " + destFilePath); if (!appStorage.DirectoryExists(directoryName)) { appStorage.CreateDirectory(directoryName); } using (Stream readStream = zipArch.GetFileStream(filename)) { if (readStream != null) { using (FileStream outStream = new IsolatedStorageFileStream(destFilePath, FileMode.OpenOrCreate, FileAccess.Write, appStorage)) { WriteStreamToPath(readStream, outStream); FileUnzipProgress progEvt = new FileUnzipProgress(totalFiles, current++); PluginResult plugRes = new PluginResult(PluginResult.Status.OK, progEvt); plugRes.KeepCallback = true; plugRes.CallbackId = callbackId; DispatchCommandResult(plugRes, callbackId); } } } } DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId); } catch (Exception) { Debug.WriteLine("File not found :: " + srcFilePath); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR), callbackId); } } } }