這篇文章主要介紹.NET4.5如何實(shí)現(xiàn)壓縮,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
在.NET 4.5中新加入的壓縮的命名空間和方法??梢話仐塈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);
}
以上是“.NET4.5如何實(shí)現(xiàn)壓縮”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞名稱:.NET4.5如何實(shí)現(xiàn)壓縮-創(chuàng)新互聯(lián)
當(dāng)前地址:http://www.rwnh.cn/article20/dcejco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、定制開(kāi)發(fā)、品牌網(wǎng)站制作、小程序開(kāi)發(fā)、商城網(wǎng)站、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容