Parsing Zip Files VB.Net
Please support our VB.NET advertiser: DiscountASP.NET – 3 Months Free on VB.NET Web Hosting
![]() |
•
•
Posts: 45
Reputation:
Solved Threads: 1
Hows it going everyone. Right now I am working on a project with VS2008 that parses directory and subdirectories and lists all filenames/paths/size/etc. This is working correctly, but I also need to parse the contents of zip files without actually unzipping them. What would be the easiest method to return the information on the files in a zip file? (Filename, Size, etc). Heres the code that I currently have to parse directories and subdirectories.
VB Syntax (Toggle Plain Text)
Sub Parse(ByVal ThisFolder As String) FSO = New Scripting.FileSystemObject Dim soFolder As Folder Dim soFile As File Dim soFolders As Folders soFolder = FSO.GetFolder(ThisFolder) ' Files in this directory For Each soFile In soFolder.Files ' TestdbDataSet.Table1.Rows.Add(CStr(ThisFolder), CStr(soFile.Name), CStr(soFile.Type), CStr(soFile.Size)) Table1TableAdapter.Insert(CStr(ThisFolder), CStr(soFile.Name), CStr(soFile.Type), CStr(soFile.Size)) Next soFile ' Recurse Subdirectories soFolders = soFolder.SubFolders ' parses subfolders For Each soFolder In soFolders Parse(soFolder.Path) Next soFolder soFolders = Nothing soFolder = Nothing soFile = Nothing End Sub
Last edited by cellus205 : Jul 25th, 2008 at 12:02 pm.
•
•
Posts: 45
Reputation:
Solved Threads: 1
Can anyone help me convert this VB6 code to get zip filenames to .Net?
VB Syntax (Toggle Plain Text)
Option Explicit Const strStartPath = "C:\" Const strDestFile = "c:\output.txt" Const strDestTargetFile = "c:\outputTarget.txt" Dim dicList dicList = CreateObject("Scripting.Dictionary") dicList.Add(LCase("mp3"), "") dicList.Add(LCase("avi"), "") dicList.Add(LCase("wav"), "") Dim objFSO, strTempFolder, objDestFile, objDestTargetFile objFSO = CreateObject("Scripting.FileSystemObject") strTempFolder = CreateTempFolder() Dim strDest, strDestTarget strDest = "" strDestTarget = "" If objFSO.FileExists(strDestFile) Then objFSO.DeleteFile(strDestFile) If objFSO.FileExists(strDestTargetFile) Then objFSO.DeleteFile(strDestTargetFile) Call TraverseFolder(strStartPath) If objFSO.FolderExists(strTempFolder) Then objFSO.DeleteFolder(strTempFolder) Sub TraverseFolder(ByVal strFolderPath) Dim objCurrentFolder, objFile, objFolder objCurrentFolder = objFSO.GetFolder(strFolderPath) On Error Resume Next For Each objFile In objCurrentFolder.Files If Not Err() Then If LCase(objFSO.GetExtensionName(objFile)) = "zip" Then Call UnZipAndCheckExtension(objFile, strTempFolder) End If Else Err.Clear() End If Next For Each objFolder In objCurrentFolder.subFolders If Not Err() Then Call TraverseFolder(objFolder.ParentFolder & "\" & objFolder.name) Else Err.Clear() End If Next On Error GoTo 0 End Sub Function UnZipAndCheckExtension(ByVal strZipFile, ByVal strTempFolder) Const strUNZIPSource = "WZUNZIP.EXE" Dim objFSO, objShell, intRet objFSO = CreateObject("Scripting.FileSystemObject") If Not objFSO.FolderExists(strTempFolder) Then objFSO.CreateFolder(strTempFolder) objShell = CreateObject("WScript.shell") On Error Resume Next intRet = objShell.Run("""" & strUNZIPSource & """ -o """ & strZipFile & """ """ & strTempFolder & """", _ 0, True) If Err() Then On Error GoTo 0 MsgBox(strZipFile & vbLf & Err.Description) Exit Function End If On Error GoTo 0 Dim objFolder, objFile objFolder = objFSO.GetFolder(strTempFolder) strDest = "The following files are found in '" & strZipFile & "':" & vbLf strDestTarget = "The following special files are found in '" & strZipFile & "':" & vbLf For Each objFile In objFolder.Files strDest = strDest & "->" & objFSO.GetFileName(objFile) & vbLf If dicList.Exists(LCase(objFSO.GetExtensionName(objFile))) Then strDestTarget = strDestTarget & "->" & objFSO.GetFileName(objFile) & vbLf End If objFSO.DeleteFile(objFile) Next objDestFile = objFSO.OpenTextFile(strDestFile, 8, True) objDestFile.Write(strDest) objDestFile.Close() objDestTargetFile = objFSO.OpenTextFile(strDestTargetFile, 8, True) objDestTargetFile.Write(strDestTarget) objDestTargetFile.Close() End Function Function CreateTempFolder() Dim objFSO, strTempFile objFSO = CreateObject("Scripting.FileSystemObject") strTempFile = objFSO.GetTempName strTempFile = Replace(strTempFile, "." & objFSO.GetExtensionName(strTempFile), "") Dim objShell objShell = CreateObject("WScript.Shell") CreateTempFolder = Replace(objShell.SpecialFolders("Desktop"), "Desktop", "Local Settings\Temp") & _ "\" & strTempFile End Function End Sub
Dont know if it worked but I had a go. THis is pretty much the same code in vb.net without errors.
Public Class Form1
Private Const strStartPath As String = "C:\"
Private Const strDestFile As String = "c:\output.txt"
Private Const strDestTargetFile As String = "c:\outputTarget.txt"
Private dicList As Object
Private objFSO As Object
Private strZipFile As String
Private Sub doit()
dicList = CreateObject("Scripting.Dictionary")
dicList.Add(LCase("mp3"), "")
dicList.Add(LCase("avi"), "")
dicList.Add(LCase("wav"), "")
Dim strTempFolder As String, objDestFile As Object, objDestTargetFile As Object
objFSO = CreateObject("Scripting.FileSystemObject")
strTempFolder = CreateTempFolder()
Dim strDest As String, strDestTarget As String
strDest = ""
strDestTarget = ""
If objFSO.FileExists(strDestFile) Then objFSO.DeleteFile(strDestFile)
If objFSO.FileExists(strDestTargetFile) Then objFSO.DeleteFile(strDestTargetFile)
Call TraverseFolder(strStartPath, strTempFolder)
If objFSO.FolderExists(strTempFolder) Then
objFSO.DeleteFolder(strTempFolder)
End If
Dim objFolder As Object, objFile As Object
objFolder = objFSO.GetFolder(strTempFolder)
strDest = "The following files are found in '" & strZipFile & "':" & vbLf
strDestTarget = "The following special files are found in '" & strZipFile & "':" & vbLf
For Each objFile In objFolder.Files
strDest = strDest & "->" & objFSO.GetFileName(objFile) & vbLf
If dicList.Items.Exists(LCase(objFSO.GetExtensionName(objFile))) Then
strDestTarget = strDestTarget & "->" & objFSO.GetFileName(objFile) & vbLf
End If
objFSO.DeleteFile(objFile)
Next
objDestFile = objFSO.OpenTextFile(strDestFile, 8, True)
objDestFile.Write(strDest)
objDestFile.Close()
objDestTargetFile = objFSO.OpenTextFile(strDestTargetFile, 8, True)
objDestTargetFile.Write(strDestTarget)
objDestTargetFile.Close()
End Sub
Function CreateTempFolder() As Object
Dim objFSO As Object, strTempFile As String
objFSO = CreateObject("Scripting.FileSystemObject")
strTempFile = objFSO.GetTempName
strTempFile = Replace(strTempFile, "." & objFSO.GetExtensionName(strTempFile), "")
Dim objShell As Object
objShell = CreateObject("WScript.Shell")
CreateTempFolder = Replace(objShell.SpecialFolders("Desktop"), "Desktop", "Local Settings\Temp") & "\" & strTempFile
' Return Nothing
End Function
Private Sub TraverseFolder(ByVal strFolderPath As String, ByVal strtempfolder As String)
Dim objCurrentFolder As Object, objFile As Object, objFolder As Object
objCurrentFolder = objFSO.GetFolder(strFolderPath)
On Error Resume Next
For Each objFile In objCurrentFolder.Files
If LCase(objFSO.GetExtensionName(objFile)) = "zip" Then
Call UnZipAndCheckExtension(objFile, strtempfolder)
End If
Next
For Each objFolder In objCurrentFolder.subFolders
Call TraverseFolder(objFolder.ParentFolder & "\" & objFolder.name, strtempfolder)
Next
End Sub
Function UnZipAndCheckExtension(ByVal strZipFile As String, ByVal strTempFolder As String) As Object
Const strUNZIPSource As String = "WZUNZIP.EXE"
Dim objFSO As Object, objShell As Object, intRet As Integer
objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strTempFolder) Then
objFSO.CreateFolder(strTempFolder)
objShell = CreateObject("WScript.shell")
On Error Resume Next
intRet = objShell.Run("""" & strUNZIPSource & """ -o """ & strZipFile & """ """ & strTempFolder & """", 0, True)
End If
Return Nothing
End Function
End Class
Public Class Form1
Private Const strStartPath As String = "C:\"
Private Const strDestFile As String = "c:\output.txt"
Private Const strDestTargetFile As String = "c:\outputTarget.txt"
Private dicList As Object
Private objFSO As Object
Private strZipFile As String
Private Sub doit()
dicList = CreateObject("Scripting.Dictionary")
dicList.Add(LCase("mp3"), "")
dicList.Add(LCase("avi"), "")
dicList.Add(LCase("wav"), "")
Dim strTempFolder As String, objDestFile As Object, objDestTargetFile As Object
objFSO = CreateObject("Scripting.FileSystemObject")
strTempFolder = CreateTempFolder()
Dim strDest As String, strDestTarget As String
strDest = ""
strDestTarget = ""
If objFSO.FileExists(strDestFile) Then objFSO.DeleteFile(strDestFile)
If objFSO.FileExists(strDestTargetFile) Then objFSO.DeleteFile(strDestTargetFile)
Call TraverseFolder(strStartPath, strTempFolder)
If objFSO.FolderExists(strTempFolder) Then
objFSO.DeleteFolder(strTempFolder)
End If
Dim objFolder As Object, objFile As Object
objFolder = objFSO.GetFolder(strTempFolder)
strDest = "The following files are found in '" & strZipFile & "':" & vbLf
strDestTarget = "The following special files are found in '" & strZipFile & "':" & vbLf
For Each objFile In objFolder.Files
strDest = strDest & "->" & objFSO.GetFileName(objFile) & vbLf
If dicList.Items.Exists(LCase(objFSO.GetExtensionName(objFile))) Then
strDestTarget = strDestTarget & "->" & objFSO.GetFileName(objFile) & vbLf
End If
objFSO.DeleteFile(objFile)
Next
objDestFile = objFSO.OpenTextFile(strDestFile, 8, True)
objDestFile.Write(strDest)
objDestFile.Close()
objDestTargetFile = objFSO.OpenTextFile(strDestTargetFile, 8, True)
objDestTargetFile.Write(strDestTarget)
objDestTargetFile.Close()
End Sub
Function CreateTempFolder() As Object
Dim objFSO As Object, strTempFile As String
objFSO = CreateObject("Scripting.FileSystemObject")
strTempFile = objFSO.GetTempName
strTempFile = Replace(strTempFile, "." & objFSO.GetExtensionName(strTempFile), "")
Dim objShell As Object
objShell = CreateObject("WScript.Shell")
CreateTempFolder = Replace(objShell.SpecialFolders("Desktop"), "Desktop", "Local Settings\Temp") & "\" & strTempFile
' Return Nothing
End Function
Private Sub TraverseFolder(ByVal strFolderPath As String, ByVal strtempfolder As String)
Dim objCurrentFolder As Object, objFile As Object, objFolder As Object
objCurrentFolder = objFSO.GetFolder(strFolderPath)
On Error Resume Next
For Each objFile In objCurrentFolder.Files
If LCase(objFSO.GetExtensionName(objFile)) = "zip" Then
Call UnZipAndCheckExtension(objFile, strtempfolder)
End If
Next
For Each objFolder In objCurrentFolder.subFolders
Call TraverseFolder(objFolder.ParentFolder & "\" & objFolder.name, strtempfolder)
Next
End Sub
Function UnZipAndCheckExtension(ByVal strZipFile As String, ByVal strTempFolder As String) As Object
Const strUNZIPSource As String = "WZUNZIP.EXE"
Dim objFSO As Object, objShell As Object, intRet As Integer
objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strTempFolder) Then
objFSO.CreateFolder(strTempFolder)
objShell = CreateObject("WScript.shell")
On Error Resume Next
intRet = objShell.Run("""" & strUNZIPSource & """ -o """ & strZipFile & """ """ & strTempFolder & """", 0, True)
End If
Return Nothing
End Function
End Class
•
•
Posts: 45
Reputation:
Solved Threads: 1
Hmm, getting a directory not found error when it gets here:
VB Syntax (Toggle Plain Text)
objFolder = objFSO.GetFolder(strTempFolder) strDest = "The following files are found in '" & strZipFile & "':" & vbLf strDestTarget = "The following special files are found in '" & strZipFile & "':" & vbLf
![]() |
Similar Threads
Other Threads in the VB.NET Forum
- Cannot find server or DNS Error - please help! (Viruses, Spyware and other Nasties)
- Open In New Window Php (PHP)
- Short guide to include RSS on your website (PHP)
- another newbie with alot of redhat and apache server Q'S (Linux Servers and Apache)
Other Threads in the VB.NET Forum
- Previous Thread: Find Com Port
- Next Thread: Instant Messenger - Please Help
•
•
•
•
Views: 1119 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode