zoukankan      html  css  js  c++  java
  • C# ICSharpCode.SharpZipLib 压缩文件 yang

     
    利用ICSharpCode.SharpZipLib.dll 压缩文件夹
    View Code
     1 private void zip(string strFile, ZipOutputStream s, string staticFile)
     2         {
     3             if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
     4             {
     5                 strFile = strFile + Path.DirectorySeparatorChar;
     6             }
     7             Crc32 crc = new Crc32();
     8             string[] fileSystemEntries = Directory.GetFileSystemEntries(strFile);
     9             foreach (string str in fileSystemEntries)
    10             {
    11                 if (Directory.Exists(str))
    12                 {
    13                     zip(str, s, staticFile);
    14                 }
    15                 else
    16                 {
    17                     FileStream stream = File.OpenRead(str);
    18                     byte[] buffer = new byte[stream.Length];
    19                     stream.Read(buffer, 0, buffer.Length);
    20                     ZipEntry entry = new ZipEntry(str.Substring(staticFile.LastIndexOf(@"\") + 1));
    21                     entry.DateTime=DateTime.Now;
    22                     entry.Size = stream.Length;
    23                     stream.Close();
    24                     crc.Reset();
    25                     crc.Update(buffer);
    26                     entry.Crc=crc.Value;
    27                     s.PutNextEntry(entry);
    28                     s.Write(buffer, 0, buffer.Length);
    29                 }
    30             }
    31         }
    32         /// <summary>
    33         /// 
    34         /// </summary>
    35         /// <param name="strFile">文件夹</param>
    36         /// <param name="strZip">压缩后文件</param>
    37         public void ZipFile(string strFile, string strZip)
    38         {
    39             if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
    40             {
    41                 strFile = strFile + Path.DirectorySeparatorChar;
    42             }
    43             ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
    44             s.Password = "**";//需要密码可以设置
    45             s.SetLevel(6);
    46             zip(strFile, s, strFile);
    47             s.Finish();
    48             s.Close();
    49         }
  • 相关阅读:
    Android kotlin jsoup解析网页html代码
    Android kotlin 判断网络状态
    ASP.NET设计模式笔记1
    C# SMTP发送邮件
    SQL连接数和CPU使用情况查询
    Grafana笔记
    Docker笔记
    solidity学习(四)---storage和memory关键字
    e-book
    solidity学习(四)-- Require(), Assert(), Revert()的用法和区别
  • 原文地址:https://www.cnblogs.com/alibaba/p/2619872.html
Copyright © 2011-2022 走看看