I want to create a zip file in the Xamarin Forms Cross Platform. I use an individual approach for each platform, iOS and Android. On iOS, it works with the ZipArchive library, but I did not find an alternative for Android.
So I try to do this native (to create a zip with only one file), but the zip file was created empty.
public void Compress(string path, string filename, string zipname)
{
var personalpath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string folder = Path.Combine(personalpath, path);
string zippath = Path.Combine(folder, zipname);
string filepath = Path.Combine(folder, filename);
System.IO.FileStream fos = new System.IO.FileStream(zippath, FileMode.OpenOrCreate);
Java.Util.Zip.ZipOutputStream zos = new Java.Util.Zip.ZipOutputStream(fos);
ZipEntry entry = new ZipEntry(filename.Substring(filename.LastIndexOf("/") + 1));
byte[] fileContents = File.ReadAllBytes(filepath);
zos.Write(fileContents);
zos.CloseEntry();
}
source
share