C# ZIPFile - JackHu88/Comm GitHub Wiki
http://dotnetzip.codeplex.com/
string folder= @"C:\Test;
DirectoryInfo di = new DirectoryInfo(folder);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
StringBuilder builder = new StringBuilder();
string fileName = "test" + ".csv";
string filePath = Path.Combine(folder, fileName);
builder.AppendLine("head,EntityCode,DEBIT,DOUBLEMSGCHAR");
using (var w = new StreamWriter(filePath))
{
w.WriteLine(builder.ToString());
}
using (ZipFile zip = new ZipFile())
{
string zipName = String.Format("zip_{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
//zip.AlternateEncodingUsage = ZipOption.AsNecessary;
//zip.AddDirectoryByName(zipName);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string[] filePaths = Directory.GetFiles(folder);
foreach (string filePath in filePaths)
{
//string FileName = Path.GetFileName(filePath);
//zip.AddFile(filePath, zipName);
zip.AddFile(filePath, @"\");
}
});
using (MemoryStream memoryStream = new MemoryStream())
{
zip.Save(memoryStream);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + zipName + ".zip");
HttpContext.Current.Response.AddHeader("Content-Length", memoryStream.ToArray().LongLength.ToString());
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
}