Last active
November 3, 2020 12:31
-
-
Save Vaikesh/471eb223d0a5ee37944a to your computer and use it in GitHub Desktop.
Sample Class to Decompress (Unzip) files in Xamarin.Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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