Created
January 30, 2016 07:52
-
-
Save Vaikesh/7856d4010619bd85f57c to your computer and use it in GitHub Desktop.
Sample Class to compress (zip) 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 System.Diagnostics; | |
using Android.Util; | |
using Java.IO; | |
using Java.Util.Zip; | |
namespace ZipManager | |
{ | |
public class Compress | |
{ | |
static int Buffer = 2048; | |
String[] _files; | |
String _zipFiles; | |
public Compress (String[] files, String zipFiles) | |
{ | |
_files = files; | |
_zipFiles = zipFiles; | |
} | |
public void Zip() | |
{ | |
try | |
{ | |
var fileOutputStream = new FileOutputStream(_zipFiles); | |
var zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream)); | |
var data = new Byte[Buffer]; | |
for (int i = 0; i < _files.Length; i++) | |
{ | |
Log.Verbose("Compress","Adding : "+_files[i]); | |
var fileInputStream = new FileInputStream(_files[i]); | |
var bufferedInputStream = new BufferedInputStream(fileInputStream,Buffer); | |
var zipEntry = new ZipEntry(_files[i].Substring(_files[i].LastIndexOf("/")+1)); | |
zipOutputStream.PutNextEntry(zipEntry); | |
int count; | |
while((count=bufferedInputStream.Read(data,0,Buffer))!=-1) | |
{ | |
zipOutputStream.Write(data,0,count); | |
} | |
bufferedInputStream.Close(); | |
} | |
zipOutputStream.Close(); | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine (ex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment