项目上用到的,随手做个记录,哈哈。

直接上代码:

  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Collections.Generic;
  5 using System.IO;
  6 using ICSharpCode.SharpZipLib.Zip;
  7 using ICSharpCode.SharpZipLib.Checksums;
  8 namespace BLL
  9 {
 10     /// <summary>
 11     /// 文件(夹)压缩、解压缩
 12     /// </summary>
 13     public class FileCompression
 14     {
 15         #region 压缩文件
 16         /// <summary>
 17         /// 压缩文件
 18         /// </summary>
 19         /// <param name="fileNames">要打包的文件列表</param>
 20         /// <param name="GzipFileName">目标文件名</param>
 21         /// <param name="CompressionLevel">压缩品质级别(0~9)</param>
 22         /// <param name="deleteFile">是否删除原文件</param>
 23         public static void CompressFile(List<FileInfo> fileNames, string GzipFileName, int CompressionLevel, bool deleteFile)
 24         {
 25             ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
 26             try
 27             {
 28                 s.SetLevel(CompressionLevel);   //0 - store only to 9 - means best compression
 29                 foreach (FileInfo file in fileNames)
 30                 {
 31                     FileStream fs = null;
 32                     try
 33                     {
 34                         fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
 35                     }
 36                     catch
 37                     { continue; }
 38                     //  方法二,将文件分批读入缓冲区
 39                     byte[] data = new byte[2048];
 40                     int size = 2048;
 41                     ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name));
 42                     entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime);
 43                     s.PutNextEntry(entry);
 44                     while (true)
 45                     {
 46                         size = fs.Read(data, 0, size);
 47                         if (size <= 0) break;
 48                         s.Write(data, 0, size);
 49                     }
 50                     fs.Close();
 51                     if (deleteFile)
 52                     {
 53                         file.Delete();
 54                     }
 55                 }
 56             }
 57             finally
 58             {
 59                 s.Finish();
 60                 s.Close();
 61             }
 62         }
 63         /// <summary>
 64         /// 压缩文件夹
 65         /// </summary>
 66         /// <param name="dirPath">要打包的文件夹</param>
 67         /// <param name="GzipFileName">目标文件名</param>
 68         /// <param name="CompressionLevel">压缩品质级别(0~9)</param>
 69         /// <param name="deleteDir">是否删除原文件夹</param>
 70         public static void CompressDirectory(string dirPath, string GzipFileName, int CompressionLevel, bool deleteDir)
 71         {
 72             //压缩文件为空时默认与压缩文件夹同一级目录
 73             if (GzipFileName == string.Empty)
 74             {
 75                 GzipFileName = dirPath.Substring(dirPath.LastIndexOf("//") + 1);
 76                 GzipFileName = dirPath.Substring(0, dirPath.LastIndexOf("//")) + "//" + GzipFileName + ".zip";
 77             }
 78             //if (Path.GetExtension(GzipFileName) != ".zip")
 79             //{
 80             //    GzipFileName = GzipFileName + ".zip";
 81             //}
 82             using (ZipOutputStream zipoutputstream = new ZipOutputStream(File.Create(GzipFileName)))
 83             {
 84                 zipoutputstream.SetLevel(CompressionLevel);
 85                 Crc32 crc = new Crc32();
 86                 Dictionary<string, DateTime> fileList = GetAllFies(dirPath);
 87                 foreach (KeyValuePair<string, DateTime> item in fileList)
 88                 {
 89                     FileStream fs = File.OpenRead(item.Key.ToString());
 90                     byte[] buffer = new byte[fs.Length];
 91                     fs.Read(buffer, 0, buffer.Length);
 92                     ZipEntry entry = new ZipEntry(item.Key.Substring(dirPath.Length));
 93                     entry.DateTime = item.Value;
 94                     entry.Size = fs.Length;
 95                     fs.Close();
 96                     crc.Reset();
 97                     crc.Update(buffer);
 98                     entry.Crc = crc.Value;
 99                     zipoutputstream.PutNextEntry(entry);
100                     zipoutputstream.Write(buffer, 0, buffer.Length);
101                 }
102             }
103             if (deleteDir)
104             {
105                 Directory.Delete(dirPath, true);
106             }
107         }
108         /// <summary>
109         /// 获取所有文件
110         /// </summary>
111         /// <returns></returns>
112         private static Dictionary<string, DateTime> GetAllFies(string dir)
113         {
114             Dictionary<string, DateTime> FilesList = new Dictionary<string, DateTime>();
115             DirectoryInfo fileDire = new DirectoryInfo(dir);
116             if (!fileDire.Exists)
117             {
118                 throw new System.IO.FileNotFoundException("目录:" + fileDire.FullName + "没有找到!");
119             }
120             GetAllDirFiles(fileDire, FilesList);
121             GetAllDirsFiles(fileDire.GetDirectories(), FilesList);
122             return FilesList;
123         }
124         /// <summary>
125         /// 获取一个文件夹下的所有文件夹里的文件
126         /// </summary>
127         /// <param name="dirs"></param>
128         /// <param name="filesList"></param>
129         private static void GetAllDirsFiles(DirectoryInfo[] dirs, Dictionary<string, DateTime> filesList)
130         {
131             foreach (DirectoryInfo dir in dirs)
132             {
133                 foreach (FileInfo file in dir.GetFiles("*.*"))
134                 {
135                     filesList.Add(file.FullName, file.LastWriteTime);
136                 }
137                 GetAllDirsFiles(dir.GetDirectories(), filesList);
138             }
139         }
140         /// <summary>
141         /// 获取一个文件夹下的文件
142         /// </summary>
143         /// <param name="dir">目录名称</param>
144         /// <param name="filesList">文件列表HastTable</param>
145         private static void GetAllDirFiles(DirectoryInfo dir, Dictionary<string, DateTime> filesList)
146         {
147             foreach (FileInfo file in dir.GetFiles("*.*"))
148             {
149                 filesList.Add(file.FullName, file.LastWriteTime);
150             }
151         }
152         #endregion
153         #region 解压缩文件
154         /// <summary>
155         /// 解压缩文件
156         /// </summary>
157         /// <param name="GzipFile">压缩包文件名</param>
158         /// <param name="targetPath">解压缩目标路径</param>
159         public static void Decompress(string GzipFile, string targetPath)
160         {
161             //string directoryName = Path.GetDirectoryName(targetPath + "//") + "//";
162             string directoryName = targetPath;
163             if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
164             string CurrentDirectory = directoryName;
165             byte[] data = new byte[2048];
166             int size = 2048;
167             ZipEntry theEntry = null;
168             using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
169             {
170                 while ((theEntry = s.GetNextEntry()) != null)
171                 {
172                     if (theEntry.IsDirectory)
173                     {// 该结点是目录
174                         if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
175                     }
176                     else
177                     {
178                         if (theEntry.Name != String.Empty)
179                         {
180                             //  检查多级目录是否存在
181                             if (theEntry.Name.Contains("//"))
182                             {
183                                 string parentDirPath = theEntry.Name.Remove(theEntry.Name.LastIndexOf("//") + 1);
184                                 if (!Directory.Exists(parentDirPath))
185                                 {
186                                     Directory.CreateDirectory(CurrentDirectory + parentDirPath);
187                                 }
188                             }
189
190                             //解压文件到指定的目录
191                             using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
192                             {
193                                 while (true)
194                                 {
195                                     size = s.Read(data, 0, data.Length);
196                                     if (size <= 0) break;
197                                     streamWriter.Write(data, 0, size);
198                                 }
199                                 streamWriter.Close();
200                             }
201                         }
202                     }
203                 }
204                 s.Close();
205             }
206         }
207         #endregion
208     }
209 }

View Code

封装的很彻底,基本不用修改什么,直接拿来用就行了。

找了很久,终于知道怎么把源代码附上了

源代码:https://files.cnblogs.com/files/hahahayang/C%E5%8E%8B%E7%BC%A9%E6%96%87%E4%BB%B6.zip

转载于:https://www.cnblogs.com/hahahayang/p/hahahayang.html

C# 文件/文件夹压缩解压缩相关推荐

  1. java 压缩文件tar_使用Java API进行tar.gz文件及文件夹压缩解压缩

    在java(JDK)中我们可以使用ZipOutputStream去创建zip压缩文件,(参考我之前写的文章 使用java API进行zip递归压缩文件夹以及解压 ),也可以使用GZIPOutputSt ...

  2. 使用Java API进行tar.gz文件及文件夹压缩解压缩

    在java(JDK)中我们可以使用ZipOutputStream去创建zip压缩文件,(参考我之前写的文章 使用java API进行zip递归压缩文件夹以及解压 ),也可以使用GZIPOutputSt ...

  3. Linux(CentOS)目录操作命令、文件操作命令、压缩解压缩命令

    一.目录操作命令 ls命令 - 功能说明:显示文件和目录列表. - 命令格式:ls [参数] [<文件或目录> -] - 常用参数: -a : 不隐藏任何以"."字符开 ...

  4. c语言程序压缩解压缩文件夹,【转】使用VC++压缩解压缩文件夹

    前言 项目中要用到一个压缩解压缩的模块, 看了很多文章和源代码, 都不是很称心, 现在把我自己实现的代码和大家分享. 要求: 1.使用Unicode(支持中文). 2.使用源代码.(不使用静态或者动态 ...

  5. .tar实现对文件和目录的压缩解压缩

    .tar实现对文件和目录的压缩解压缩 1.tar命令 功能描述:将文件或者目录进行打包.或者解压缩 格式:tar [参数] [打包后的文件名] [需要打包的文件或目录] 其中参数包括一下几个: -c ...

  6. MAC/Linux 压缩/解压缩命令大全整理 gzip / tar / zip

    1-1, 常用压缩解压缩之gzip 压缩 gzip filename #对某个文件进行压缩,会默认生成.gz 的压缩文件,并且删除原文件: gzip -k filename 或者 gzip -c fi ...

  7. 【C语言-数据结构与算法】-哈夫曼压缩解压缩-终局-如何做一个自己独有的压缩软件

    哈夫曼压缩&解压缩 Ⅰ 前言 Ⅱ 需求分析&主函数带参的应用 A. 需求分析 B. 压缩部分 B. 解压缩部分 Ⅲ 哈夫曼压缩 A. 代码分析 B. 从文件中读取内容生成频度表 C. ...

  8. C#压缩、解压缩文件(夹)(rar、zip)

    主要是使用Rar.exe压缩解压文件(夹)(*.rar),另外还有使用SevenZipSharp.dll.zLib1.dll.7z.dll压缩解压文件(夹)(*.zip).需要注意的几点如下: 1.注 ...

  9. 服务器里解压缩gz文件夹,Shell命令文件压缩解压缩之gzip、zip的案例分析

    Shell命令文件压缩解压缩之gzip.zip的案例分析 发布时间:2020-11-13 10:32:36 来源:亿速云 阅读:114 作者:小新 小编给大家分享一下Shell命令文件压缩解压缩之gz ...

最新文章

  1. CRM:把 isv.config.xml 按钮事件移动到 entity.onload()
  2. 开源组织:Datawhale
  3. Hive之数据倾斜的原因和解决方法
  4. 设计模式之十(外观模式)
  5. 开源RefreshListView下拉刷新效果
  6. Windows 10修改环境变量方法
  7. uva 12253 - Simple Encryption(dfs)
  8. 网络自由访问 巧解除Win XP文件共享限制
  9. mysql指定库执行sql语句_对多个mysql的一部分库进行执行sql语句
  10. 蒋文华《博弈论》笔记及视频摘录
  11. 前端实现base64解码编码
  12. 解决使用CSDN下载东西时,点击直接下载没有反应的问题
  13. java分解因式_Java将一个整数因式分解
  14. 小米air12.5做Java_到底够不够用? 小米笔记本Air12.5性能测试
  15. 使用Qt进行音视频播放
  16. 观察 | 家长焦虑,教培着急,暑期“培训热”今年还会持续吗?
  17. 加盐密码哈希:如何正确使用 (密码加密的经典文章)
  18. 微信小程序 图片旋转后上传
  19. 银河系中一定有生命存在
  20. 河南单招计算机分数线,2019年河南单招分数线一般多少分

热门文章

  1. idea、eclipse常用快捷键
  2. [Swift通天遁地]七、数据与安全-(1)XML文档的创建和解析
  3. IntelliJ IDEA 2017 注册方法
  4. c#如何操作excel文件、Interior.ColorIndex 色彩列表
  5. Corona按钮只能让点击一次
  6. 图像转置的MATLAB和OpenCV源码
  7. mysql 怎么导入函数_mysql导入导出包括函数或者存储过程_MySQL
  8. k8s集群搭建教程(centos k8s搭建)
  9. linux内核网络协议栈--sk_buff结构体(四)
  10. 计算机传票录入教案,传票翻打教案.docx