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.

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

Hi,
Take a look on this library DotNetZip.

Hope it helps.

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.

bump

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

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

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

objFolder = objFSO.GetFolder(strTempFolder)
strDest = "The following files are found in '" & strZipFile & "':" & vbLf
strDestTarget = "The following special files are found in '" & strZipFile & "':" & vbLf
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.