c# 文件壓縮zip或?qū)ip文件解壓的方法
1.必須Dll:
ICSharpCode.SharpZipLib.dll??蓮腘utget程序包中獲取。
2.壓縮文件
/// <summary>
/// 壓縮文件成zip
/// </summary>
/// <param name="fileZip">壓縮成zip文件的絕對(duì)路徑</param>
/// <param name="fileName">被壓縮指定文件的名字</param>
/// <param name="zipFilePath"></param>
/// <returns></returns>
public bool CreateZipFile(string fileZip,string fileName, string zipFilePath)
{
bool isZip = false;
if (!Directory.Exists(zipFilePath))
{
Logger.Info($"Cannot find directory {zipFilePath}", false, "FileToZip");
return isZip;
}
try
{
string[] filenames = Directory.GetFiles(zipFilePath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(fileZip)))
{
s.SetLevel(9); // 壓縮級(jí)別 0-9
//s.Password = "123"; //Zip壓縮文件密碼
byte[] buffer = new byte[4096]; //緩沖區(qū)大小
foreach (string file in filenames.ToList())
{
if (file== zipFilePath+fileName)//指定被壓縮文件的絕對(duì)路徑
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
fs.Close();
fs.Dispose();
}
break;
}
}
s.Finish();
s.Close();
isZip = true;
}
}
catch (Exception ex)
{
Logger.Info($"Exception during processing {0}", false, "FileToZip");
}
return isZip;
}
3.將zip文件解壓
/// <summary>
/// 解壓文件
/// </summary>
/// <param name="zipFilePath">壓縮文件的絕對(duì)路徑</param>
public void UnZipFile(string zipFilePath)
{
if (!File.Exists(zipFilePath))
{
Logger.Info($"Cannot find file {zipFilePath}", false, "FileToZip");
return;
}
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName?.Length > 0)
{
Directory.CreateDirectory(directoryName);
}
if (!string.IsNullOrEmpty(fileName))
{
using (FileStream streamWriter = File.Create(theEntry.Name))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
}
4.其它:其中的Logger是Log4的用法。
以上這篇c# 文件壓縮zip或?qū)ip文件解壓的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用Linq to XML進(jìn)行XPath查詢的代碼實(shí)現(xiàn)
最近在用到HtmlAgliltyPack進(jìn)行結(jié)點(diǎn)查詢時(shí),發(fā)現(xiàn)這里選擇結(jié)點(diǎn)使用的是XPath,所以這里總結(jié)一下在C#中使用XPath查詢XML的方法,習(xí)慣了用Linq,這里也是用的Linq to xml的,需要的朋友可以參考下2024-08-08
在C#中調(diào)用VBScript、javascript等腳本的實(shí)現(xiàn)代碼
在C#中調(diào)用VBScript、javascript等腳本的實(shí)現(xiàn)步驟,需要的朋友可以參考下。2009-11-11
Unity封裝延時(shí)調(diào)用定時(shí)器
這篇文章主要為大家詳細(xì)介紹了Unity封裝延時(shí)調(diào)用定時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#基于正則表達(dá)式刪除字符串中數(shù)字或非數(shù)字的方法
這篇文章主要介紹了C#基于正則表達(dá)式刪除字符串中數(shù)字或非數(shù)字的方法,涉及C#針對(duì)數(shù)字的簡(jiǎn)單正則匹配相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
C#?漢字與拼音互轉(zhuǎn)的實(shí)現(xiàn)示例
本文主要介紹了C#?漢字與拼音互轉(zhuǎn)的實(shí)現(xiàn)示例,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

