943,597 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 2251
  • VB.NET RSS
Jul 25th, 2008
0

Parsing Zip Files VB.Net

Expand Post »
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)
  1. Sub Parse(ByVal ThisFolder As String)
  2. FSO = New Scripting.FileSystemObject
  3. Dim soFolder As Folder
  4. Dim soFile As File
  5. Dim soFolders As Folders
  6.  
  7.  
  8. soFolder = FSO.GetFolder(ThisFolder)
  9.  
  10. ' Files in this directory
  11.  
  12. For Each soFile In soFolder.Files
  13. ' TestdbDataSet.Table1.Rows.Add(CStr(ThisFolder), CStr(soFile.Name), CStr(soFile.Type), CStr(soFile.Size))
  14. Table1TableAdapter.Insert(CStr(ThisFolder), CStr(soFile.Name), CStr(soFile.Type), CStr(soFile.Size))
  15. Next soFile
  16.  
  17. ' Recurse Subdirectories
  18. soFolders = soFolder.SubFolders ' parses subfolders
  19.  
  20. For Each soFolder In soFolders
  21. Parse(soFolder.Path)
  22. Next soFolder
  23.  
  24. soFolders = Nothing
  25. soFolder = Nothing
  26. soFile = Nothing
  27.  
  28.  
  29. End Sub
Last edited by cellus205; Jul 25th, 2008 at 1:02 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Jul 25th, 2008
0

Re: Parsing Zip Files VB.Net

Hi,
Take a look on this library DotNetZip.

Hope it helps.
Reputation Points: 18
Solved Threads: 7
Junior Poster in Training
dmf1978 is offline Offline
51 posts
since Aug 2006
Jul 28th, 2008
0

Re: Parsing Zip Files VB.Net

Is this library able to just list the files? I was reading over it and mostly only saw that it zipped and unzipped files, etc.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Jul 31st, 2008
0

Re: Parsing Zip Files VB.Net

bump
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Aug 1st, 2008
0

Re: Parsing Zip Files VB.Net

Can anyone help me convert this VB6 code to get zip filenames to .Net?

VB Syntax (Toggle Plain Text)
  1. Option Explicit
  2.  
  3. Const strStartPath = "C:\"
  4. Const strDestFile = "c:\output.txt"
  5. Const strDestTargetFile = "c:\outputTarget.txt"
  6.  
  7. Dim dicList
  8. dicList = CreateObject("Scripting.Dictionary")
  9. dicList.Add(LCase("mp3"), "")
  10. dicList.Add(LCase("avi"), "")
  11. dicList.Add(LCase("wav"), "")
  12.  
  13.  
  14.  
  15. Dim objFSO, strTempFolder, objDestFile, objDestTargetFile
  16. objFSO = CreateObject("Scripting.FileSystemObject")
  17. strTempFolder = CreateTempFolder()
  18.  
  19. Dim strDest, strDestTarget
  20. strDest = ""
  21. strDestTarget = ""
  22.  
  23. If objFSO.FileExists(strDestFile) Then objFSO.DeleteFile(strDestFile)
  24. If objFSO.FileExists(strDestTargetFile) Then objFSO.DeleteFile(strDestTargetFile)
  25.  
  26. Call TraverseFolder(strStartPath)
  27.  
  28. If objFSO.FolderExists(strTempFolder) Then objFSO.DeleteFolder(strTempFolder)
  29.  
  30.  
  31.  
  32. Sub TraverseFolder(ByVal strFolderPath)
  33. Dim objCurrentFolder, objFile, objFolder
  34. objCurrentFolder = objFSO.GetFolder(strFolderPath)
  35.  
  36. On Error Resume Next
  37.  
  38. For Each objFile In objCurrentFolder.Files
  39. If Not Err() Then
  40. If LCase(objFSO.GetExtensionName(objFile)) = "zip" Then
  41. Call UnZipAndCheckExtension(objFile, strTempFolder)
  42. End If
  43. Else
  44. Err.Clear()
  45. End If
  46. Next
  47.  
  48. For Each objFolder In objCurrentFolder.subFolders
  49. If Not Err() Then
  50. Call TraverseFolder(objFolder.ParentFolder & "\" & objFolder.name)
  51. Else
  52. Err.Clear()
  53. End If
  54. Next
  55.  
  56. On Error GoTo 0
  57. End Sub
  58.  
  59.  
  60. Function UnZipAndCheckExtension(ByVal strZipFile, ByVal strTempFolder)
  61. Const strUNZIPSource = "WZUNZIP.EXE"
  62.  
  63. Dim objFSO, objShell, intRet
  64. objFSO = CreateObject("Scripting.FileSystemObject")
  65. If Not objFSO.FolderExists(strTempFolder) Then objFSO.CreateFolder(strTempFolder)
  66. objShell = CreateObject("WScript.shell")
  67.  
  68. On Error Resume Next
  69. intRet = objShell.Run("""" & strUNZIPSource & """ -o """ & strZipFile & """ """ & strTempFolder & """", _
  70. 0, True)
  71. If Err() Then
  72. On Error GoTo 0
  73. MsgBox(strZipFile & vbLf & Err.Description)
  74. Exit Function
  75. End If
  76. On Error GoTo 0
  77.  
  78. Dim objFolder, objFile
  79. objFolder = objFSO.GetFolder(strTempFolder)
  80.  
  81. strDest = "The following files are found in '" & strZipFile & "':" & vbLf
  82. strDestTarget = "The following special files are found in '" & strZipFile & "':" & vbLf
  83.  
  84. For Each objFile In objFolder.Files
  85. strDest = strDest & "->" & objFSO.GetFileName(objFile) & vbLf
  86.  
  87. If dicList.Exists(LCase(objFSO.GetExtensionName(objFile))) Then
  88. strDestTarget = strDestTarget & "->" & objFSO.GetFileName(objFile) & vbLf
  89. End If
  90.  
  91. objFSO.DeleteFile(objFile)
  92. Next
  93.  
  94. objDestFile = objFSO.OpenTextFile(strDestFile, 8, True)
  95. objDestFile.Write(strDest)
  96. objDestFile.Close()
  97.  
  98. objDestTargetFile = objFSO.OpenTextFile(strDestTargetFile, 8, True)
  99. objDestTargetFile.Write(strDestTarget)
  100. objDestTargetFile.Close()
  101. End Function
  102.  
  103.  
  104. Function CreateTempFolder()
  105. Dim objFSO, strTempFile
  106. objFSO = CreateObject("Scripting.FileSystemObject")
  107. strTempFile = objFSO.GetTempName
  108. strTempFile = Replace(strTempFile, "." & objFSO.GetExtensionName(strTempFile), "")
  109.  
  110. Dim objShell
  111. objShell = CreateObject("WScript.Shell")
  112. CreateTempFolder = Replace(objShell.SpecialFolders("Desktop"), "Desktop", "Local Settings\Temp") & _
  113. "\" & strTempFile
  114. End Function
  115. End Sub
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Aug 1st, 2008
0

Re: Parsing Zip Files VB.Net

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jask Azel is offline Offline
1 posts
since Jul 2008
Aug 1st, 2008
0

Re: Parsing Zip Files VB.Net

Hmm, getting a directory not found error when it gets here:

VB Syntax (Toggle Plain Text)
  1. objFolder = objFSO.GetFolder(strTempFolder)
  2. strDest = "The following files are found in '" & strZipFile & "':" & vbLf
  3. strDestTarget = "The following special files are found in '" & strZipFile & "':" & vbLf
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Find Com Port
Next Thread in VB.NET Forum Timeline: Instant Messenger - Please Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC