Results 1 to 15 of 15

Thread: [RESOLVED] Unzip folder

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    73

    Resolved [RESOLVED] Unzip folder

    Hi

    I have the following error on the code, am working with Vb.Net 2005;
    am trying to unzip zip files within a folder..

    Any help please..

    Many thanks

    The errors state:

    - Type 'Package' is not defined
    - Name 'ZipPacakage' is not defined
    - Type 'ZipPackagePart' is not defined
    - Type 'ZipPackagePage' is not defined


    Code:
    Public Sub UnzipArchive(ByVal zipFile As String, ByVal UnzipLocation As String)
    
            If (Not zipFile Is Nothing And Not UnzipLocation Is Nothing) Then
    
                Dim zipFilePackage As Package
                zipFilePackage = ZipPackage.Open(zipFile, FileMode.Open, FileAccess.ReadWrite)
    
                ' Iterate through all the files in the collection 
                For Each contentFile As ZipPackagePart In zipFilePackage.GetParts()
    
                    CreateFile(UnzipLocation, contentFile)
    
                Next
    
                zipFilePackage.Close()
    
                MessageBox.Show("Unzip Complete")
    
            End If
    
        End Sub
    Code:
       Private Sub CreateFile(ByVal UnzipLocation As String, ByVal contentFile As ZipPackagePage)
    
            Dim ContentFilePath As String = String.Empty
            ContentFilePath = contentFile.Uri.OriginalString.Replace("/", Path.DirectorySeparatorChar)
    
            If (ContentFilePath.StartsWith(Path.DirectorySeparatorChar.ToString())) Then
    
                ContentFilePath = ContentFilePath.TrimStart(Path.DirectorySeparatorChar)
            End If
    
            ContentFilePath = Path.Combine(UnzipLocation, ContentFilePath)
    
            ' Check if folder already exists. If it does, delete it and create it. Otherwise, just create it 
            ' If directory exists = true 
            If (Directory.Exists(Path.GetDirectoryName(ContentFilePath)) = True) Then
                ' Delete directory and all files in it. Second parameter is a boolean for a recursive delete 
                ' It will delete all files in the folder and the folder. 
                Directory.Delete(Path.GetDirectoryName(ContentFilePath), True)
                Directory.CreateDirectory(Path.GetDirectoryName(ContentFilePath)) ' re-create the folder with no files in it 
            Else
                Directory.CreateDirectory(Path.GetDirectoryName(ContentFilePath))
            End If
    
            Dim NewFileStream As FileStream = File.Create(ContentFilePath)
            NewFileStream.Close()
    
            Dim content As Byte() = New Byte(contentFile.GetStream().Length - 1) {}
    
            contentFile.GetStream().Read(content, 0, content.Length)
            File.WriteAllBytes(ContentFilePath, content)
    
        End Sub

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Unzip folder

    You must add a reference to WindowsBase to use the System.IO.Packaging namespace.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    73

    Re: Unzip folder

    I cant find WindowsBase in Reference section to import..
    am working with Vb.Net 2005

    any help please..

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Unzip folder

    That explains it. It was added in .Net 3.0 so it doesn't exist in 2.0. What you can do is to download ICSharpCode.SharpZipLib and use that instead. Of course it's a different component so you need to change your code.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    73

    Re: Unzip folder

    Ok fine.. managed to download it succesfully and receive no errors..

    However, at run time when I call UnzipArchive function by clicking the sub below;

    Code:
      Private Sub btnUnpackFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnpackFolder.Click
    
            UnzipArchive(Label1.Text, Label2.Text)
    
        End Sub

    I received the error ;

    UnauthorizedAccssException was unhandled
    Access to the path 'F:\New Folder\Test' is denied

    This path do exists..

    Note:

    Label1: 'F:\New Folder\Test'
    Label2: 'F:\New Folder\Test'

    Any help please
    Last edited by abdallahr; Aug 10th, 2011 at 11:26 AM.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Unzip folder

    Well, 'F:\New Folder\Test' doesn't sound as a zip file to me.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Unzip folder

    Before continuing with this thread, I suggest people read this thread for some background paying particular attention to post #10 where he explains what he's trying to do.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    73

    Re: Unzip folder

    Thanks techgnome..

    Am not rying to unzip a file, am trying to unzip 100 zip files within a folder. Then copy the unzipped files to another folder..

    Hope am clearer now...any assistance...

    Thanks

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Unzip folder

    You have to loop through each zip file and unzip them.
    Code:
    For Each file In IO.Directory.GetFiles(Label1.Text, "*.zip")
        UnzipArchive(file, Label2.Text)
    Next

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    73

    Re: Unzip folder

    I added in the btnclick as shown below but I receive the error;

    Code:
      Private Sub btnUnpackFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnpackFolder.Click
            For Each File In IO.Directory.GetFiles(Label1.Text, "*.zip")
                UnzipArchive(File, Label2.Text)
            Next
        End Sub
    'File' is a type and cannot be used as an expression

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Unzip folder

    For Each file As String In IO.Directory ...

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Unzip folder

    Quote Originally Posted by abdallahr View Post
    Thanks techgnome..

    Am not rying to unzip a file, am trying to unzip 100 zip files within a folder. Then copy the unzipped files to another folder..

    Hope am clearer now...any assistance...

    Thanks
    That's what you said in the other thread too... you were clear... apparently I was not... Specifically stated that you would need to get a list of all the zip files in the folder... there is no such thing as a zip folder... so right now... clear your head. Smack yourself around a few times and get the idea of unzipping a folder out of your head... go ahead... we'll wait. Now... re-read what you wrote: " unzip 100 zip files within a folder" ... even you'll admit that what you are doing is unzipping a file... the fact that you need to do it 100 times is a minor detail.

    Quote Originally Posted by Joacim Andersson View Post
    You have to loop through each zip file and unzip them.
    Code:
    For Each file In IO.Directory.GetFiles(Label1.Text, "*.zip")
        UnzipArchive(file, Label2.Text)
    Next
    Which is exactly what I told him the first time through...

    Quote Originally Posted by abdallahr View Post
    I added in the btnclick as shown below but I receive the error;

    Code:
      Private Sub btnUnpackFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnpackFolder.Click
            For Each File In IO.Directory.GetFiles(Label1.Text, "*.zip")
                UnzipArchive(File, Label2.Text)
            Next
        End Sub
    'File' is a type and cannot be used as an expression
    This is what happens when people copy code and try to use it w/o understanding what they are doing. Especially when the debug skills are minimal.

    Yes, "file" is a type... so you can't use it as a variable name...
    So look for where File is used:
    Code:
      Private Sub btnUnpackFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnpackFolder.Click
            For Each File In IO.Directory.GetFiles(Label1.Text, "*.zip")
                UnzipArchive(File, Label2.Text)
            Next
        End Sub
    and change the name of it...
    Code:
      Private Sub btnUnpackFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnpackFolder.Click
            For Each myFile In IO.Directory.GetFiles(Label1.Text, "*.zip")
                UnzipArchive(myFile, Label2.Text)
            Next
        End Sub
    now, to make it even better, myFile should have a type... but the IDE will catch that because you have option strict on.... right?

    Code:
      Private Sub btnUnpackFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnpackFolder.Click
            For Each myFile as String In IO.Directory.GetFiles(Label1.Text, "*.zip")
                UnzipArchive(myFile, Label2.Text)
            Next
        End Sub
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    73

    Re: Unzip folder

    woow, thats so kind of you all.. Very nice explanation...Thank you

    Just one more last question;

    The unzip code I have which works perfectly is

    Code:
    Private Sub unzip(ByVal filename As String, ByVal targetdir As String, ByVal overwrite As Boolean, Optional ByVal password As String = "")
            Dim inputStrm As New ZipInputStream(File.OpenRead(filename))
            inputStrm.Password = password
            Dim nextEntry As ZipEntry = inputStrm.GetNextEntry()
            'loop through every file in zip
            While Not nextEntry Is Nothing
                'if no slash at end of nextentry.name, file isn't a directory
                If Not nextEntry.Name.LastIndexOf("/") = nextEntry.Name.Length - 1 Then
                    'checks to make SURE the directory exists, sometimes they arent specified prior to their contents
                    If nextEntry.Name.IndexOf("/") > 0 Then
                        If Not Directory.Exists(targetdir & "\" & nextEntry.Name.Replace("/", "\").Substring(0, nextEntry.Name.Replace("/", "\").LastIndexOf("\"))) Then
                            Directory.CreateDirectory(targetdir & "\" & nextEntry.Name.Replace("/", "\").Substring(0, nextEntry.Name.Replace("/", "\").LastIndexOf("\")))
                        End If
                    End If
                    Dim tmpStrm As FileStream
                    Dim tmpBuffer(2048) As Byte
                    Dim tmpLength As Integer = -1
    
                    If overwrite = True Then
                        tmpStrm = New FileStream(Path.Combine(targetdir, nextEntry.Name), FileMode.Create)
                    Else
                        tmpStrm = New FileStream(Path.Combine(targetdir, nextEntry.Name), FileMode.CreateNew)
                    End If
    
                    While True
                        tmpLength = inputStrm.Read(tmpBuffer, 0, tmpBuffer.Length)
                        If tmpLength > 0 Then
                            tmpStrm.Write(tmpBuffer, 0, tmpLength)
                        Else
                            Exit While
                        End If
                    End While
    
                    tmpStrm.Flush()
                    tmpStrm.Close()
    
                    nextEntry = inputStrm.GetNextEntry()
                Else
                    'else, is a directory... createdirectory ensures directory exists
                    Directory.CreateDirectory(targetdir & "\" & nextEntry.Name.Replace("/", "\"))
                    nextEntry = inputStrm.GetNextEntry()
                End If
            End While
    
        End Sub
    So, I changed the btnUnpackFolder Click Code to:

    Code:
     Private Sub btnUnpackFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnpackFolder.Click
            For Each myFile as String In IO.Directory.GetFiles(Label1.Text, "*.zip")
                Unzip(myFile, Label2.Text)
            Next
        End Sub
    Error:

    Argument not specified for parameter 'overwrite' of 'Private Sub unzip (filename As String, targetdir As String, overwrite As Boolean, [password as String = ''])'.

    Any help please

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Unzip folder

    ok... this goes back to the problem in your other thread... the sub UnZip expects THREE parameters... and you're only passing it two. you need to pass in the third parameter as well.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2011
    Posts
    73

    Re: Unzip folder

    Thank you guys!!

    Resolved...
    Last edited by abdallahr; Aug 11th, 2011 at 08:40 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width