ICSharpCode.SharpZipLib.Zip

 

//压缩整个目录下载

var projectFolder = Request.Params["folder"] != null ? Request.Params["folder"] : string.Empty;
if (!string.IsNullOrWhiteSpace(projectFolder))
{
string path = Util.GetMapPath(string.Format("attarch/html/{0}", projectFolder));
Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}.zip\"", projectFolder));
Response.BufferOutput = false;

var buffer = new byte[1024 * 1024];
using (var zipOutputStream = new ZipOutputStream(Response.OutputStream, 1024 * 1024))
{
zipOutputStream.SetLevel(0);
var directoryInfo = new DirectoryInfo(path);
foreach (string file in Directory.GetFiles(directoryInfo.FullName, "*.*", SearchOption.AllDirectories))
{
string folder = Path.GetDirectoryName(file);
if (folder.Length > directoryInfo.FullName.Length)
{
folder = folder.Substring(directoryInfo.FullName.Length).Trim('\\') + @"\";
}
else
{
folder = string.Empty;
}
zipOutputStream.PutNextEntry(new ZipEntry(folder + Path.GetFileName(file)));
using (var fs = System.IO.File.OpenRead(file))
{
ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(fs, zipOutputStream, buffer);
}
zipOutputStream.Flush();
Response.Flush();
}
zipOutputStream.Finish();
}
Response.Flush();
}
else
{
HandlerMessage("没有下载的数据!");
}

posted @ 2013-07-24 09:23  火腿骑士  阅读(364)  评论(0编辑  收藏  举报