Home > Back-end >  How to compress files using C# .NET 2.0?
How to compress files using C# .NET 2.0?

Time:10-11

I want to compress multiple files into a Zip folder.

I'm using C# .NET 2.0, is it possible to do it without the use of 3rd party libraries?

I've tried System.IO.Compression but doesn't compress multiple files, just once.

CodePudding user response:

Could you not just do the zip compress with a foreach for all the files? something like this?

public static void CreateZip(string fileName, IEnumerable<string> files)
{
    var zip = ZipFile.Open(fileName, ZipArchiveMode.Create);
    foreach (var file in files)
    {
         zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
    }
}

CodePudding user response:

No. assuming you mean .Net framework 2.0, and not .Net Core 2.0. The ZipArchive class you need to create actual zip files was added in .net framework 4.5. Earlier framework versions need to use a third party library. See stack software recommendations for such libraries.

If you are on .Net Core 2.0, just use the linked ZipArchive class or the ZipFile.CreateFromDirectory-method. If you are on .Net framework 2.0 I would highly recommend that you update. I do not think .Net framework 2.0 is preinstalled on any supported OS, and I do not think it is supported at all anymore. Updating to 4.8 should be fairly straightforward unless you use some really old and outdated libraries.

  • Related