Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Compress a Directory in net 4.0? No DLL's?

How would i compress a directory into a .zip or .gz without any external dll's?


I can do it with with dotnetzip but then you have an external dll which i would like to avoid.

I know in net 4.5 you can use this: https://msdn.microsoft.com/en-us/library....110).aspx


But I need a solution for 4.0
DotNetZip:
https://www.nuget.org/packages/DotNetZip/

+

Costura Fody:
https://www.nuget.org/packages/Costura.Fody/

=

Standalone .EXE file (even in 4.0)
You could implement LZMA compression via 7-zip's SDK for C#. http://7-zip.org/sdk.html
(Dec 11, 2016 12:52 PM)Zilog80 Wrote:  DotNetZip:
https://www.nuget.org/packages/DotNetZip/

+

Costura Fody:
https://www.nuget.org/packages/Costura.Fody/

=

Standalone .EXE file (even in 4.0)

Doesn't seem to work when building the solution using xbuild (linux)
.NET 4 you probably have to use gzipstream if you dont want third party libs
(Dec 11, 2016 01:08 PM)mrkoolaid Wrote:  .NET 4 you probably have to use gzipstream if you dont want third party libs

It can only compress single files. I would like to compress a folder/directory.


(Dec 11, 2016 12:55 PM)webdev77 Wrote:  You could implement LZMA compression via 7-zip's SDK for C#. http://7-zip.org/sdk.html

Are there any easier alternatives? seems quite complicated 🤮


Actually I found this which seems to work nicely:

Code
public static void zipFolder(string inputFolder, string outputZip)
        {
            using (Package Zip = Package.Open(outputZip, FileMode.Create))
            {
                foreach (string F in Directory.GetFiles(inputFolder, "*.*", SearchOption.AllDirectories))
                {
                    string F1 = F.Replace(' ', '_');
                    Uri URI = new Uri(F1.Substring(inputFolder.Length), UriKind.Relative);
                    PackagePart ZipPart = Zip.CreatePart(PackUriHelper.CreatePartUri(URI), MediaTypeNames.Application.Zip, CompressionOption.Maximum);
                    byte[] B = File.ReadAllBytes(F);
                    ZipPart.GetStream().Write(B, 0, B.Length);
                }
            }
        }


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by HackooJul 1, 2020 08:22 AM 3 273 Jul 1, 2020 08:22 AM
Last Post: katerinaxd55
[C] by SecurifierJan 17, 2020 07:20 AM 12 416 Jan 17, 2020 07:20 AM
Last Post: SIM-3602340
[.NET] by SIM-478284Oct 7, 2018 10:42 PM 20 2,125 Oct 7, 2018 10:42 PM
Last Post: Vyxn