首先:

引用 ICSharpCode.SharpZipLib.dll,百度下载

然后引用命名空间:

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

我自己的代码:

                      //取数据dt   
 
                      string path = Server.MapPath("~\\tempzt\\");
                      if (dt.Rows.Count > 0)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string wjmc = "xxxxxxxxxxxx".txt"; for (int n = 0; n < dt.Rows.Count; n++)
{
for (int m = 0; m < dt.Columns.Count; m++)
{
sb.Append(dt.Rows[n][m].ToString()); sb.Append("\t");
}
sb.Append("\r\n");
}
txt_export(sb, wjmc, path);
}
}
}
//zip下载
string[] files = Directory.GetFiles(path);
if (files.Length > 0)
{
string name = "xxxxxxxxxxxx.zip";
ZipFileMain(files, path + name, 9);
DownLoadZip(path + name, name);
}
else
{
Response.Write("<script>alert('没有目标文件!请先写入')</script>");
}
public void txt_export(StringBuilder sb, string wjmc, string path)
{
//zip下载
string txtPath = path + wjmc;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
} if (File.Exists(txtPath))
{
File.Delete(txtPath);
} StreamWriter sw = new StreamWriter(txtPath, false, System.Text.Encoding.Default);
sw.Write(sb.ToString());
sw.Close();
//zip下载
} //zip下载
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileName">要压缩的所有文件(完全路径)</param>
/// <param name="name">压缩后文件路径</param>
/// <param name="Level">压缩级别</param>
public void ZipFileMain(string[] filenames, string name, int Level)
{
if (File.Exists(name))
{
File.Delete(name);
}
ZipOutputStream s = new ZipOutputStream(File.Create(name));
Crc32 crc = new Crc32();
//压缩级别
s.SetLevel(Level); // 0 - store only to 9 - means best compression
try
{
foreach (string file in filenames)
{
if (!Path.GetExtension(file).Equals(".txt"))
{
continue;
}
//打开压缩文件
FileStream fs = File.OpenRead(file);//文件地址
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
//建立压缩实体
ZipEntry entry = new ZipEntry(Path.GetFileName(file));//原文件名
//时间
entry.DateTime = DateTime.Now;
//空间大小
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
File.Delete(file);
}
}
catch
{
throw;
}
finally
{
s.Finish();
s.Close();
}
} public void DownLoadZip(string fileName,string name)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + name + "");
Response.TransmitFile(fileName);
}

最新文章

  1. Sqrtx
  2. vim 快捷键 以及技巧
  3. [LeetCode] Implement strStr()
  4. Sprint第三个冲刺(第二天)
  5. Calculation控制台
  6. Apache 性能优化
  7. .NET基础之迭代器
  8. angularjs跨域调取webservice
  9. DoNet开源项目-基于Amaze UI的点餐系统
  10. DOM基础(一)
  11. 2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017)
  12. java最小公倍数与最大公约数
  13. Nginx 测试环境配置,留作笔记使用
  14. 如何解决“504 Gateway Time-out”错误
  15. 『流畅的Python』第9章笔记_对象
  16. vue-cli项目配置文件分析
  17. Tomcat学习总结(15)—— Tomcat优化时的参数分析
  18. VS 类快捷键
  19. 轻量级ORM框架Dapper应用六:Dapper支持存储过程
  20. log4j和logback

热门文章

  1. git 使用那些事儿
  2. 算法训练 Balloons in a Box (枚举,模拟)
  3. soj 131 找题
  4. SQLAlchemy外键的使用
  5. Java泛型&lt;&gt;内各种参数的异同
  6. python 字符串,bytes和hex字符串之间的相互转换
  7. SP16549 QTREE6 - Query on a tree VI LCT维护颜色联通块
  8. 换根DP+树的直径【洛谷P3761】 [TJOI2017]城市
  9. HDU1253 胜利大逃亡 (BFS)
  10. 使用between and 作为查询条件