2013-03-29 49 views
0

所以我有兩個需要幫助的功能。使用DotNetZip檢查zip文件

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文件存儲壓縮過程的信息,例如「目標」,「文件源」和「作業名稱」。我想要這個函數檢查壓縮文件,看看是否所有的文件都在壓縮文件中。正如你所看到的,這個函數需要藉助「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 

回答

0

您的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集合類來保存所有文件名。這會讓你的陳述適當地工作。

我不知道,從你的使用情況,您是否需要添加file.Namefile.FullNamereturnList,但我會建議你通過調試或以其他方式都會嘗試,看看哪一個對你的作品。

現在,我們有這個列表中,我們可以:

  1. 經過每個文件
  2. 檢查該文件是否存在於ZIP或不

如此看來,乍一看,您的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像你這樣的文件名已經在做。

相關問題