In my application I am getting ZIP file with 4 pdf documents during My API call. I am saving the ZIP file using the below code.
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 I can unzip and individually save the pdf documents into phones memory. Can anyone please direct me to solve this issue. I found SharpZipLib & Iconic zip libraries for unzipping the code; But only dot net implementation if found in the documentation, don't know how to integrate this in Xamarin Forms.
Please help.
You can use SharpZipLib to unzip the downloaded file. I've added a function to unzip the file to a user-defined location 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;
}