2008-08-01 11 views
42

C#에서 파일과 폴더를 빨리 압축하거나 압축 해제하는 좋은 방법에 대해 알고 있습니까? 대용량 파일을 처리해야 할 수도 있습니다.폴더/파일 압축/압축 해제

답변

21

저는 항상 SharpZip 라이브러리를 사용했습니다. 톰 지적

Here's a link

+0

참고 : 필자는 * int 이상을 발견했습니다. 몇 년 전 SharpZip 코드에서 flow * 버그가 발생하여 방금 올바른 값 조합을 가진 임의의 파일에서 오류가 발생했습니다. 그것이 고정되어 있는지 확실하지 않지만 메모리에서 SharpZip 소스에서'int' 변수를'long'으로 변경하여 오버플로를 피하십시오. * 내 오래된 고정 샤프 우편 코드를 찾아서 최신 *과 일치하는지 확인해야합니다. –

6

이 자바에서 아주 쉽게 할 수 있으며, 명시된 바와 같이 당신은 C#을에서 java.util.zip 라이브러리에 도달 할 수 위.
sample code

java.util.zip javadocs 내가 폴더 구조의 깊이 (재귀) 우편을이 얼마 전에 사용하지만, 나는 이제까지 압축 풀기를 사용 생각하지 않는다 : 참고 문헌을 참조. 내가 그렇게 동기를 부여한다면 나중에 그 코드를 꺼내 편집 할 수 있습니다.

24

.Net 2.0 프레임 워크 네임 스페이스 System.IO.Compression은 GZip 및 Deflate 알고리즘을 지원합니다. 다음은 파일 객체에서 얻을 수있는 바이트 스트림을 압축하고 압축 해제하는 두 가지 방법입니다. 해당 알고리즘을 사용하려면 GZipStreamDefaultStream에 대해 아래 방법으로 대체 할 수 있습니다. 그러나 여전히 다른 알고리즘으로 압축 된 파일을 처리하는 문제가 남아 있습니다.

public static byte[] Compress(byte[] data) 
{ 
    MemoryStream output = new MemoryStream(); 

    GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true); 
    gzip.Write(data, 0, data.Length); 
    gzip.Close(); 

    return output.ToArray(); 
} 

public static byte[] Decompress(byte[] data) 
{ 
    MemoryStream input = new MemoryStream(); 
    input.Write(data, 0, data.Length); 
    input.Position = 0; 

    GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true); 

    MemoryStream output = new MemoryStream(); 

    byte[] buff = new byte[64]; 
    int read = -1; 

    read = gzip.Read(buff, 0, buff.Length); 

    while (read > 0) 
    { 
     output.Write(buff, 0, read); 
     read = gzip.Read(buff, 0, buff.Length); 
    } 

    gzip.Close(); 

    return output.ToArray(); 
} 
8

내 대답은 눈을 감고 DotNetZip을 선택합니다. 대규모 커뮤니티에서 테스트되었습니다.

이 방법으로 zip 파일을 만들 수 있습니다
0

:

public async Task<string> CreateZipFile(string sourceDirectoryPath, string name) 
    { 
     var path = HostingEnvironment.MapPath(TempPath) + name; 
     await Task.Run(() => 
     { 
      if (File.Exists(path)) File.Delete(path); 
      ZipFile.CreateFromDirectory(sourceDirectoryPath, path); 
     }); 
     return path; 
    } 

을하고이 방법으로 zip 파일을 압축을 해제 할 수 있습니다 : zip 파일 경로
1이 방법 작업

public async Task ExtractZipFile(string filePath, string destinationDirectoryName) 
    { 
     await Task.Run(() => 
     { 
      var archive = ZipFile.Open(filePath, ZipArchiveMode.Read); 
      foreach (var entry in archive.Entries) 
      { 
       entry.ExtractToFile(Path.Combine(destinationDirectoryName, entry.FullName), true); 
      } 
      archive.Dispose(); 
     }); 
    } 

2 -이 방법은 zip 파일 스트림에서 작동합니다.

public async Task ExtractZipFile(Stream zipFile, string destinationDirectoryName) 
    { 
     string filePath = HostingEnvironment.MapPath(TempPath) + Utility.GetRandomNumber(1, int.MaxValue); 
     using (FileStream output = new FileStream(filePath, FileMode.Create)) 
     { 
      await zipFile.CopyToAsync(output); 
     } 
     await Task.Run(() => ZipFile.ExtractToDirectory(filePath, destinationDirectoryName)); 
     await Task.Run(() => File.Delete(filePath)); 
    }