2012-10-08 2 views
2

wpf 응용 프로그램을 개발 중입니다. 나는 파일을 압축하고 압축을 풀기 위해 sharpziplib을 사용하고 있습니다. 다음 코드를 사용하여 .zip 파일을 쉽게 압축 해제합니다.C#에서 .bz2 파일을 압축 해제하는 방법은 무엇입니까?

public static void UnZip(string SrcFile, string DstFile, string safeFileName, int bufferSize) 
     { 
      //ICSharpCode.SharpZipLib.Zip.UseZip64.Off; 

      FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read); 
      ZipInputStream zipInStream = new ZipInputStream(fileStreamIn); 


      string rootDirectory = string.Empty; 
      if (safeFileName.Contains(".zip")) 
      { 
       rootDirectory = safeFileName.Replace(".zip", string.Empty); 
      } 
      else 
      { 
       rootDirectory = safeFileName; 
      } 

      Directory.CreateDirectory(App.ApplicationPath + rootDirectory); 

      while (true) 
      { 
       ZipEntry entry = zipInStream.GetNextEntry(); 

       if (entry == null) 
        break; 

       if (entry.Name.Contains("/")) 
       { 
        string[] folders = entry.Name.Split('/'); 

        string lastElement = folders[folders.Length - 1]; 
        var folderList = new List<string>(folders); 
        folderList.RemoveAt(folders.Length - 1); 
        folders = folderList.ToArray(); 

        string folderPath = ""; 
        foreach (string str in folders) 
        { 
         folderPath = folderPath + "/" + str; 
         if (!Directory.Exists(App.ApplicationPath + rootDirectory + "/" + folderPath)) 
         { 
          Directory.CreateDirectory(App.ApplicationPath + rootDirectory + "/" + folderPath); 
         } 
        } 

        if (!string.IsNullOrEmpty(lastElement)) 
        { 
         folderPath = folderPath + "/" + lastElement; 
         WriteToFile(DstFile + rootDirectory + @"\" + folderPath, bufferSize, zipInStream, rootDirectory, entry); 
        } 

       } 
       else 
       { 
        WriteToFile(DstFile + rootDirectory + @"\" + entry.Name, bufferSize, zipInStream, rootDirectory, entry); 
       } 
      } 

      zipInStream.Close();   
      fileStreamIn.Close(); 
     } 

     private static void WriteToFile(string DstFile, int bufferSize, ZipInputStream zipInStream, string rootDirectory, ZipEntry entry) 
     { 
      FileStream fileStreamOut = new FileStream(DstFile, FileMode.OpenOrCreate, FileAccess.Write); 
      int size; 
      byte[] buffer = new byte[bufferSize]; 

      do 
      { 
       size = zipInStream.Read(buffer, 0, buffer.Length); 
       fileStreamOut.Write(buffer, 0, size); 
      } while (size > 0); 

      fileStreamOut.Close(); 
     } 

그러나 동일한 코드가 .bz2 파일과 작동하지 않습니다. 줄에 오류가 있습니다.

ZipEntry entry = zipInStream.GetNextEntry(); 

오류는 - 잘못된 로컬 헤더 서명 : 0x26594131입니다. .bz2 파일을 어떻게 압축 해제해야합니까? 위의 문제를 해결할 수있는 코드 나 링크를 제공해 주시겠습니까? 당신이 ZipInputStream.zip에 대한 파일을 사용하는 동안

답변

3

, 당신은 BZip2InputStream.bz2에 대한 파일 (및 GZipInputStream.gz에 대한 파일 등)을 사용해야합니다.

+0

C#으로 코드를 제공해 주실 수 있습니까? –

+0

[this] (http://www.codeproject.com/Articles/6834/MemoryStream-Compression) 예제 – sloth

3

zip (및 RAR 및 tar)과 달리 bz2 및 gzip은 바이트 스트림 압축기 일뿐입니다. 앞에서와 같이 컨테이너 형식에 대한 개념이 없으므로 GetNextEntry에서 실패합니다. (즉, bz2 및 gzip은 최대 1 개의 항목 만 가질 수 있습니다).

+2

... 따라서 확장자 .tar.bz2 및 .tar.gz ... tarball이 필요합니다 파일 시스템과 스트림 압축 프로그램은이 단일 tarball 파일을 압축합니다. +1 – spender