StezPet
StezPet

Reputation: 2450

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.

Upvotes: 11

Views: 5893

Answers (2)

Marc_Alx
Marc_Alx

Reputation: 1441

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.

Upvotes: 3

Sparsha Bhattarai
Sparsha Bhattarai

Reputation: 703

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;
    }

Upvotes: 5

Related Questions