C#實(shí)現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
本文實(shí)例講述了C#實(shí)現(xiàn)文件壓縮與解壓的方法。分享給大家供大家參考,具體如下:
在企業(yè)開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)遇到文件的壓縮與解壓,雖然網(wǎng)上很多流行的壓縮文件格式都是RAR的,但是由于RAR不是一個(gè)開(kāi)放的標(biāo)準(zhǔn),因此ZIP成了更多人的選擇。如果你不想自己開(kāi)發(fā)的話(huà)可以選擇開(kāi)源的項(xiàng)目,比如SharpZipLib就是一個(gè)不錯(cuò)的選擇。
組件的使用比較簡(jiǎn)單,請(qǐng)參照下面的代碼。點(diǎn)擊下載項(xiàng)目源碼。
/* * Gary Zhang -- cbcye@live.com * www.cbcye.com * www.quicklearn.cn * cbcye.cnblogs.com */ using System; using System.Collections.Generic; using System.Text; using System.IO; using ICSharpCode.SharpZipLib.Zip; using System.Diagnostics; using ICSharpCode.SharpZipLib.Core; namespace TestConsole { class Program { static void Main() { //CreateZipFile(@"d:\", @"d:\a.zip"); UnZipFile(@"d:\a.zip"); Console.Read(); } private static void CreateZipFile(string filesPath, string zipFilePath) { if (!Directory.Exists(filesPath)) { Console.WriteLine("Cannot find directory '{0}'", filesPath); return; } try { string[] filenames = Directory.GetFiles(filesPath); using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath))) { s.SetLevel(9); // 壓縮級(jí)別 0-9 //s.Password = "123"; //Zip壓縮文件密碼 byte[] buffer = new byte[4096]; //緩沖區(qū)大小 foreach (string file in filenames) { 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); } } s.Finish(); s.Close(); } } catch (Exception ex) { Console.WriteLine("Exception during processing {0}", ex); } } private static void UnZipFile( string zipFilePath) { if (!File.Exists(zipFilePath)) { Console.WriteLine("Cannot find file '{0}'", zipFilePath); return; } using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { Console.WriteLine(theEntry.Name); string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); // create directory if (directoryName.Length > 0) { Directory.CreateDirectory(directoryName); } if (fileName != String.Empty) { 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; } } } } } } } } }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》及《C#程序設(shè)計(jì)之線(xiàn)程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
- C#實(shí)現(xiàn)GZip壓縮和解壓縮入門(mén)實(shí)例
- C#實(shí)現(xiàn)rar壓縮與解壓縮文件的方法
- C#文件流進(jìn)行壓縮和解壓縮的方法
- C# 利用ICSharpCode.SharpZipLib實(shí)現(xiàn)在線(xiàn)壓縮和解壓縮
- asp.net C#實(shí)現(xiàn)解壓縮文件的方法
- C#實(shí)現(xiàn)的文件壓縮和解壓縮類(lèi)
- C#實(shí)現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】
- C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能
- C#自定義字符串壓縮和解壓縮的方法
- C#壓縮或解壓rar、zip文件方法實(shí)例
相關(guān)文章
C#實(shí)現(xiàn)數(shù)字轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)數(shù)字轉(zhuǎn)換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Unity 實(shí)現(xiàn)刪除missing腳本組件
這篇文章主要介紹了Unity 刪除missing腳本組件的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04