Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to UnZip downloaded Zip file in Xamarin forms

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.

like image 957
StezPet Avatar asked Feb 08 '17 16:02

StezPet


2 Answers

There's a builtin compression system in .NET System.IO.Compression.

It's really simple to use it :

using System.IO.Compression;
ZipFile.ExtractToDirectory(zipPath, extractPath);

I've tested it in Xamarin.Forms 4.7 and it works without problems.

ZipFile.ExtractToDirectory(
    Path.Combine(Xamarin.Essentials.FileSystem.CacheDirectory,"archive.zip"),//zipPath 
    Xamarin.Essentials.FileSystem.AppDataDirectory//extractPath
);

In this sample I use Xamarin.Essentials to get common directories, but it's not mandatory.

like image 61
Marc_Alx Avatar answered Oct 13 '22 08:10

Marc_Alx


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;
    }
like image 5
Sparsha Bhattarai Avatar answered Oct 13 '22 10:10

Sparsha Bhattarai