Skip to content

Instantly share code, notes, and snippets.

@Vaikesh
Last active November 3, 2020 12:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Vaikesh/471eb223d0a5ee37944a to your computer and use it in GitHub Desktop.
Save Vaikesh/471eb223d0a5ee37944a to your computer and use it in GitHub Desktop.
Sample Class to Decompress (Unzip) files in Xamarin.Android.
using System;
using Android.Util;
using Java.IO;
using Java.Util.Zip;
namespace ZipManager
{
public class Decompress
{
String _zipFile;
String _location;
public Decompress(String zipFile, String location)
{
_zipFile = zipFile;
_location = location;
DirChecker("");
}
void DirChecker(String dir)
{
var file = new File(_location + dir);
if(!file.IsDirectory) {
file.Mkdirs ();
}
}
public void UnZip()
{
try
{
var fileInputStream = new FileInputStream(_zipFile);
var zipInputStream = new ZipInputStream(fileInputStream);
ZipEntry zipEntry = null;
while((zipEntry = zipInputStream.NextEntry)!=null)
{
Log.Verbose("Decompress", "UnZipping : "=zipEntry.Name);
if(zipEntry.IsDirectory)
{
DirChecker(zipEntry.Name);
}
else
{
var fileOutputStream = new FileOutputStream(_location + zipEntry.Name);
for (int i = zipInputStream.Read(); i != -1; i = zipInputStream.Read())
{
fileOutputStream.Write(i);
}
zipInputStream.CloseEntry();
fileOutputStream.Close();
}
}
zipInputStream.Close();
}
catch (Exception ex)
{
Log.Error ("Decompress", "UnZip", ex);
}
}
}
}
/// <summary>
///
/// Sample shows unzipping 'ZipFile.zip'
/// and store in '/sdcard/unzip/'.
///
/// </summary>
String zipFile = Android.OS.Environment.ExternalStorageDirectory+"/ZipFile.zip";
String unZipLocation = Android.OS.Environment.ExternalStorageDirectory+"/unzip/";
Decompress decompress = new Decompress (zipFile, unZipLocation);
decompress.UnZip ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment