The Zip, GZip, BZip2 and Tar Implementation For .NET
#ziplib (SharpZipLib, formerly NZipLib) is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented
as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language). The creator of #ziplib put it this
way: "I've ported the zip library over to C# because I needed gzip/zip compression and I didn't want to use libzip.dll or something like this. I want
all in pure C#."
Ref:http://www.icsharpcode.net/
Ref:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
Example:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Reference:ICSharpCode.SharpZipLib.dll ---------------------------------------------- using System.IO; using ICSharpCode.SharpZipLib.Core; using ICSharpCode.SharpZipLib.Zip; string sourceDir = "d:\\ziptest"; string targetZipDir = "d:\\ziptest\\zip"; string targetUnZipDir = "d:\\ziptest\\unzip"; string targetZipFile = "d:\\ziptest\\zip\\temp";
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var astrFileNames = Directory.GetFiles(sourceDir); if (!Directory.Exists(targetZipDir)) { Directory.CreateDirectory(targetZipDir); } var strmZipOutputStream = new ZipOutputStream(File.Create(targetZipFile)); try { strmZipOutputStream.SetLevel(9); byte[] abyBuffer = new byte[4096]; foreach (var strFile in astrFileNames) { var strmFile = File.OpenRead(strFile); try { var objZipEntry = new ZipEntry(strFile); objZipEntry.DateTime = DateTime.Now; objZipEntry.Size = strmFile.Length; strmZipOutputStream.PutNextEntry(objZipEntry); StreamUtils.Copy(strmFile, strmZipOutputStream, abyBuffer); } finally { strmFile.Close(); } } strmZipOutputStream.Finish(); } finally { strmZipOutputStream.Close(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
try { using (ZipInputStream s = new ZipInputStream(File.OpenRead(targetZipFile))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string fileName = Path.GetFileName(theEntry.Name); if (!Directory.Exists(targetUnZipDir)) { Directory.CreateDirectory(targetUnZipDir); } if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(targetUnZipDir + "\\" + Path.GetFileName(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; } } } } } } } catch (Exception ex) { throw ex; }