10 out of 19 rated this helpful - Rate this topic

System.IO.Compression Namespace

Note: This namespace is new in the .NET Framework version 2.0.

The System.IO.Compression namespace contains classes that provide basic compression and decompression services for streams.

  Class Description
Public class DeflateStream Provides methods and properties for compressing and decompressing streams using the Deflate algorithm.
Public class GZipStream Provides methods and properties used to compress and decompress streams.
  Enumeration Description
Public enumeration CompressionMode Specifies whether to compress or decompress the underlying stream.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
If you want to manipulate .ZIP files
The classes in the System.IO.Compression namespace do not handle ZIP files. You can do some things with System.IO.Packaging, but it is not intuitive or easy. There's a nice, free third party library though, that will let you create, read, extract, and update ZIP files. DotNetZip.
http://DotNetZip.codeplex.com

It reads and writes regular ZIP files very easily. You can create or read ZIP files in WinForms apps, WPF apps, ASP.NET apps, mobile device apps, and more. It works with VB or C# or any .NET language. It's fully managed code. A single DLL. It supports Unicode, password protection, AES, streaming, ZIP64, file and archive comments, split/spanned archives, self-extracting archives, and more stuff. You can add or extract files by name. Or by wildcard. You can add or extract files based on file attributes, file size, or even file time. It has an easy-to-use eventing model. Full documentation in chm format. And the compression is very good. It's also very fast.

There's even a nifty little GUI tool that you can use to create zip files. It's a good replacement for WinZip - all based on the DotNetZip library.

Check it out. Free.
Another 1.* technique, using only managed .NET code

There's a GPL library for compression, implemented entirely in managed code.  I've found this library very easy to use in the past.

http://www.icsharpcode.net/OpenSource/SharpZipLib/

 

.Net 1.1 Compression Technique.

This is a method that will compress a directory of files using the ShellClass and the Shell32 api.

    Public Sub startCompression(ByVal stateInfo As Object)
 
      Dim strArgs As String = ""
      Dim strdest As String = ""

      '----- Check the file for a .zip extenstion.
      strdest += strDestinationFile
      If System.IO.Path.GetExtension(strdest).ToUpper.EndsWith(".ZIP") = False Then
        strdest += ".zip"
      End If
      If strdest.IndexOf(" ") <> -1 Then
        strdest = """" & strdest & """"
      End If
 
      '----- Binary Array representing a zip file header.
      Dim emptyZip As Byte() = New Byte() {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
 
      '----- Write out the zip file
      Dim fs As FileStream = File.Create(Me.strDestinationFile)
      fs.Write(emptyZip, 0, emptyZip.Length)
      fs.Flush()
      fs.Close()
      fs = Nothing
      
 
      Dim sc As ShellClass = New ShellClass
      Dim src As Shell32.Folder = sc.NameSpace(Me.strSourceDir)
      Dim dst As Shell32.Folder = sc.NameSpace(Me.strDestinationFile)
      '----- Begining File Compression in System Controlled Thread
      dst.CopyHere(src, 20)
     
      '----- Sleep to allow the compression thread to begin execution
      System.Threading.Thread.CurrentThread.Sleep(5000) ' 5 seconds is usually more than enough
    End Sub