18

我想在 C# 中压缩一个文件和一个目录。我在互联网上找到了一些解决方案,但它们太复杂了,我无法在我的项目中运行它们。有人可以建议我一个清晰有效的解决方案吗?

4

10 回答 10

25

您可以GZipStreamSystem.IO.Compression命名空间中使用

.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

于 2012-06-22T09:29:03.450 回答
21

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

  1. 在您的解决方案中安装DotNetZip DLL(最简单的方法是从 nuget安装包)
  2. 添加对 DLL 的引用。
  3. 通过添加导入命名空间: using 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() 等的覆盖。

于 2013-04-18T16:14:52.327 回答
3

对于 .Net Framework 4.5,这是我发现的最清晰的示例:

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

您需要添加对 System.IO.Compression.FileSystem 的引用

来自:https ://docs.microsoft.com/en-us/dotnet/standard/io/how-to-compress-and-extract-files

于 2019-01-11T09:33:03.770 回答
2

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

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

于 2012-06-22T09:28:42.433 回答
1

使用 DotNetZip http://dotnetzip.codeplex.com/, ZipFile 类上有一个 AddDirectory() 方法可以满足您的需求:

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

邦尼继续...

于 2013-12-05T13:20:23.867 回答
1

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

于 2013-03-01T23:25:03.670 回答
1

http://www.icsharpcode.net/opensource/sharpziplib/

于 2012-06-22T09:27:43.903 回答
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());
                        }
                    }
                }
            }
        }

    }
于 2016-01-19T16:28:11.120 回答
0

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

于 2012-06-22T09:27:55.587 回答
0

源代码取自 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();
    }  
于 2012-06-22T11:55:45.727 回答