.NET 压缩与解压

复制代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using ICSharpCode.SharpZipLib.Zip;
  7 using ICSharpCode.SharpZipLib.Checksums;
  8 using System.Security.Cryptography;
  9 
 10 namespace zip压缩与解压
 11 {
 12     public class ZipHelper
 13     {
 14         /// <summary>
 15         /// 压缩单个文件
 16         /// </summary>
 17         /// <param name="fileToZip">需压缩的文件名</param>
 18         /// <param name="zipFile">压缩后的文件名(文件名都是绝对路径)</param>
 19         /// <param name="level">压缩等级(0-9)</param>
 20         /// <param name="password">压缩密码(解压是需要的密码)</param>
 21         public static void ZipFile(string fileToZip, string zipFile, int level = 5, string password = "123")
 22         {
 23             if (!File.Exists(fileToZip))
 24                 throw new FileNotFoundException("压缩文件" + fileToZip + "不存在");
 25 
 26             using (FileStream fs = File.OpenRead(fileToZip))
 27             {
 28                 fs.Position = 0;//设置流的起始位置
 29                 byte[] buffer = new byte[(int)fs.Length];
 30                 fs.Read(buffer, 0, buffer.Length);//读取的时候设置Position,写入的时候不需要设置
 31                 fs.Close();
 32                 using (FileStream zfstram = File.Create(zipFile))
 33                 {
 34                     using (ZipOutputStream zipstream = new ZipOutputStream(zfstram))
 35                     {
 36                         zipstream.Password = md5(password);//设置属性的时候在PutNextEntry函数之前
 37                         zipstream.SetLevel(level);
 38                         string fileName = fileToZip.Substring(fileToZip.LastIndexOf('\\') + 1);
 39                         ZipEntry entry = new ZipEntry(fileName);
 40                         zipstream.PutNextEntry(entry);
 41                         zipstream.Write(buffer, 0, buffer.Length);
 42                     }
 43                 }
 44 
 45             }
 46         }
 47 
 48         /// <summary>
 49         /// 压缩多个文件目录
 50         /// </summary>
 51         /// <param name="dirname">需要压缩的目录</param>
 52         /// <param name="zipFile">压缩后的文件名</param>
 53         /// <param name="level">压缩等级</param>
 54         /// <param name="password">密码</param>
 55         public static void ZipDir(string dirname, string zipFile, int level = 5, string password = "123")
 56         {
 57             ZipOutputStream zos = new ZipOutputStream(File.Create(zipFile));
 58             zos.Password = md5(password);
 59             zos.SetLevel(level);
 60             addZipEntry(dirname, zos, dirname);
 61             zos.Finish();
 62             zos.Close();
 63 
 64         }
 65         /// <summary>
 66         /// 往压缩文件里面添加Entry
 67         /// </summary>
 68         /// <param name="PathStr">文件路径</param>
 69         /// <param name="zos">ZipOutputStream</param>
 70         /// <param name="BaseDirName">基础目录</param>
 71         private static void addZipEntry(string PathStr, ZipOutputStream zos, string BaseDirName)
 72         {
 73             DirectoryInfo dir = new DirectoryInfo(PathStr);
 74             foreach (FileSystemInfo item in dir.GetFileSystemInfos())
 75             {
 76                 if ((item.Attributes & FileAttributes.Directory) == FileAttributes.Directory)//如果是文件夹继续递归
 77                 {
 78                     addZipEntry(item.FullName, zos, BaseDirName);
 79                 }
 80                 else
 81                 {
 82                     FileInfo f_item = (FileInfo)item;
 83                     using (FileStream fs = f_item.OpenRead())
 84                     {
 85                         byte[] buffer = new byte[(int)fs.Length];
 86                         fs.Position = 0;
 87                         fs.Read(buffer, 0, buffer.Length);
 88                         fs.Close();
 89                         ZipEntry z_entry = new ZipEntry(item.FullName.Replace(BaseDirName, ""));
 90                         zos.PutNextEntry(z_entry);
 91                         zos.Write(buffer, 0, buffer.Length);
 92                     }
 93                 }
 94             }
 95 
 96 
 97         }
 98 
 99         /// <summary>
100         /// 解压多个文件目录
101         /// </summary>
102         /// <param name="zfile">压缩文件绝对路径</param>
103         /// <param name="dirname">解压文件目录</param>
104         /// <param name="password">密码</param>
105         public static void UnZip(string zfile, string dirname, string password)
106         {
107             if (!Directory.Exists(dirname)) Directory.CreateDirectory(dirname);
108 
109             using (ZipInputStream zis = new ZipInputStream(File.OpenRead(zfile)))
110             {
111                 zis.Password = md5(password);
112                 ZipEntry entry;
113                 while ((entry = zis.GetNextEntry()) != null)
114                 {
115                     var strArr = entry.Name.Split('\\');//这边判断压缩文件里面是否存在目录,存在的话先创建目录后继续解压
116                     if (strArr.Length > 2)        
117                         Directory.CreateDirectory(dirname + @"\" + strArr[1]);
118                     
119                     using (FileStream dir_fs = File.Create(dirname + entry.Name))
120                     {
121                         int size = 1024 * 2;
122                         byte[] buffer = new byte[size];
123                         while (true)
124                         {
125                             size = zis.Read(buffer, 0, buffer.Length);
126                             if (size > 0)
127                                 dir_fs.Write(buffer, 0, size);
128                             else
129                                 break;
130                         }
131                     }
132                 }
133             }
134         }
135 
136         private static string md5(string pwd)
137         {
138             var res = "";
139             MD5 md = MD5.Create();
140             byte[] s = md.ComputeHash(Encoding.Default.GetBytes(pwd));
141             for (int i = 0; i < s.Length; i++)
142                 res = res + s[i].ToString("X");
143 
144             return res;
145         }
146     }
147 }
复制代码

 

posted @   每天进步多一点  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示