Skip to content

Instantly share code, notes, and snippets.

@mgonzales3
Created March 13, 2015 14:43
Show Gist options
  • Save mgonzales3/215cce075db853b44285 to your computer and use it in GitHub Desktop.
Save mgonzales3/215cce075db853b44285 to your computer and use it in GitHub Desktop.
ZipArchive in Folder
/*
Script for zipping a list of xml files
*/
private void CreateZipPackage()
{
String zipDir = AppDomain.CurrentDomain.BaseDirectory + @"zips\";
DirectoryInfo di = new DirectoryInfo(zipDir);
foreach (var file in di.GetFiles("*.xml"))
{
Compress(di, file, zipDir);
}
}
private void Compress(DirectoryInfo directorySelected, FileInfo fileName, string directoryPath)
{
using (FileStream zipToOpen = new FileStream(directoryPath + DateTime.Now.ToString("yyyyMMddhhmmss") + ".zip", FileMode.OpenOrCreate))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry = archive.CreateEntry(fileName.Name);
using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
{
writer.Write(GetData(fileName.FullName));
}
}
}
}
private string GetData(string fileName)
{
StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
using (XmlWriter xw = XmlWriter.Create(sb, xws))
{
XDocument doc = XDocument.Load(fileName);
doc.WriteTo(xw);
}
return sb.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment