首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何压缩文件

如何压缩文件
EN

Stack Overflow用户
提问于 2012-06-22 17:26:04
回答 10查看 58K关注 0票数 18

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

EN

回答 10

Stack Overflow用户

发布于 2012-06-22 17:29:03

您可以在System.IO.Compression名称空间中使用GZipStream

.NET 2.0.

代码语言:javascript
复制
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

代码语言:javascript
复制
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

票数 25
EN

Stack Overflow用户

发布于 2013-04-19 00:14:52

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

  1. 在您的解决方案中安装DotNetZip DLL(最简单的方法是从nuget安装the package )
  2. 添加对DLL的引用。
  3. 通过添加以下内容导入命名空间:使用Ionic.Zip;

<代码>H19压缩文件<代码>H210<代码>G211

代码:

代码语言:javascript
复制
using (ZipFile zip = new ZipFile())
{
    zip.AddFile("C:\test\test.txt");
    zip.AddFile("C:\test\test2.txt");
    zip.Save("C:\output.zip");
}

如果您不想在压缩文件中镜像原始文件夹结构,请查看AddFile()和AddFolder()等的覆盖。

票数 21
EN

Stack Overflow用户

发布于 2019-01-11 17:33:04

对于.Net Framework4.5,这是我找到的最清晰的例子:

代码语言:javascript
复制
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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11153542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档