How to unzip a zip file in Xamarin format

In my application, I get a ZIP file with 4 PDF documents during a call to My API. I save the zip file using the code below.

var rootFolder = FileSystem.Current.LocalStorage; var file = await rootFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); using (var fileHandler = await file.OpenAsync(FileAccess.ReadAndWrite)) { await fileHandler.WriteAsync(document, 0, document.Length); } 

After saving the document

How can I unzip and individually save PDF documents to my phone? Can anyone contact me to solve this problem. I found SharpZipLib and Iconic zip libraries for unpacking code; But only a point network implementation, if found in the documentation, does not know how to integrate it into Xamarin Forms.

Please, help.

+7
source share
1 answer

You can use SharpZipLib to unzip the downloaded file. I added a function to unzip the file in a user-defined place below.

  private async Task<bool> UnzipFileAsync(string zipFilePath, string unzipFolderPath) { try { var entry = new ZipEntry(Path.GetFileNameWithoutExtension(zipFilePath)); var fileStreamIn = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read); var zipInStream = new ZipInputStream(fileStreamIn); entry = zipInStream.GetNextEntry(); while (entry != null && entry.CanDecompress) { var outputFile = unzipFolderPath + @"/" + entry.Name; var outputDirectory = Path.GetDirectoryName(outputFile); if (!Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } if (entry.IsFile) { var fileStreamOut = new FileStream(outputFile, FileMode.Create, FileAccess.Write); int size; byte[] buffer = new byte[4096]; do { size = await zipInStream.ReadAsync(buffer, 0, buffer.Length); await fileStreamOut.WriteAsync(buffer, 0, size); } while (size > 0); fileStreamOut.Close(); } entry = zipInStream.GetNextEntry(); } zipInStream.Close(); fileStreamIn.Close(); } catch { return false; } return true; } 
0
source

Source: https://habr.com/ru/post/1014807/


All Articles