Skip to content

Instantly share code, notes, and snippets.

@Vaikesh
Created January 30, 2016 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vaikesh/7856d4010619bd85f57c to your computer and use it in GitHub Desktop.
Save Vaikesh/7856d4010619bd85f57c to your computer and use it in GitHub Desktop.
Sample Class to compress (zip) files in Xamarin.Android
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