2013-03-29 2 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 파일에는 "Destination", "File Source"및 "Job name"과 같은 압축 프로세스에 대한 정보가 저장됩니다. 이 함수는 모든 파일이 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 

답변

0

함수 checkFilesInFolder 함수는 문자열 대신 모음을 반환해야합니다. 같은 그것의 정의를 변경하십시오 : 우리는 모든 파일 이름을 유지하기 위해 List 컬렉션 클래스를 사용하는

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 

참고. 이렇게하면 For Each File In checkFilesInFolder(item.InnerText) 문이 적절하게 작동합니다. 당신이 returnListfile.Name 또는 file.FullName를 추가 할 필요가 있는지

나는, 사용 사례에서, 잘 모르겠지만, 난 당신을 디버깅하거나 모두 시도하고 당신을 위해 작동하는 하나의 참조 건의 할 것입니다.

이제 우리는 우리가 할 수있는이 목록이 있는지 :

은 그래서 언뜻 보인다 각각의 파일을 통해

  • 파일이 ZIP에 있는지 확인 여부를

    1. 이동, 귀하의 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 파일에서 확인할 수 있습니다 파일 이름입니다 이미하고 있었다.

  • 관련 문제