压缩文件的使用

首先引入dll文件ICSharpCode.SharpZipLib.dll 管理NuGet包里面下载

压缩文件

 1 /// <summary>
 2 /// 压缩文件
 3 /// </summary>
 4 /// <param name="fileName">要压缩的所有文件(完全路径)</param>
 5 /// <param name="fileName">文件名称</param>
 6 /// <param name="name">压缩后文件路径</param>
 7 /// <param name="Level">压缩级别</param>
 8 public void ZipFileMain(string[] filenames, string[] fileName, string name, int Level)
 9 {
10     ZipOutputStream s = new ZipOutputStream(File.Create(name));
11     Crc32 crc = new Crc32();
12     //压缩级别
13     s.SetLevel(Level); // 0 - store only to 9 - means best compression
14     try
15     {
16         int m = 0;
17         foreach (string file in filenames)
18         {
19             //打开压缩文件
20             FileStream fs = File.OpenRead(file);//文件地址
21             byte[] buffer = new byte[fs.Length];
22             fs.Read(buffer, 0, buffer.Length);
23             //建立压缩实体
24             ZipEntry entry = new ZipEntry(fileName[m].ToString());//原文件名
25             //时间
26             entry.DateTime = DateTime.Now;
27             //空间大小
28             entry.Size = fs.Length;
29             fs.Close();
30             crc.Reset();
31             crc.Update(buffer);
32             entry.Crc = crc.Value;
33             s.PutNextEntry(entry);
34             s.Write(buffer, 0, buffer.Length);
35             m++;
36         }
37     }
38     catch
39     {
40         throw;
41     }
42     finally
43     {
44         s.Finish();
45         s.Close();
46     }
47 }

 

文件下载

 

 1 //下载打包文件
 2     private void DownloadFile(string fileName, string filePath)
 3     {
 4         FileInfo fileInfo = new FileInfo(filePath);
 5         Response.Clear();
 6         Response.ClearContent();
 7         Response.ClearHeaders();
 8         Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
 9         Response.AddHeader("Content-Length", fileInfo.Length.ToString());
10         Response.AddHeader("Content-Transfer-Encoding", "binary");
11         Response.ContentType = "application/octet-stream";
12         Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
13         Response.WriteFile(fileInfo.FullName);
14         Response.Flush();
15         File.Delete(filePath);//删除已下载文件
16         Response.End();
17     }

 

具体使用

 1 protected void BtnDownloadFiles_Click(object sender, EventArgs e)
 2     {
 3         List<string> listFJ = new List<string>();//保存附件路径
 4         List<string> listFJName = new List<string>();//保存附件名字
 5         for (int i = 0; i < gridView.Rows.Count; i++)
 6         {
 7             HtmlInputCheckBox chk = (Page.Master.FindControl("ContentPlaceHolder1").FindControl("gridView") as GridView).Rows[i].FindControl("checkboxname") as HtmlInputCheckBox;
 8             if (chk != null && chk.Checked)
 9             {
10                 string temp = gridView.Rows[i].Cells[14].Text.ToString().Trim();
11                 if (temp != " ")
12                 {
13                     listFJ.Add(Server.MapPath("~") + temp);
14                     listFJName.Add(temp);
15                 }
16             }
17         }
18         string time = DateTime.Now.Ticks.ToString();
19         ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("~/uploadfiles/" + time + ".zip"), 9);//压缩文件
20         DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("~/uploadfiles/" + time + ".zip"));//下载文件
21     }

 

posted @ 2019-04-25 09:14  聖潔  阅读(469)  评论(0编辑  收藏  举报