C# ZipArchive加壓解壓zip文件方式
更新時(shí)間:2025年11月06日 14:11:58 作者:mouka~
本文介紹了一種在內(nèi)存中創(chuàng)建zip文件并獲取其流的方法,從而避免了創(chuàng)建臨時(shí)文件和下載文件的需要,最后展示了如何解壓該zip文件
創(chuàng)建zip文件
using (var fileStream = new FileStream(saveZipName, FileMode.CreateNew))
{
// 使用內(nèi)存流創(chuàng)建壓縮文件
using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
{
var files = Directory.GetFiles(taskDir, "*", SearchOption.AllDirectories);
foreach (var file in files)
{
//相對(duì)路徑
var relativePath = Path.GetRelativePath(FileAndFolderConfig.CollectionSpaceFolder.Path, file);
archive.CreateEntryFromFile(file, relativePath);
}
}
}獲取zip文件流,在內(nèi)存中創(chuàng)建zip文件避免創(chuàng)建臨時(shí)文件
using (var memoryStream = new MemoryStream())
{
// 使用內(nèi)存流創(chuàng)建壓縮文件
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var files = Directory.GetFiles(taskDir, "*", SearchOption.AllDirectories);
foreach (var file in files)
{
//相對(duì)路徑
var relativePath = Path.GetRelativePath(FileAndFolderConfig.CollectionSpaceFolder.Path, file);
var entry = archive.CreateEntry(relativePath, CompressionLevel.Optimal);
using var entryStream = entry.Open();
using var fileStream = File.OpenRead(file);
await fileStream.CopyToAsync(entryStream);
}
}
memoryStream.Position = 0; // 重置流位置
}下載文件并解壓
// 下載文件到內(nèi)存流
using (var memoryStream = new MemoryStream())
{
await taskFile.CopyToAsync(memoryStream);
taskFile.Close();
memoryStream.Position = 0; // 重置流位置
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Read))
{
foreach (var entry in archive.Entries)
{
var entryOutputPath = Path.Combine(FileAndFolderConfig.CollectionSpaceFolder.Path, entry.FullName);
// 如果是目錄條目,創(chuàng)建目錄
if (string.IsNullOrEmpty(entry.Name))
{
Directory.CreateDirectory(Path.GetDirectoryName(entryOutputPath));
continue;
}
// 確保父目錄存在
var parentDir = Path.GetDirectoryName(entryOutputPath);
if (!string.IsNullOrEmpty(parentDir))
{
Directory.CreateDirectory(parentDir);
}
// 解壓文件
entry.ExtractToFile(entryOutputPath, overwrite: true);
}
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)json格式轉(zhuǎn)換成對(duì)象并更換key的方法
這篇文章主要介紹了C#實(shí)現(xiàn)json格式轉(zhuǎn)換成對(duì)象并更換key的方法,涉及C#操作json格式數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
這篇文章主要介紹了C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法,涉及C#實(shí)現(xiàn)HTML、WORD及PDF等文件格式轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-09-09
C#如何實(shí)時(shí)獲取鼠標(biāo)坐標(biāo)及模擬鼠標(biāo)點(diǎn)擊
這篇文章主要介紹了C#如何實(shí)時(shí)獲取鼠標(biāo)坐標(biāo)及模擬鼠標(biāo)點(diǎn)擊問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
C# Winform按鈕中圖片實(shí)現(xiàn)左圖右字的效果實(shí)例
這篇文章主要給大家介紹了關(guān)于C# Winform按鈕中圖片實(shí)現(xiàn)左圖右字效果的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
C#網(wǎng)頁(yè)跳轉(zhuǎn)方法總結(jié)
這篇文章主要介紹了C#網(wǎng)頁(yè)跳轉(zhuǎn)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2015-12-12
在C#中基于Semantic?Kernel的檢索增強(qiáng)生成(RAG)實(shí)踐記錄
SemanticKernel是一個(gè)用于集成和操作大語(yǔ)言模型的應(yīng)用程序框架,支持C#、Python和Java等多種編程語(yǔ)言,通過(guò)SemanticKernel,開(kāi)發(fā)者可以輕松構(gòu)建基于最新AI技術(shù)的應(yīng)用程序2024-10-10

