?
Solved

simple SharpZipLib unzip function

Posted on 2005-05-07
3
Medium Priority
?
1,146 Views
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.
0
Comment
Question by:shawnlehner
  • 2
3 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13951177
heres one I use in C# converting to VB.NET should  be rather simple.

Greg

            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) {

                              Console.WriteLine(theEntry.Name);
                  
                              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 {
                                                break;
                                          }
                                    }
                        
                                    streamWriter.Close();
                              }

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

                  }
                  finally {
                        zipStream.Close();

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

Author Comment

by:shawnlehner
ID: 13954245
dont know that much C... could somone convert this to vb.net?

Thanks Much.
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 13954721

    Public Sub Extract(ByVal _Filename As String, ByVal _TargetDir As String)
        Dim zipStream As ZipInputStream = Nothing
        Try
            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()
                Directory.SetCurrentDirectory(_TargetDir)
                Directory.CreateDirectory(directoryName)
                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)
                        Else
                            Exit While
                        End If
                    End While
                    streamWriter.Close()
                End If
                Directory.SetCurrentDirectory(cdir)
                theEntry = zipStream.GetNextEntry()
            End While
        Catch ex As Exception
            Throw New System.Exception("Unable to extract zip", ex)
        Finally
            zipStream.Close()
            File.Delete(_Filename)
        End Try
    End Sub


should be pretty close ...

Greg
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

864 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