基于.NET 4.5 壓縮的使用
在.NET 4.5中新加入的壓縮的命名空間和方法??梢話仐塈CSharpCode.SharpZipLib.dll 這個類庫了。性能上不相上下。但是能夠大大簡化你的代碼。如果開始使用.NET FrameWork4.5 做壓縮不妨試試自帶的壓縮方法.
傳統(tǒng)使用ICSharpCode.SharpZipLib.dll 所寫的代碼。
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("消耗時間:{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("消耗時間:{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);
}
怎么樣代碼是不是簡潔了很多呢?
相關(guān)文章
未在本地計算機(jī)上注冊“microsoft.ACE.oledb.12.0”提供程序報錯的解決辦法
這篇文章主要給大家介紹了關(guān)于未在本地計算機(jī)上注冊“microsoft.ACE.oledb.12.0”提供程序報錯的完美解決辦法,需要的朋友可以參考下2019-03-03
Asp.Net如何將多個RadioButton指定在一個組中
將多個RadioButton指定在一個組中,實現(xiàn)其實很簡單,一句代碼即可,具體如下,希望對大家有所幫助2013-12-12
js獲取Treeview選中的節(jié)點(C#選中CheckBox項)
方法網(wǎng)上有很多,試了一下都有瑕疵,于是設(shè)置斷點調(diào)試,各個屬性查找有用的字段,終于找到,接下來與大家分享解決方法,需要了解的朋友可以參考下2012-12-12
關(guān)于.net環(huán)境下跨進(jìn)程、高頻率讀寫數(shù)據(jù)的問題
最近老大教給我一個項目,項目要求高頻次地讀寫數(shù)據(jù),數(shù)據(jù)量也不是很大,難點在于這個規(guī)模的熱點數(shù)據(jù),變化非常頻繁,下面把我的處理方法分享到腳本之家平臺,對.net跨進(jìn)程高頻率讀寫數(shù)據(jù)相關(guān)知識感興趣的朋友跟隨小編一起學(xué)習(xí)下吧2021-05-05
詳解ASP.NET MVC 常用擴(kuò)展點:過濾器、模型綁定
本篇文章主要介紹了詳解ASP.NET MVC 常用擴(kuò)展點:過濾器、模型綁定,非常具有實用價值,需要的朋友可以參考下2017-05-05

