網站首頁 編程語言 正文
zip 是一個非常常見的壓縮包格式,本文主要用于說明如何使用代碼 文件或文件夾壓縮為 zip壓縮包及其解壓操作,
我們采用的是 微軟官方的實現,所以也不需要安裝第三方的組件包。
使用的時候記得?using System.IO.Compression;
/// <summary>
/// 將指定目錄壓縮為Zip文件
/// </summary>
/// <param name="folderPath">文件夾地址 D:/1/ </param>
/// <param name="zipPath">zip地址 D:/1.zip </param>
public static void CompressDirectoryZip(string folderPath, string zipPath)
{
DirectoryInfo directoryInfo = new(zipPath);
if (directoryInfo.Parent != null)
{
directoryInfo = directoryInfo.Parent;
}
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
ZipFile.CreateFromDirectory(folderPath, zipPath, CompressionLevel.Optimal, false);
}
其中?CompressionLevel?是個枚舉,支持下面四種類型
枚舉 | 值 | 注解 |
---|---|---|
Optimal | 0 | 壓縮操作應以最佳方式平衡壓縮速度和輸出大小。 |
Fastest | 1 | 即使結果文件未可選擇性地壓縮,壓縮操作也應盡快完成。 |
NoCompression | 2 | 該文件不應執行壓縮。 |
SmallestSize | 3 | 壓縮操作應盡可能小地創建輸出,即使該操作需要更長的時間才能完成。 |
我方法這里直接固定了采用 CompressionLevel.Optimal,大家可以根據個人需求自行調整。
/// <summary>
/// 將指定文件壓縮為Zip文件
/// </summary>
/// <param name="filePath">文件地址 D:/1.txt </param>
/// <param name="zipPath">zip地址 D:/1.zip </param>
public static void CompressFileZip(string filePath, string zipPath)
{
FileInfo fileInfo = new FileInfo(filePath);
string dirPath = fileInfo.DirectoryName?.Replace("\\", "/") + "/";
string tempPath = dirPath + Guid.NewGuid() + "_temp/";
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
fileInfo.CopyTo(tempPath + fileInfo.Name);
CompressDirectoryZip(tempPath, zipPath);
DirectoryInfo directory = new(path);
if (directory.Exists)
{
//將文件夾屬性設置為普通,如:只讀文件夾設置為普通
directory.Attributes = FileAttributes.Normal;
directory.Delete(true);
}
}
壓縮單個文件的邏輯其實就是先將我們要壓縮的文件復制到一個臨時目錄,然后對臨時目錄執行了壓縮動作,壓縮完成之后又刪除了臨時目錄。
/// <summary>
/// 解壓Zip文件到指定目錄
/// </summary>
/// <param name="zipPath">zip地址 D:/1.zip</param>
/// <param name="folderPath">文件夾地址 D:/1/</param>
public static void DecompressZip(string zipPath, string folderPath)
{
DirectoryInfo directoryInfo = new(folderPath);
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
ZipFile.ExtractToDirectory(zipPath, folderPath);
}
至此 C# 使用原生 System.IO.Compression 實現 zip 的壓縮與解壓 就講解完了,有任何不明白的,可以在文章下面評論或者私信我,歡迎大家積極的討論交流,有興趣的朋友可以關注我目前在維護的一個 .NET 基礎框架項目,項目地址如下
https://github.com/berkerdong/NetEngine.git
https://gitee.com/berkerdong/NetEngine.git
原文鏈接:https://www.cnblogs.com/berkerdong/archive/2022/09/27/16733726.html
相關推薦
- 2022-06-25 基于Python實現五子棋游戲_python
- 2022-10-07 Android?Gradle?三方依賴管理詳解_Android
- 2023-07-07 關于啟動報錯ModuleNotFoundError:No module named ‘python-
- 2022-10-12 Docker開啟遠程連接并實現安全通信詳解_docker
- 2022-11-08 goalng?結構體?方法集?接口實例詳解_Golang
- 2022-09-18 Go語言包管理工具Godep的用法_Golang
- 2022-09-30 關于react中列表渲染的局部刷新問題_React
- 2022-07-16 typescript封裝websocket,監測心跳,斷開重連
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支