星空网 > 软件开发 > ASP.net

使用插件压缩文件并提供下载

直接上网上拿来的方法:

    /// <summary>    /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略)    /// </summary>    /// <param name="dirPath">被压缩的文件夹夹路径</param>    /// <param name="zipFilePath">生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip</param>    /// <param name="err">出错信息</param>    /// <returns>是否压缩成功</returns>    public static bool ZipFile(string dirPath, string zipFilePath, out string err)    {      //如果服务器存在同名文件,则删除      if (File.Exists(zipFilePath))      {        try        {          File.Delete(zipFilePath);        }        catch { }      }      err = "";      if (dirPath == string.Empty)      {        err = "要压缩的文件夹不能为空!";        return false;      }      if (!Directory.Exists(dirPath))      {        err = "要压缩的文件夹不存在!";        return false;      }      //压缩文件名为空时使用文件夹名+.zip      if (zipFilePath == string.Empty)      {        if (dirPath.EndsWith("\\"))        {          dirPath = dirPath.Substring(0, dirPath.Length - 1);        }        zipFilePath = dirPath + ".zip";      }      try      {        string[] filenames = Directory.GetFiles(dirPath);        using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))        {          s.SetLevel(9);          byte[] buffer = new byte[4096];          foreach (string file in filenames)          {            ZipEntry entry = new ZipEntry(Path.GetFileName(file));            entry.DateTime = DateTime.Now;            s.PutNextEntry(entry);            using (FileStream fs = File.OpenRead(file))            {              int sourceBytes;              do              {                sourceBytes = fs.Read(buffer, 0, buffer.Length);                s.Write(buffer, 0, sourceBytes);              } while (sourceBytes > 0);            }          }          s.Finish();          s.Close();        }      }      catch (Exception ex)      {        err = ex.Message;        return false;      }      return true;    }

直接上打包下载代码

        string temp_path = HttpContext.Current.Request.MapPath(".\\query\\");        String path = temp_path + DateTime.Now.ToString("yyyyMMddHHmmss");        string newPath = path + "\\";        Directory.CreateDirectory(path);        for (int i = 0; i < ds.Tables.Count; i++)        {          DataToCSV.SaveCSV(ds.Tables[i], newPath);        }        String result = String.Empty;        Boolean flag = DataToCSV.ZipFile(newPath, temp_path + "MesData.zip", out result);        if (flag)        {          //删除生成文件夹          Directory.Delete(path, true);          //提供下载          FileInfo fileInfo = new FileInfo(temp_path + "MesData.zip");          Response.Clear();          Response.ClearContent();          Response.ClearHeaders();          Response.AddHeader("Content-Disposition", "attachment;filename=MesData.zip");          Response.AddHeader("Content-Length", fileInfo.Length.ToString());          Response.AddHeader("Content-Transfer-Encoding", "binary");          Response.ContentType = "application/octet-stream";          Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");          Response.WriteFile(fileInfo.FullName);          Response.Flush();          HttpContext.Current.ApplicationInstance.CompleteRequest();          //删除副本//File.Delete(filePath);          Response.Close();        }        else        {          //提示信息        }

 




原标题:使用插件压缩文件并提供下载

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

官方辟谣:疯传的亚马逊沟通政策是假的:https://www.ikjzd.com/articles/111280
Shopee子母账号常见问题解析!:https://www.ikjzd.com/articles/111281
火力全开!Shopee推出专业KOL代理服务抢夺东南亚市场!:https://www.ikjzd.com/articles/111282
亚马逊开店?亚马逊各站点特点为您来介绍!:https://www.ikjzd.com/articles/111283
亚马逊旺季运营流量如何提升?:https://www.ikjzd.com/articles/111286
惊呆!阿里控制的这些海外电商平台,你知道几个?:https://www.ikjzd.com/articles/111287
怪物在游轮上复活的电影 怪物在游轮上复活的电影叫什么:https://www.vstour.cn/a/411230.html
在线旅游如何选择更优惠的旅游产品?:https://www.vstour.cn/a/411231.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流