How to compress files

前端 未结 10 888
渐次进展
渐次进展 2021-02-01 06:58

I want to compress a file and a directory in C#. I found some solution in Internet but they are so complex and I couldn\'t run them in my project. Can anybody suggest me a clear

10条回答
  •  有刺的猬
    2021-02-01 07:40

    For .Net Framework 4.5 this is the most clear example I found:

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

    You'll need to add a reference to System.IO.Compression.FileSystem

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

提交回复
热议问题