Created
March 13, 2015 14:43
-
-
Save mgonzales3/215cce075db853b44285 to your computer and use it in GitHub Desktop.
ZipArchive in Folder
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
/* | |
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