Link to home
Start Free TrialLog in
Avatar of shawnlehner
shawnlehnerFlag for United States of America

asked on

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.
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

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) ;
                  }
            }
Avatar of shawnlehner

ASKER

dont know that much C... could somone convert this to vb.net?

Thanks Much.
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial