2012-06-22 83 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()); 
         } 
        } 
       } 
      } 
     } 

    }