zoukankan      html  css  js  c++  java
  • 压缩文件的使用

    首先引入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     }
  • 相关阅读:
    三种数据解析方式
    requests模块相关用法
    urllib模块基本用法
    django复习题
    scrapy框架编写向redis数据库中存储数据的相关代码时报错解决办法
    并发编程练习题
    网络编程 socket 开发练习题
    面向对象编程设计练习题(2)
    pytest-fixtured
    Python 删除某一目录下的所有文件或文件夹
  • 原文地址:https://www.cnblogs.com/loushengjie/p/10766351.html
Copyright © 2011-2022 走看看