基于.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)文章
C#反射(Reflection)對類的屬性get或set值實現(xiàn)思路
可以使用反射動態(tài)創(chuàng)建類型的實例,將類型綁定到現(xiàn)有對象,或從現(xiàn)有對象獲取類型并調(diào)用其方法或訪問其字段和屬性,接下來為大家介紹下對一個類別的屬性進(jìn)行set和get值,感興趣的各位可以參考下哈2013-03-03ASP.NET Core3.x API版本控制的實現(xiàn)
這篇文章主要介紹了ASP.NET Core3.x API版本控制的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問題
這篇文章主要介紹了解決 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死鎖問題的相關(guān)資料,需要的朋友可以參考下2016-09-09asp.net實現(xiàn)生成靜態(tài)頁并添加鏈接的方法
這篇文章主要介紹了asp.net實現(xiàn)生成靜態(tài)頁并添加鏈接的方法,非常實用的功能,需要的朋友可以參考下2014-07-07完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁
這篇文章主要介紹了完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁,需要的朋友可以參考下2014-12-12