• c# 创建压缩包并下载文件


     1 public class IndexController: ICSharpCode.SharpZipLib.Core.INameTransform
     2 {
     3 
     4 #region INameTransform 成员
     5 
     6 public string TransformDirectory(string name)
     7 {
     8 return null;
     9 }
    10 
    11 public string TransformFile(string name)
    12 {
    13 return Path.GetFileName(name);
    14 }
    15 
    16 #endregion
    17 }
    18 
    19 /// <summary>
    20 /// 压缩打包文件
    21 /// </summary>
    22 public ActionResult ZipFileByCode()
    23 {
    24 MemoryStream ms = new MemoryStream();
    25 byte[] buffer = null;
    26 
    27 using (ZipFile file = ZipFile.Create(ms))
    28 {
    29 file.BeginUpdate();
    30 file.NameTransform = new IndexController();//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。
    31 
    32 file.Add(Server.MapPath("/Content/images/img01.jpg"));
    33 file.Add(Server.MapPath("/Content/images/img02.jpg"));
    34 file.CommitUpdate();
    35 
    36 buffer = new byte[ms.Length];
    37 ms.Position = 0;
    38 ms.Read(buffer, 0, buffer.Length);
    39 }
    40 Response.AddHeader("content-disposition", "attachment;filename=test.zip");
    41 Response.BinaryWrite(buffer);
    42 Response.Flush();
    43 Response.End();
    44 
    45 return new EmptyResult();
    46 }
    47 
    48 }

    所需Dll下载:http://files.cnblogs.com/wangbogo/ICSharpCode.SharpZipLib.rar 

  • 相关阅读:
    NSOperationQueue
    iOS开发网络数据之AFNetworking使用
    NSURLConnection
    SQLite3 嵌入式数据库
    iOS中常用的四种数据持久化方法简介
    <转> core Animation动画-2
    core Animation动画
    ios数据库操作SQLite
    SQLite3-各个函数
    SQLite3-数据类型
  • 原文地址:https://www.cnblogs.com/jixingsuiyuan/p/7999984.html
Copyright © 2020-2023  润新知