simple SharpZipLib unzip function

Posted on 2005-05-07
Last Modified: 2012-08-14
I need a simple function using the free SharpZipLib.dll that does the following.

Unzips a single file at a time from the archive (looping through each file unzipping one at a time) so that I can run a function on each file as it is decompressed.
Question by:shawnlehner
    LVL 37

    Expert Comment

    heres one I use in C# converting to VB.NET should  be rather simple.


                public void Extract(string _filename, string _targetDir) {

                      ZipInputStream zipStream = null;

                      try {

                            this.OnProcessMessage(this, "Extracting Downloaded Zip File");

                            zipStream = new ZipInputStream(File.OpenRead(_filename));
                            ZipEntry theEntry;

                            while ((theEntry = zipStream.GetNextEntry()) != null) {

                                  string directoryName = Path.GetDirectoryName(theEntry.Name);
                                  string fileName      = Path.GetFileName(theEntry.Name);
                                  // create directory
                                  string cdir = Directory.GetCurrentDirectory() ;
                                  Directory.SetCurrentDirectory(_targetDir) ;
                                  Directory.CreateDirectory(directoryName) ;
                                  if (fileName != String.Empty) {
                                        this.OnProcessMessage(this, "Extracting file: " + theEntry.Name);
                                        FileStream streamWriter = File.Create(theEntry.Name);
                                        int size = 2048;
                                        byte[] data = new byte[2048];
                                        while (true) {
                                              size = zipStream.Read(data, 0, data.Length);
                                              if (size > 0) {
                                                    streamWriter.Write(data, 0, size);
                                              } else {

                                  Directory.SetCurrentDirectory(cdir) ;
                      catch(System.Exception ex) {
                            throw new Exception("Unable to Extract Zip!",ex) ;

                      finally {

                            this.OnProcessMessage(this, "Deleting Downloaded Zip File");
                            File.Delete(_filename) ;
    LVL 4

    Author Comment

    dont know that much C... could somone convert this to

    Thanks Much.
    LVL 37

    Accepted Solution


        Public Sub Extract(ByVal _Filename As String, ByVal _TargetDir As String)
            Dim zipStream As ZipInputStream = Nothing
                zipStream = New ZipInputStream(File.OpenRead(_Filename))
                Dim theEntry As ZipEntry
                theEntry = zipStream.GetNextExtry()
                While Not theEntry Is Nothing
                    Dim directoryName As String = Path.GetDirectoryName(theEntry.Name)
                    Dim filename As String = Path.GetFileName(theEntry.Name)
                    Dim cdir As String = Directory.GetCurrentDirectory()
                    If filename <> String.Empty Then
                        Dim streamWriter As FileStream = File.Create(theEntry.Name)
                        Dim size As Integer = 2048
                        Dim data(size) As Byte
                        While True
                            size = zipStream.Read(data, 0, data.Length)
                            If size > 0 Then
                                streamWriter.Write(data, 0, size)
                                Exit While
                            End If
                        End While
                    End If
                    theEntry = zipStream.GetNextEntry()
                End While
            Catch ex As Exception
                Throw New System.Exception("Unable to extract zip", ex)
            End Try
        End Sub

    should be pretty close ...


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now