2012-06-22 86 views
11

我想壓縮文件,並在C#中的目錄。我在互聯網上找到了一些解決方案,但它們非常複雜,我無法在我的項目中運行它們。任何人都可以向我建議一個清晰有效的解決方案嗎如何壓縮文件

+0

爲什麼不能在你的項目中運行它們? – BugFinder

+0

是指在.NET 4.0 http://msdn.microsoft.com/en-us/library/ms404280.aspx –

+1

可能重複[如何將目錄壓縮成zip文件編程(HTTP://計算器。com/questions/2498572 /如何壓縮目錄到一個zip文件編程) – ken2k

回答

0

使用http://dotnetzip.codeplex.com/ zip文件或目錄,也沒有內建類直接在.NET做

+0

謝謝@Arnaud F.我看到這個,但是我的程序不識別Z​​ipFile。我如何在我的程序中描述它? – selentoptas

23

您可以在System.IO.Compression名稱空間中使用GZipStream

.NET 2.0.

public static void CompressFile(string path) 
{ 
    FileStream sourceFile = File.OpenRead(path); 
    FileStream destinationFile = File.Create(path + ".gz"); 

    byte[] buffer = new byte[sourceFile.Length]; 
    sourceFile.Read(buffer, 0, buffer.Length); 

    using (GZipStream output = new GZipStream(destinationFile, 
     CompressionMode.Compress)) 
    { 
     Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name, 
      destinationFile.Name, false); 

     output.Write(buffer, 0, buffer.Length); 
    } 

    // Close the files. 
    sourceFile.Close(); 
    destinationFile.Close(); 
} 

.NET 4

public static void Compress(FileInfo fi) 
    { 
     // Get the stream of the source file. 
     using (FileStream inFile = fi.OpenRead()) 
     { 
      // Prevent compressing hidden and 
      // already compressed files. 
      if ((File.GetAttributes(fi.FullName) 
       & FileAttributes.Hidden) 
       != FileAttributes.Hidden & fi.Extension != ".gz") 
      { 
       // Create the compressed file. 
       using (FileStream outFile = 
          File.Create(fi.FullName + ".gz")) 
       { 
        using (GZipStream Compress = 
         new GZipStream(outFile, 
         CompressionMode.Compress)) 
        { 
         // Copy the source file into 
         // the compression stream. 
        inFile.CopyTo(Compress); 

         Console.WriteLine("Compressed {0} from {1} to {2} bytes.", 
          fi.Name, fi.Length.ToString(), outFile.Length.ToString()); 
        } 
       } 
      } 
     } 
    } 

http://msdn.microsoft.com/en-us/library/ms404280.aspx

+0

感謝您的解決方案,但是我在這段代碼中有一個關於「CopyTo」的錯誤。我無法解決這個問題。 – selentoptas

+0

@Romil - 不,命名空間在2.0中可用,我檢查過。您也可以在以下網址找到證明:http://msdn.microsoft.com/en-us/library/ms404280(v=vs.80).aspx –

+0

@DarrenDavies,uow是正確的,它在2.0中引入。我讀錯了,我需要更多的咖啡。 –

0

Source code taken from MSDN that is compatible to .Net 2.0 and above

public static void CompressFile(string path) 
     { 
      FileStream sourceFile = File.OpenRead(path); 
      FileStream destinationFile = File.Create(path + ".gz"); 

      byte[] buffer = new byte[sourceFile.Length]; 
     sourceFile.Read(buffer, 0, buffer.Length); 

     using (GZipStream output = new GZipStream(destinationFile, 
      CompressionMode.Compress)) 
     { 
      Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name, 
       destinationFile.Name, false); 

      output.Write(buffer, 0, buffer.Length); 
     } 

     // Close the files. 
     sourceFile.Close(); 
     destinationFile.Close(); 
    } 
+0

我試圖使用這個剪切,但我有一個關於到達文件路徑的問題。我有一個關於它的錯誤?什麼是不能到達文件路徑的原因?您使用的是Windows 7 – selentoptas

+0

。 –

+0

是的,我使用的是Windows7。 – selentoptas

1

你可以使用MS-DOS命令在線程序compact.exe。 查找上的參數compact.exe在cmd並使用.NET方法的Process.Start(啓動此過程)。

15

我加入這個答案,因爲我發現比現有的任何答案更簡單的方法:

  1. 在解決方案安裝DotNetZip的DLL(最簡單的方法就是安裝the package從的NuGet)
  2. 添加對DLL的引用。
  3. 通過添加導入名稱空間:使用Ionic.Zip;
  4. 郵編文件

代碼:

using (ZipFile zip = new ZipFile()) 
{ 
    zip.AddFile("C:\test\test.txt"); 
    zip.AddFile("C:\test\test2.txt"); 
    zip.Save("C:\output.zip"); 
} 

如果你不想在zip文件反映了原來的文件夾結構,然後再看看的替換值AddFile()和AddFolder()等

+0

同意,最簡單的,如果不是最簡單的。 – AMissico

+1

似乎DotNetZip是自年以來不再維護,並在壓縮某些大文件有一個錯誤: https://dotnetzip.codeplex.com/workitem/14087 我發現它太可怕了,使用無法添加的zip庫可靠地歸檔到一個archieve。任何人都可以確認DotNetZip有問題嗎? –

+1

@PeterHuber是的,它確實有這個錯誤,但是它的修復是在你發佈的鏈接上。 – NickG

0

使用DotNetZip http://dotnetzip.codeplex.com/,還有對zip文件類的AddDirectory()方法,你想要做什麼:

using (var zip = new Ionic.Zip.ZipFile()) 
{ 
    zip.AddDirectory("DirectoryOnDisk", "rootInZipFile"); 
    zip.Save("MyFile.zip"); 
} 

Bonne繼續...

1

只需使用以下代碼來壓縮文件。

 public void Compressfile() 
     { 
      string fileName = "Text.txt"; 
      string sourcePath = @"C:\SMSDBBACKUP"; 
      DirectoryInfo di = new DirectoryInfo(sourcePath); 
      foreach (FileInfo fi in di.GetFiles()) 
      { 
       //for specific file 
       if (fi.ToString() == fileName) 
       { 
        Compress(fi); 
       } 
      } 
     } 

public static void Compress(FileInfo fi) 
     { 
      // Get the stream of the source file. 
      using (FileStream inFile = fi.OpenRead()) 
      { 
       // Prevent compressing hidden and 
       // already compressed files. 
       if ((File.GetAttributes(fi.FullName) 
        & FileAttributes.Hidden) 
        != FileAttributes.Hidden & fi.Extension != ".gz") 
       { 
        // Create the compressed file. 
        using (FileStream outFile = 
           File.Create(fi.FullName + ".gz")) 
        { 
         using (GZipStream Compress = 
          new GZipStream(outFile, 
          CompressionMode.Compress)) 
         { 
          // Copy the source file into 
          // the compression stream. 
          inFile.CopyTo(Compress); 

          Console.WriteLine("Compressed {0} from {1} to {2} bytes.", 
           fi.Name, fi.Length.ToString(), outFile.Length.ToString()); 
         } 
        } 
       } 
      } 
     } 

    }