zoukankan      html  css  js  c++  java
  • C# DotNetZip压缩单、多文件以及文件夹

    有些项目为了更好的用户体验,会把下载文件做成一个压缩的文件,直接下载,免得去一个个的点击下载文件。网上有很多压缩文件的方法,也有第三方的分装DLL文件,本文主要介绍DotNetZip压缩方法。

    DotNetZip的DLl下载地址:http://download.csdn.net/detail/lilinoscar/8295255

    官网下载地址:http://dotnetzip.codeplex.com/ 

    解决DotNetZip压缩中文名称乱码,只需要在实例化时设置编码:System.Text.Encoding.Default

    即:ZipFile zip = new ZipFile(System.Text.Encoding.Default)。

    解决DotNetZip压缩后的文件有多层目录:zip.AddFile(file,"");  

    AddFile加上第二个参数即可去掉多层的文件夹。

    [csharp] view plain copy
     
    1.   #region bool SaveFile(string filePath, byte[] bytes) 文件保存,  
    2.   /// <summary>  
    3.   ///  文件保存,特别是有些文件放到数据库,可以直接从数据取二进制,然后保存到指定文件夹  
    4.   /// </summary>  
    5.   /// <param name="filePath">保存文件地址</param>  
    6.   /// <param name="bytes">文件二进制</param>  
    7.   /// <returns></returns>  
    8.   public static bool SaveFile(string filePath, byte[] bytes)  
    9.   {  
    10.       bool result = true;  
    11.       try  
    12.       {  
    13.           using (var fileStream = new FileStream(filePath, FileMode.Create))  
    14.           {  
    15.               fileStream.Write(bytes, 0, bytes.Length);  
    16.           }  
    17.       }  
    18.       catch (Exception)  
    19.       {  
    20.           result = false;  
    21.       }  
    22.       return result;  
    23.   }  
    24.   #endregion  
    25.  
    26.   #region 判断文件夹是否存在  
    27.   /// <summary>  
    28.   /// 判断文件夹是否存在  
    29.   /// </summary>  
    30.   /// <param name="path">文件夹地址</param>  
    31.   /// <returns></returns>  
    32.   public static bool directoryExist(string path)  
    33.   {  
    34.       if (!string.IsNullOrEmpty(path) && Directory.Exists(path))  
    35.       {  
    36.           return true;  
    37.       }  
    38.       return false;  
    39.   }   
    40.   #endregion  
    41.  
    42.   #region 创建文件夹  
    43.   /// <summary>  
    44.   /// 创建文件夹  
    45.   /// </summary>  
    46.   /// <param name="path">文件地址</param>  
    47.   /// <returns></returns>  
    48.   public static bool directoryAdd(string path)  
    49.   {  
    50.       if (!string.IsNullOrEmpty(path) && !Directory.Exists(path))  
    51.       {  
    52.           Directory.CreateDirectory(path); //新建文件夹    
    53.           return true;  
    54.       }  
    55.       return false;  
    56.   }   
    57.   #endregion  
    58.  
    59.   #region 获取压缩后的文件路径  
    60.   /// <summary>  
    61.   /// 获取压缩后的文件路径  
    62.   /// </summary>  
    63.   /// <param name="dirPath">压缩的文件路径</param>  
    64.   /// <param name="filesPath">多个文件路径</param>  
    65.   /// <returns></returns>  
    66.   public static string GetCompressPath(string dirPath, List<string> filesPath)  
    67.   {  
    68.       var zipPath = "";//返回压缩后的文件路径  
    69.       using (ZipFile zip = new ZipFile(System.Text.Encoding.Default)) //System.Text.Encoding.Default设置中文附件名称乱码,不设置会出现乱码  
    70.       {  
    71.           foreach (var file in filesPath)  
    72.           {  
    73.               zip.AddFile(file,"");  
    74. //第二个参数为空,说明压缩的文件不会存在多层文件夹。比如C: estac.doc 压缩后解压文件会出现c.doc  
    75. //如果改成zip.AddFile(file);则会出现多层文件夹压缩,比如C: estac.doc 压缩后解压文件会出现testac.doc  
    76.           }  
    77.           zipPath = string.Format("{0}\{1}.zip", dirPath, DateTime.Now.ToString("yyyyMMddHHmmss"));  
    78.           zip.Save(zipPath);  
    79.       }  
    80.       return zipPath;  
    81.   }   
    82.   #endregion  


    调用:

    [csharp] view plain copy
     
      1.               List<string> filesPath = new List<string>();  
      2. filesPath.Add(“C:/test/a.doc”);  
      3. filesPath.Add(“C:/test/b.doc”);  
      4. //filesPath.Add(Server.MapPath("~/text/Files/c.doc"));//可以设置添加虚拟路径  
      5.   
      6. var dirPath="Server.MapPath("~/compress/")";  
      7. var filePath=GetCompressPath(dirPath,filesPath);//返回压缩的文件  
  • 相关阅读:
    LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
    LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
    LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
    LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
    LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
    LeetCode 617. 合并二叉树(Merge Two Binary Trees)
    LeetCode 206. 反转链表(Reverse Linked List) 16
    LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
    LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
    LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14
  • 原文地址:https://www.cnblogs.com/husam/p/9104846.html
Copyright © 2011-2022 走看看