2017-12-06 4 views
0

기존 파일을 대체하는 zip 파일에 문제가 있습니다. 나는 여기에 다른 예제를 보았고 나는 그것을 알아낼 수없는 것 같습니다 ... loop 나는 텍스트 상자에 압축을 푼 파일의 일부 통계를 씁니다. 나는 그것이 줄 생각 :파일을 압축 해제하지 않음

if (!System.IO.File.Exists(fileUnzipFullName))

내 코드 :

public void UnzipFileNew() 
    { 
     richTextBox1.AppendText("\r\n" + "EXTRACTING!"); 

     String rootpath = Environment.CurrentDirectory; 
     //This stores the path where the file should be unzipped to, 
     //including any subfolders that the file was originally in. 
     string fileUnzipFullPath; 

     //This is the full name of the destination file including 
     //the path 
     string fileUnzipFullName; 

     //Opens the zip file up to be read 
     using (ZipArchive archive = ZipFile.OpenRead(@"update.zip")) 
     { 
      //Loops through each file in the zip file 
      foreach (ZipArchiveEntry file in archive.Entries) 
      { 

       //Outputs file information to the Textbox 
       richTextBox1.AppendText("\r\n"); 
       richTextBox1.AppendText("File Name: "+ file.Name); 
       richTextBox1.AppendText("\r\n"); 
       richTextBox1.AppendText("File Size: bytes "+ file.Length); 
       richTextBox1.AppendText("\r\n"); 
       richTextBox1.AppendText("Compression Ratio: "+ ((double)file.CompressedLength/file.Length).ToString("0.0%")); 
       richTextBox1.AppendText("\r\n"); 

       //Identifies the destination file name and path 
       fileUnzipFullName = Path.Combine(rootpath, file.FullName); //fileUnzipFullName = Path.Combine(@"Example\", file.FullName); 

       //Extracts 
       if (!System.IO.File.Exists(fileUnzipFullName)) 
       { 

        fileUnzipFullPath = Path.GetDirectoryName(fileUnzipFullName); 

        //Creates the directory if it doesn't exist 
        Directory.CreateDirectory(fileUnzipFullPath); 

        //Extracts the file to (potentially new) path 
        file.ExtractToFile(fileUnzipFullName); 
       } 

      } 
     } 
    } 

답변

1

if (!System.IO.File.Exists(fileUnzipFullName)) 실제로 당신도 이미있는 경우 파일을 추출하려고 방지 할 수 있습니다. 따라서 이것을 제거하거나 사용 사례에 따라 변경해야합니다.

또한 ExtractToFile 메서드는 파일을 이미 사용하고 있으면 IOException이됩니다.

public static void ExtractToFile(
    this ZipArchiveEntry source, 
    string destinationFileName, 
    bool overwrite 
) 

그래서 대신

file.ExtractToFile(fileUnzipFullName); 

사용

file.ExtractToFile(fileUnzipFullName, true); 

코드를 사용하여,이 뜻을 다행히 MSDN은 덮어 쓰기에 대한 부울 플래그 과부하가 있음을 알 수 모든 파일을 Zip에서 추출한 파일을 무차별 적으로 덮어 씁니다.

//Extracts 
//if (!System.IO.File.Exists(fileUnzipFullName)) 
//{ 
    fileUnzipFullPath = Path.GetDirectoryName(fileUnzipFullName); 

    //Creates the directory if it doesn't exist 
    Directory.CreateDirectory(fileUnzipFullPath); 

    //Extracts the file to (potentially new) path 
    file.ExtractToFile(fileUnzipFullName, true); 
//} 
+0

존 감사합니다! 이것은 효과가 있었다! zip에 폴더를 추가하여 루트 디렉토리에있는 폴더의 다른 파일을 대체했습니다. 'System.IO.DirectoryNotFoundException : 'C : \ Users \ stump \ source \ repos \ UpdaterTest \ UpdaterTest \ bin \ Debug \ resources \ test \'경로의 일부를 찾을 수 없습니다. '그것은 같은'file.ExtractToFile (fileUnzipFullName, true);에 던져진다. – javajoejuan

+0

오, 나는 실제 _filename_이 필요하다고 생각한다. IIRC'GetDirectoryName'는 디렉토리 만 제공합니다. 그 줄을 주석 처리하면 제대로 작동한다고 생각합니다. – john

+0

답장을 보내 주셔서 다시 한 번 감사드립니다! 하지만 그 라인을 주석 처리 할 때 오류가있는 것 같았습니다. 나는 또한'GetDirectoryName'과 관련된 다른 줄을 시도했다. – javajoejuan

관련 문제