2017-03-15 1 views
0

안녕하세요, ZipInputStream (예 : Unseekable 입력 스트림)을 사용하여 zip을 압축 해제하려고합니다. SharpZipLib의 도움으로 그러나 이것은 항상 나에게 오류를 제공합니다 :예외가 throw되었습니다 : 'System.IO.DirectoryNotFoundException'및 System.IO.PathTooLongException

오류 :가 슬로우

예외 : mscorlib.dll에서 오류에서 'System.IO.DirectoryNotFoundException가'경로 'C의 일부를 찾을 수 없습니다 : \ Users \ username \ Documents \ Visual Studio 2015 \ Projects \ WpfApplication1 \ WpfApplication1 \ bin \ Debug \ ASPNETWebAPISamples-master \ '에 있습니다.

나는 **ZipFile.ExtractToDirectory** 추출기 및 http://dotnetzip.codeplex.com/을 빌드했습니다. 그들도 둘 다 경로가 너무 길다 예외.

경로에 관한 몇 가지 질문을 너무 오래 동안 발견했습니다. 그러나 아무도 나를 위해 일하지 못했습니다.

이 오류를 해결하는 방법? 감사합니다. .

public static async Task HttpGetForLargeFileInRightWay() 
    { 
     using (HttpClient client = new HttpClient()) 
     { 
      const string url = "https://github.com/tugberkugurlu/ASPNETWebAPISamples/archive/master.zip"; 
      using (HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead)) 
      using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync()) 
      { 
       try 
       { 
        Debug.Print("A"); 
        UnzipFromStream(streamToReadFrom, Environment.CurrentDirectory); 
        Debug.Print("M"); 
       } 
       catch (Exception ex) 
       { 

        Debug.Print("Error: " + ex.Message); 
       } 
      } 
     } 
    } 


    public static void UnzipFromStream(Stream zipStream, string outFolder) 
    { 

     ZipInputStream zipInputStream = new ZipInputStream(zipStream); 
     ZipEntry zipEntry = zipInputStream.GetNextEntry(); 
     Debug.Print("B"); 
     while (zipEntry != null) 
     { 
      String entryFileName = zipEntry.Name; 
      // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName); 
      // Optionally match entrynames against a selection list here to skip as desired. 
      // The unpacked length is available in the zipEntry.Size property. 

      byte[] buffer = new byte[4096];  // 4K is optimum 

      Debug.Print("C"); 
      // Manipulate the output filename here as desired. 
      String fullZipToPath = Path.Combine(outFolder, entryFileName); 
      Debug.Print("D"); 
      string directoryName = Path.GetDirectoryName(fullZipToPath); 
      Debug.Print("E"); 
      if (directoryName.Length > 0) 
      { 

       Debug.Print("F"); 
       Directory.CreateDirectory(directoryName); 
       Debug.Print("G"); 
      } 

      Debug.Print("H"); 
      // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size 
      // of the file, but does not waste memory. 
      // The "using" will close the stream even if an exception occurs. 
      using (FileStream streamWriter = File.Create(fullZipToPath)) 
      { 
       Debug.Print("I"); 
       StreamUtils.Copy(zipInputStream, streamWriter, buffer); 
       Debug.Print("J"); 
      } 
      Debug.Print("K"); 
      zipEntry = zipInputStream.GetNextEntry(); 
      Debug.Print("L"); 
     } 
    } 
+0

디렉토리가 존재합니까? – TheLethalCoder

+0

예'Directory.CreateDirectory (directoryName)'줄이 실제로 존재하지 않는다면 실제로 생성됩니다 – djkp

+0

어디에서 오류가 발생합니까? 그리고 사이드 노트'fullZipToPath'는 파일 경로이고'outFolder'는 그것이 존재하는 디렉토리입니다. 왜'outFolder' 대신에'Path.GetDirectoryName (fullZipToPath)'에 디렉토리를 생성하고 있습니까? – TheLethalCoder

답변

1

zipInputStream.GetNextEntry()은 zip 파일 내에서 디렉터리와 파일을 모두 반환하는 문제가 있습니다. 이것은 그 자체로는 문제가되지 않지만 코드는 파일 만 처리합니다. 이 문제를 해결하려면 fullZipToPath 변수에 파일이나 디렉토리의 경로가 있는지 여부를 감지해야합니다.

그 방법은 ZipEntry.IsDirectory 속성을 검사하는 것입니다. 코드를 다음으로 변경하십시오.

if (!zipEntry.IsDirectory) 
{ 
    using (FileStream streamWriter = File.Create(fullZipToPath)) 
    { 
     StreamUtils.Copy(zipInputStream, streamWriter, buffer); 
    } 
} 

그리고 zip 파일을 다운로드하여 잘 추출하십시오.

PathTooLongException에 대한 자세한 내용은 this question을 참조하십시오.

관련 문제