如何压缩文件

我想在C#中压缩文件和目录。 我在Internet上找到了一些解决方案,但它们非常复杂,我无法在我的项目中运行它们。 有人能建议我一个明确有效的解决方案吗?

您可以在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

我正在添加这个答案,因为我找到了比现有答案更简单的方法:

  1. 在您的解决方案中安装DotNetZip DLL(最简单的方法是从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()等的覆盖。

System.IO.Packaging有一个名为ZipPackage的内置类:

http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage(v=vs.100).aspx

你可以使用ms-dos命令行程序compact.exe。 查看cmd中的参数compact.exe,并使用.NET方法Process.Start()启动此过程。

只需使用以下代码压缩文件即可。

  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()); } } } } } } 

使用http://dotnetzip.codeplex.com/来压缩ZIP文件或目录,没有内置类可以直接在.NET中完成

源自MSDN的源代码,与.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(); } 

使用DotNetZip http://dotnetzip.codeplex.com/,ZipFile类上有一个AddDirectory()方法可以执行您想要的操作:

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

Bonne继续……