How can I Compress a directory with .NET?

前端 未结 10 2712
孤街浪徒
孤街浪徒 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:33

    The question is quite old and so are the answers.

    Best answer since end of 2012 is: Use .NET 4.5 and the contained System.IO.Compression and System.IO.Compression.ZipArchive namespace classes.

    One of many example links you receive if you search in the internet: http://www.codeproject.com/Articles/381661/Creating-Zip-Files-Easily-in-NET

    Since 2014/2015 ff.: With Roslyn the whole framework library was published as Open Source, so AFAI understand it, you are free to extract the code from the 4.5 classes (as it should be not really system specific) and use it as a library for the earlier .NET frameworks. Maybe this would give some license advantages over using the other classes- but this has to be analyzed by you.

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

    This is a good discussion that discusses the possibility of doing this without any third party libraries. I think you should have a look on it.

    Here is a large repository of sample codes that can help you in your work. Good Luck..

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

    Look into using SharpZipLib. It supports both GZip and ZIP compression in C#.

    There is an excellent tutorial here outlining what you need to do to zip a directory with SharpZipLib.

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

    GZip is part of Microsoft Framework 2.0 onward. Its called GZipStream under System.IO.Compression namespace.

    To compress a directory with this class, you'd have to create a serializable class (for e.g. Directory) which contains a collection of Files.

    The Files class would contain file-name and file-stream to read bytes from file. Once you do apply GZip on the Directory, it'll read Files one by one and write them to GZipStream.

    Check this link: http://www.vwd-cms.com/forum/forums.aspx?topic=18

    0 讨论(0)
提交回复
热议问题