11

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.

Esteban Verbel
  • 738
  • 2
  • 20
  • 39
StezPet
  • 2,430
  • 2
  • 27
  • 49
  • Microsoft has a PCL package (Microsoft.Bcl.Compression) that implements `System.IO.Compression` and enables ZipArchive, GZipStream and DeflateStream classes in pure CIL-based code not as fast as using platform native code, but if you are not doing this a lot... https://www.nuget.org/packages/Microsoft.Bcl.Compression/3.9.85 – SushiHangover Feb 08 '17 at 16:42
  • https://forums.xamarin.com/discussion/2113/unzip-a-file-always-failing – ClintL Feb 08 '17 at 19:49
  • Check out https://github.com/escamoteur/TBInfrastructure – Thomas Jul 18 '17 at 09:18

2 Answers2

5

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;
    }
Sparsha Bhattarai
  • 693
  • 11
  • 20
3

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.

Marc_Alx
  • 1,285
  • 17
  • 25