• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1154
  • Last Modified:

simple SharpZipLib unzip function

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
shawnlehner
Asked:
shawnlehner
  • 2
1 Solution
 
gregoryyoungCommented:
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
 
shawnlehnerAuthor Commented:
dont know that much C... could somone convert this to vb.net?

Thanks Much.
0
 
gregoryyoungCommented:

    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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now