使用DotNetZip检查zip文件

时间:2013-03-29 20:14:56

标签: vb.net zip dotnetzip

所以我有两个需要帮助的功能。

Public Function checkZipForFiles(ByVal zipFilepath As String)
    Try
        Dim doc As New System.Xml.XmlDocument
        If My.Computer.FileSystem.FileExists("Backup.xml") Then
            doc.Load("Backup.xml")

            Dim JobNodes As XmlNodeList = doc.GetElementsByTagName("Job")
            For Each JobNode In JobNodes
                Dim Source = JobNode.SelectNodes("Source")

                For Each item As System.Xml.XmlNode In Source
                    For Each File In checkFilesInFolder(item.InnerText)
                        Using zip = ZipFile.Read(zipFilepath)
                            Dim fileName As String
                            fileName = checkFilesInFolder(item.InnerText)
                            Dim e As ZipEntry = zip(fileName)

                            If e Is Nothing Then
                                Console.WriteLine("File: " & fileName & " does not exist in zip.")
                            End If
                        End Using
                    Next

                Next
            Next
        End If
    Catch ex As Exception
        Console.Error.WriteLine(ex.Message)
        myLogger.Log(ex.Message)
    End Try

End Function

这个读取xml文件。 xml文件存储压缩过程的信息,例如“目标”,“文件源”和“作业名称”。我希望此功能检查zip以查看所有文件是否都在zip中。正如您所看到的,此函数需要“checkFilesiInFolder”函数来获取要在zip中搜索的文件名。

问题 - 我只返回在“checkFilesInFolder”函数中扫描的最后一个文件。

Public Function checkFilesInFolder(ByVal folderPath As String)
    Try
        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo(folderPath)
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim file As IO.FileInfo

        Console.WriteLine("The following files are located in " & folderPath)

        'list the names of all files in the specified directory
        For Each file In diar1
            Console.WriteLine(file.FullName)
            'myLogger.Log(file.ToString)

        Next
        Return file.ToString
    Catch ex As Exception
        Console.Error.WriteLine(ex.Message)
        myLogger.Log(ex.Message)
    End Try
End Function

1 个答案:

答案 0 :(得分:1)

您的checkFilesInFolder函数应该返回一个集合而不仅仅是字符串。尝试将其定义更改为:

Public Function checkFilesInFolder(ByVal folderPath As String) As List(Of String)
    Dim returnList As List(Of String) = New List(Of String)()

    Try
        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo(folderPath)
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim file As IO.FileInfo

        Console.WriteLine("The following files are located in " & folderPath)

        'list the names of all files in the specified directory
        For Each file In diar1
            Console.WriteLine(file.FullName)
            Console.WriteLine(file.Name)
            returnList.Add(file.Name) 'Or FullName, depending on what you want to use
        Next
    Catch ex As Exception
        Console.Error.WriteLine(ex.Message)
    End Try

    Return returnList
End Function

请注意,我们使用List集合类来保存所有文件名。这将使您的For Each File In checkFilesInFolder(item.InnerText)声明正常运作。

我不确定,从您的用例来看,您是否需要将file.Namefile.FullName添加到returnList,但我建议您通过调试或以其他方式尝试两者并查看哪一个适合你。

现在我们有了这个列表,我们可以:

  1. 浏览每个文件
  2. 检查ZIP中是否存在该文件
  3. 乍一看,你的checkZipForFiles方法正在做正确的事情,但你的For循环可能需要进行一些调整才能使用新的集合:

        For Each item As System.Xml.XmlNode In Source
            For Each fileName As String In checkFilesInFolder(item.InnerText)
                Using zip = ZipFile.Read(zipFilepath)
                    Dim e As ZipEntry = zip(fileName)
    
                    If e Is Nothing Then
                        Console.WriteLine("File: " & fileName & " does NOT exist in zip.")
                    Else
                        Console.WriteLine("File: " & fileName & " does EXIST in zip.")
                    End If
                End Using
            Next
        Next
    

    请注意以下内容:我们从List(Of String)中获得checkFilesInFolder,因此您的For Each只处理一堆字符串 - 在这种情况下,它们是文件名,可以是通过将其提供给zip来检查ZIP文件,就像您已经在做的那样。