2011-01-26 6 views
6

현재 SharpZipLib과 .NET 2.0에서 작업 중이며 단일 압축 파일로 압축해야합니다. 이를 위해 저는 현재 다음 사용하고 있습니다 : 이것은 정확히 (틱) 예상대로 작동SharpZipLib : 하나의 파일을 하나의 압축 파일로 압축

string tempFilePath = @"C:\Users\Username\AppData\Local\Temp\tmp9AE0.tmp.xml"; 
string archiveFilePath = @"C:\Archive\Archive_[UTC TIMESTAMP].zip"; 

FileInfo inFileInfo = new FileInfo(tempFilePath); 
ICSharpCode.SharpZipLib.Zip.FastZip fZip = new ICSharpCode.SharpZipLib.Zip.FastZip(); 
fZip.CreateZip(archiveFilePath, inFileInfo.Directory.FullName, false, inFileInfo.Name); 

, 나는 작은 잡았다가 발생했을 그러나 테스트하는 동안. 내 임시 디렉토리 (압축되지 않은 입력 파일을 포함 즉 디렉토리) 다음 파일이 포함되어 있다고 말할 수 :

tmp9AE0.tmp.xml //The input file I want to compress 
xxx_tmp9AE0.tmp.xml // Some other file 
yyy_tmp9AE0.tmp.xml // Some other file 
wibble.dat // Some other file 

을 나는 모든 .xml 파일이 압축 된 아카이브에 포함 된 압축을 실행합니다. 그 이유는 CreateZip 메서드에 전달 된 마지막 fileFilter 매개 변수 때문입니다. 후드 아래에서 SharpZipLib은 패턴 일치를 수행 중이고 접두사가 xxx_yyy_ 인 파일을 선택합니다. 나는 또한 그것도 postfixed 아무것도 데리러 것이라고 가정합니다.

그래서 SharpZipLib로 단일 파일을 압축 할 수 있습니까? 그렇다면 어쩌면 질문은 어떻게하면 fileFilter의 형식을 지정하여 압축하고 싶은 파일 만 선택할 수 있도록 할 수 있습니다.

왜 그렇다면 System.IO.Compression에는 ZipStream 클래스가 포함되어 있지 않은 이유가 있습니까? 그냥 돈,

private static void CompressFile(string inputPath, string outputPath) 
{ 
    FileInfo outFileInfo = new FileInfo(outputPath); 
    FileInfo inFileInfo = new FileInfo(inputPath); 

    // Create the output directory if it does not exist 
    if (!Directory.Exists(outFileInfo.Directory.FullName)) 
    { 
     Directory.CreateDirectory(outFileInfo.Directory.FullName); 
    } 

    // Compress 
    using (FileStream fsOut = File.Create(outputPath)) 
    { 
     using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(fsOut)) 
     { 
      zipStream.SetLevel(3); 

      ICSharpCode.SharpZipLib.Zip.ZipEntry newEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(inFileInfo.Name); 
      newEntry.DateTime = DateTime.UtcNow; 
      zipStream.PutNextEntry(newEntry); 

      byte[] buffer = new byte[4096]; 
      using (FileStream streamReader = File.OpenRead(inputPath)) 
      { 
       ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(streamReader, zipStream, buffer); 
      } 

      zipStream.CloseEntry(); 
      zipStream.IsStreamOwner = true; 
      zipStream.Close(); 
     } 
    } 
} 
+2

옆으로 : ZipStream은 두 개 이상의 파일을 저장할 수있는 보관 형식이므로 ZipStream은별로 의미가 없습니다. 나는 그들이 읽기 및 쓰기 및 ZIP 용으로 완전한 API를 제공 할 수 있었지만 GZipStream보다 훨씬 많은 노력을 기울 였을 것입니다. –

+0

내 생각에, 하나의 파일 만 압축 할 때 GZip 압축을 사용하고 싶지 않은 이유가 있습니까? 프레임 워크 내에서 지원되는 것을 압니다. –

+0

나는 두 줄의 코드로이 작업을 수행 할 수있다. 우리의 '지원'데스크는 gzip의 복잡성을 처리 할 수 ​​없으므로 지퍼 여야합니다. (소리가 좋지 않습니까 ??)) – MrEyes

답변

5

이는 XY의 문제 : (한스 옆모습에서 허용 대답에서 파생) 솔루션

이 내가 구현 된 압축 방법은 다음과 같습니다

편집을 (그것은 단지 GZipStream 지원) FastZip을 사용하십시오. 사고를 피하기 위해 this web page의 첫 번째 예를 따르십시오.

+0

그래도 작동하지만 압축 된 아카이브에는 모든 디렉토리가 포함됩니다. 아카이브가 루트 레벨에서만 파일을 포함하고 다른 것은 포함하지 않는 것이 좋습니다. – MrEyes

+1

string entryName = System.IO.Path.GetFileName (filename); –

관련 문제