基于.NET 4.5 壓縮的使用
在.NET 4.5中新加入的壓縮的命名空間和方法??梢?huà)仐塈CSharpCode.SharpZipLib.dll 這個(gè)類(lèi)庫(kù)了。性能上不相上下。但是能夠大大簡(jiǎn)化你的代碼。如果開(kāi)始使用.NET FrameWork4.5 做壓縮不妨試試自帶的壓縮方法.
傳統(tǒng)使用ICSharpCode.SharpZipLib.dll 所寫(xiě)的代碼。
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
string path = @"E:\";
Compress(Directory.GetFiles(path), @"F:\4.0.zip");
watch.Stop();
Console.WriteLine("消耗時(shí)間:{0}", watch.ElapsedMilliseconds);
FileInfo f = new FileInfo(@"F:\4.0.zip");
Console.WriteLine("文件大小{0}", f.Length);
}
static void Compress(string[] filePaths, string zipFilePath)
{
byte[] _buffer = new byte[4096];
if (!Directory.Exists(zipFilePath))
Directory.CreateDirectory(Path.GetDirectoryName(zipFilePath));
using (ZipOutputStream zip = new ZipOutputStream(File.Create(zipFilePath)))
{
foreach (var item in filePaths)
{
if (!File.Exists(item))
{
Console.WriteLine("the file {0} not exist!", item);
}
else
{
ZipEntry entry = new ZipEntry(Path.GetFileName(item));
entry.DateTime = DateTime.Now;
zip.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(item))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(_buffer, 0, _buffer.Length);
zip.Write(_buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
}
zip.Finish();
zip.Close();
}
}
使用.NET FrameWork 4.5中自帶的壓縮。
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
string path = @"E:\";
Compress(path, @"F:\4.5.zip");
watch.Stop();
Console.WriteLine("消耗時(shí)間:{0}", watch.ElapsedMilliseconds);
FileInfo f = new FileInfo(@"F:\4.5.zip");
Console.WriteLine("文件大小{0}", f.Length);
}
static void Compress(string filePath, string zipFilePath)
{
ZipFile.CreateFromDirectory(filePath, zipFilePath, CompressionLevel.Fastest, false);
}
怎么樣代碼是不是簡(jiǎn)潔了很多呢?
相關(guān)文章
C#反射(Reflection)對(duì)類(lèi)的屬性get或set值實(shí)現(xiàn)思路
可以使用反射動(dòng)態(tài)創(chuàng)建類(lèi)型的實(shí)例,將類(lèi)型綁定到現(xiàn)有對(duì)象,或從現(xiàn)有對(duì)象獲取類(lèi)型并調(diào)用其方法或訪(fǎng)問(wèn)其字段和屬性,接下來(lái)為大家介紹下對(duì)一個(gè)類(lèi)別的屬性進(jìn)行set和get值,感興趣的各位可以參考下哈2013-03-03.NET實(shí)現(xiàn)簡(jiǎn)易的文件增量備份程序
這篇文章主要介紹了.NET實(shí)現(xiàn)簡(jiǎn)易的文件增量備份程序,需要的朋友可以參考下2016-06-06.NET中的repeater簡(jiǎn)介及分頁(yè)效果
Repeater控件是一個(gè)數(shù)據(jù)綁定容器控件,它能夠生成各個(gè)項(xiàng)的列表,并可以使用模板定義網(wǎng)頁(yè)上各個(gè)項(xiàng)的布局。本文對(duì)此進(jìn)行詳細(xì)介紹,下面跟著小編一起來(lái)看下吧2017-02-02ASP.NET Core3.x API版本控制的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core3.x API版本控制的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問(wèn)題
這篇文章主要介紹了解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-09-09asp.net實(shí)現(xiàn)生成靜態(tài)頁(yè)并添加鏈接的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)生成靜態(tài)頁(yè)并添加鏈接的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-07-07完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁(yè)
這篇文章主要介紹了完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁(yè),需要的朋友可以參考下2014-12-12