Skip to content

Instantly share code, notes, and snippets.

@jens1101
Last active July 4, 2016 11:31
Show Gist options
  • Save jens1101/56c45be53619fc033b268bcbe3ec177e to your computer and use it in GitHub Desktop.
Save jens1101/56c45be53619fc033b268bcbe3ec177e to your computer and use it in GitHub Desktop.
Adding files to ZIP in C#
function saveFileAsZip(string filePath) {
using (ZipFile zip = new ZipFile())
{
//Add string
zip.AddEntry("dataset.json", "{ foo: 'bar' }");
//Add file as byte array
zip.AddEntry("customName.json", System.IO.File.ReadAllBytes(filePath));
//Add file, preserving path
zip.AddFile(filePath);
//Add file to zip root
zip.AddFile(filePath, "");
//Add file to folder in zip
zip.AddFile(filePath, "/folder/bar");
return zip;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment