|
-
Aug 10th, 2011, 09:28 AM
#1
Thread Starter
Lively Member
[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
-
Aug 10th, 2011, 10:44 AM
#2
Re: Unzip folder
You must add a reference to WindowsBase to use the System.IO.Packaging namespace.
-
Aug 10th, 2011, 10:54 AM
#3
Thread Starter
Lively Member
Re: Unzip folder
I cant find WindowsBase in Reference section to import..
am working with Vb.Net 2005
any help please..
-
Aug 10th, 2011, 11:00 AM
#4
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.
-
Aug 10th, 2011, 11:11 AM
#5
Thread Starter
Lively Member
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.
-
Aug 10th, 2011, 12:29 PM
#6
Re: Unzip folder
Well, 'F:\New Folder\Test' doesn't sound as a zip file to me.
-
Aug 10th, 2011, 12:33 PM
#7
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
-
Aug 11th, 2011, 04:17 AM
#8
Thread Starter
Lively Member
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
-
Aug 11th, 2011, 06:41 AM
#9
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
-
Aug 11th, 2011, 06:58 AM
#10
Thread Starter
Lively Member
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
-
Aug 11th, 2011, 07:10 AM
#11
Re: Unzip folder
For Each file As String In IO.Directory ...
-
Aug 11th, 2011, 07:11 AM
#12
Re: Unzip folder
 Originally Posted by abdallahr
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.
 Originally Posted by Joacim Andersson
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...
 Originally Posted by abdallahr
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
-
Aug 11th, 2011, 08:02 AM
#13
Thread Starter
Lively Member
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
-
Aug 11th, 2011, 08:21 AM
#14
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
-
Aug 11th, 2011, 08:31 AM
#15
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|