How can I Compress a directory with .NET?

前端 未结 10 2682
孤街浪徒
孤街浪徒 2021-02-14 16:16

I have a directory that contains several files. I want compress this folder to a zip or tar.gz file. How can I do his work in C#?

相关标签:
10条回答
  • 2021-02-14 16:19

    use 7zip from commandline in C# --> LZMA SDK supports C#, and there are codesamples in the package

    0 讨论(0)
  • 2021-02-14 16:26

    You can zip the directory in pure .NET 3.0.

    First, you will need a reference to WindowsBase.dll.

    This code will open or create a zip file, create a directory inside, and place the file in that directory. If you want to zip a folder, possibly containing sub-directories, you could loop through the files in the directory and call this method for each file. Then, you could depth-first search the sub-directories for files, call the method for each of those and pass in the path to create that hierarchy within the zip file.

    public void AddFileToZip(string zipFilename, string fileToAdd, string destDir)
    {
        using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
        {
            string destFilename = "." + destDir + "\\" + Path.GetFileName(fileToAdd);
            Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
            if (zip.PartExists(uri))
            {
                zip.DeletePart(uri);
            }
            PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
    
            using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
            {
                using (Stream dest = part.GetStream())
                {
                    CopyStream(fileStream, dest);
                }
            }
        }
    }
    

    destDir could be an empty string, which would place the file directly in the zip.

    Sources: https://weblogs.asp.net/jongalloway/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib

    https://weblogs.asp.net/albertpascual/creating-a-folder-inside-the-zip-file-with-system-io-packaging

    0 讨论(0)
  • 2021-02-14 16:27

    i use the System.IO.Packaging Namespace which was introduced with .NET Framework 3.5. I decided to use that one because it's based on .NET Framework Base classes and no 3rd party code is required which blows up the size of the code..

    here's another post on Stackoverflow regarding this Question

    And here's the Namespace and ZipPackage declaration / explanation @MSDN

    hope that helps

    Christian

    0 讨论(0)
  • 2021-02-14 16:29

    Another pre-3.5 option is to use the zip utilities from J#. After all, .Net doesn't care what language the code was originally written in ;-).

    Articles on how to do this:

    • ASP-Alliance
    • MSDN
    • CodeProject
    • C-Sharp Corner
    0 讨论(0)
  • 2021-02-14 16:32

    At my previous job we used #ziplib.

    0 讨论(0)
  • 2021-02-14 16:32

    You can use DotNetZip Library. It has quite rich and useful features.


    EDIT:

    string[] MainDirs = Directory.GetDirectories(DirString);
    
    for (int i = 0; i < MainDirs.Length; i++)
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.UseUnicodeAsNecessary = true;
            zip.AddDirectory(MainDirs[i]);
            zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
            zip.Save(string.Format("test{0}.zip", i));   
        }
    }
    
    0 讨论(0)
提交回复
热议问题