• C#利用SharpZipLib解压或压缩文件(支持多层目录递归压缩)


    需要下载ICSharpCode.SharpZipLib.dll

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 using System.IO;
      8 using ICSharpCode.SharpZipLib.Checksums;
      9 using ICSharpCode.SharpZipLib.Zip;
     10 using ICSharpCode.SharpZipLib.GZip;
     11 
     12 namespace ZYOfficeWrap
     13 {
     14 
     15     /// <summary>
     16     /// 压缩文件格式
     17     /// </summary>
     18 
     19     public enum fileType
     20     {
     21         Zip = 1,
     22         RAR = 2,
     23         Tar =3
     24     }
     25     /// <summary>
     26     /// 文件解压缩操作类库
     27     /// </summary>
     28     public class FileZipOpr
     29     {
     30         /// <summary>
     31         /// 构造函数
     32         /// </summary>
     33         public FileZipOpr()
     34         {
     35             
     36         }
     37 
     38         /// <summary>
     39         /// 压缩单个文件
     40         /// </summary>
     41         /// <param name="fileToZip">要压缩的文件</param>
     42         /// <param name="zipedFile">压缩后的文件</param>
     43         /// <param name="compressionLevel">压缩等级</param>
     44         /// <param name="blockSize">每次写入大小</param>
     45         public  void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
     46         {
     47             //如果文件没有找到,则报错
     48             if (!File.Exists(fileToZip))
     49             {
     50                 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
     51             }
     52 
     53             using (FileStream ZipFile = File.Create(zipedFile))
     54             {
     55                 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
     56                 {
     57                     using (FileStream StreamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
     58                     {
     59                         string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\") + 1);
     60                         ZipEntry ZipEntry = new ZipEntry(fileName);
     61                         ZipStream.PutNextEntry(ZipEntry);
     62                         ZipStream.SetLevel(compressionLevel);
     63                         byte[] buffer = new byte[blockSize];
     64                         int sizeRead = 0;
     65                         try
     66                         {
     67                             do
     68                             {
     69                                 sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
     70                                 ZipStream.Write(buffer, 0, sizeRead);
     71                             }
     72                             while (sizeRead > 0);
     73                         }
     74                         catch (System.Exception ex)
     75                         {
     76                             throw ex;
     77                         }
     78                         StreamToZip.Close();
     79                     }
     80 
     81                     ZipStream.Finish();
     82                     ZipStream.Close();
     83                 }
     84                 ZipFile.Close();
     85             }
     86         }
     87 
     88         /// <summary>
     89         /// 压缩单个文件
     90         /// </summary>
     91         /// <param name="fileToZip">要进行压缩的文件名</param>
     92         /// <param name="zipedFile">压缩后生成的压缩文件名</param>
     93         public  void ZipFile(string fileToZip, string zipedFile)
     94         {
     95             //如果文件没有找到,则报错
     96             if (!File.Exists(fileToZip))
     97             {
     98                 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
     99             }
    100 
    101             using (FileStream fs = File.OpenRead(fileToZip))
    102             {
    103                 byte[] buffer = new byte[fs.Length];
    104                 fs.Read(buffer, 0, buffer.Length);
    105                 fs.Close();
    106 
    107                 using (FileStream ZipFile = File.Create(zipedFile))
    108                 {
    109                     using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
    110                     {
    111                         string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\") + 1);
    112                         ZipEntry ZipEntry = new ZipEntry(fileName);
    113                         ZipStream.PutNextEntry(ZipEntry);
    114                         ZipStream.SetLevel(5);
    115 
    116                         ZipStream.Write(buffer, 0, buffer.Length);
    117                         ZipStream.Finish();
    118                         ZipStream.Close();
    119                     }
    120                 }
    121             }
    122         }
    123 
    124         /// <summary>
    125         /// 压缩多层目录
    126         /// </summary>
    127         /// <param name="strDirectory">要进行压缩的文件夹</param>
    128         /// <param name="zipedFile">压缩后生成的压缩文件名</param>
    129         public  void ZipFileDirectory(string strDirectory, string zipedFile)
    130         {
    131             using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
    132             {
    133                 using (ZipOutputStream s = new ZipOutputStream(ZipFile))
    134                 {
    135                     ZipSetp(strDirectory, s, "");
    136                 }
    137             }
    138         }
    139 
    140         /// <summary>
    141         /// 递归遍历目录
    142         /// </summary>
    143         /// <param name="strDirectory">文件夹名称</param>
    144         /// <param name="s">The ZipOutputStream Object.</param>
    145         /// <param name="parentPath"></param>
    146         private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
    147         {
    148             if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
    149             {
    150                 strDirectory += Path.DirectorySeparatorChar;
    151             }
    152             Crc32 crc = new Crc32();
    153 
    154             string[] filenames = Directory.GetFileSystemEntries(strDirectory);
    155 
    156             foreach (string file in filenames)// 遍历所有的文件和目录
    157             {
    158 
    159                 if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
    160                 {
    161                     string pPath = parentPath;
    162                     pPath += file.Substring(file.LastIndexOf("\") + 1);
    163                     pPath += "\";
    164                     ZipSetp(file, s, pPath);
    165                 }
    166 
    167                 else // 否则直接压缩文件
    168                 {
    169                     //打开压缩文件
    170                     using (FileStream fs = File.OpenRead(file))
    171                     {
    172 
    173                         byte[] buffer = new byte[fs.Length];
    174                         fs.Read(buffer, 0, buffer.Length);
    175 
    176                         string fileName = parentPath + file.Substring(file.LastIndexOf("\") + 1);
    177                         ZipEntry entry = new ZipEntry(fileName);
    178 
    179                         entry.DateTime = DateTime.Now;
    180                         entry.Size = fs.Length;
    181 
    182                         fs.Close();
    183 
    184                         crc.Reset();
    185                         crc.Update(buffer);
    186 
    187                         entry.Crc = crc.Value;
    188                         s.PutNextEntry(entry);
    189 
    190                         s.Write(buffer, 0, buffer.Length);
    191                     }
    192                 }
    193             }
    194         }
    195 
    196         /// <summary>
    197         /// 解压缩一个 zip 文件。
    198         /// </summary>
    199         /// <param name="zipedFile">The ziped file.</param>
    200         /// <param name="strDirectory">The STR directory.</param>
    201         /// <param name="password">zip 文件的密码。</param>
    202         /// <param name="overWrite">是否覆盖已存在的文件。</param>
    203         public void UnZipFile(string zipedFile, string strDirectory, string password, bool overWrite)
    204         {
    205 
    206             if (strDirectory == "")
    207                 strDirectory = Directory.GetCurrentDirectory();
    208             if (!strDirectory.EndsWith("\"))
    209                 strDirectory = strDirectory + "\";
    210 
    211             using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
    212             {
    213                 s.Password = password;
    214                 ZipEntry theEntry;
    215 
    216                 while ((theEntry = s.GetNextEntry()) != null)
    217                 {
    218                     string directoryName = "";
    219                     string pathToZip = "";
    220                     pathToZip = theEntry.Name;
    221 
    222                     if (pathToZip != "")
    223                         directoryName = Path.GetDirectoryName(pathToZip) + "\";
    224 
    225                     string fileName = Path.GetFileName(pathToZip);
    226 
    227                     Directory.CreateDirectory(strDirectory + directoryName);
    228 
    229                     if (fileName != "")
    230                     {
    231                         if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
    232                         {
    233                             using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
    234                             {
    235                                 int size = 2048;
    236                                 byte[] data = new byte[2048];
    237                                 while (true)
    238                                 {
    239                                     size = s.Read(data, 0, data.Length);
    240 
    241                                     if (size > 0)
    242                                         streamWriter.Write(data, 0, size);
    243                                     else
    244                                         break;
    245                                 }
    246                                 streamWriter.Close();
    247                             }
    248                         }
    249                     }
    250                 }
    251 
    252                 s.Close();
    253             }
    254         }
    255 
    256     }
    257 }
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    C#使用BouncyCastle操作国密SM3
    Win10 2004种子
    Excel对比两列数据,找到重复项
    CentOS7 + frp远程访问内网Windows电脑
    Docker on Mac OS
    MySQL5.6中查询多边形包含点情况(ST_Contains、ST_Within)
    SQL查询表的所有字段名
    清除文本中Html的标签
  • 原文地址:https://www.cnblogs.com/lyd2016/p/5964768.html
一二三 - 开发者的网上家园