Solved

Unzip a package(with sub-folders)

Posted on 2004-04-27
9
542 Views
Last Modified: 2011-10-03
Hello,

I have the folllowing function in VB.NET using icsharp

Function Unzip(ByVal zipFile As String, ByVal UnzipToFolder As String)

        Dim nByte As Integer
        Dim s As New ICSharpCode.SharpZipLib.Zip.ZipInputStream(System.IO.File.OpenRead(zipFile))
        Dim entry As ICSharpCode.SharpZipLib.Zip.ZipEntry
        Dim Zip_Data(2048) As Byte
        Zip_Data(2048) = New Byte

        entry = s.GetNextEntry

        Do

            Dim fs As New System.IO.FileStream(System.IO.Path.Combine(UnzipToFolder, System.IO.Path.GetFileName(entry.Name)), System.IO.FileMode.Create)

            Dim bw As New System.IO.BinaryWriter(fs, System.Text.Encoding.ASCII)

            nByte = s.Read(Zip_Data, 0, Zip_Data.GetLength(0))

            While nByte > 0
                bw.Write(Zip_Data, 0, nByte)
                nByte = s.Read(Zip_Data, 0, Zip_Data.GetLength(0))
            End While

            fs.Close()
            bw.Close()
            entry = s.GetNextEntry

        Loop While Not (entry Is Nothing)

        s.Close()

    End Function

The problem with this function is that it crasehes when i have subfolders in the  zipped package. Why is that? and more importantly, HOW DO I FIX IT???
0
Comment
Question by:b_o_b
  • 4
  • 2
9 Comments
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10931700
Check the IsDirectory attribute on your zip entry, and skip this entry or create corresponding directory if it is true.
0
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10931715
Sorry, my english can be sometimes confusing.

Check the IsDirectory attribute on your zip entry, and if it is true, then skip this entry or create corresponding directory.

0
 

Author Comment

by:b_o_b
ID: 10931732
ok, I understand how to use the isdirectory and check the entry, but if it returns true, how do I create a corresponding directory?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:chaniewskim
ID: 10931796
You can use a function Directory.CreateDirectory (path As String)
0
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10931810
ex.


Directory.CreateDirectory ( System.IO.Path.Combine(UnzipToFolder, System.IO.Path.GetFileName(entry.Name)) )
0
 

Author Comment

by:b_o_b
ID: 10932015
Still not working, im getting filez unzipped, but not in the correct structure, all the files are getting unzipped in the main folders and I dont see any subfolders getting created
This is what i tried

The structure of the package is as followed

2 folders(Compenent and Scripts)
4 files
          Folder Component (1 folder(sql) and 10 files)
                      Folder Sql (1 file)
          Folder Scripts (11 folders and 30 files)
                      Folder (each of the 11 folders have 3 files)

The unzip process must keep the same structure

Here is the redone function

 Function unzip2(ByVal zipFile As String, ByVal UnzipToFolder As String)
        Dim nByte As Integer
        Dim s As New Zip.ZipInputStream(System.IO.File.OpenRead(zipFile))
        Dim entry As Zip.ZipEntry
        Dim Zip_Data(2048) As Byte
        Zip_Data(2048) = New Byte

        entry = s.GetNextEntry

        Do

            If entry.IsDirectory Then
                System.IO.Directory.CreateDirectory(System.IO.Path.Combine(UnzipToFolder, System.IO.Path.GetFileName(entry.Name)))
            Else
                Dim fs As New System.IO.FileStream(System.IO.Path.Combine(UnzipToFolder, System.IO.Path.GetFileName(entry.Name)), System.IO.FileMode.Create)

                Dim bw As New System.IO.BinaryWriter(fs, System.Text.Encoding.ASCII)

                nByte = s.Read(Zip_Data, 0, Zip_Data.GetLength(0))

                While nByte > 0
                    bw.Write(Zip_Data, 0, nByte)
                    nByte = s.Read(Zip_Data, 0, Zip_Data.GetLength(0))
                End While

                fs.Close()
                bw.Close()
            End If
            entry = s.GetNextEntry


        Loop While Not (entry Is Nothing)

        s.Close()


    End Function
 
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 11176992
It sounds like you want a recursive function, but I don't see any other call to unzip2.

Bob
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

21 Experts available now in Live!

Get 1:1 Help Now