open a zip file in csharp

To open a zip file in c#, you can use the System.IO.Compression.ZipFile class in the .NET framework. To open a zip file, you must first create an instance of the ZipArchive class:

main.cs
using System.IO.Compression;

ZipArchive archive = ZipFile.OpenRead("path/to/zip/file.zip");
93 chars
4 lines

Once you have an instance of the ZipArchive class, you can navigate the contents of the archive using the Entries property:

main.cs
foreach (ZipArchiveEntry entry in archive.Entries)
{
    // Do something with the entry
}
90 chars
5 lines

You can decompress the contents of each entry using the Open method:

main.cs
foreach (ZipArchiveEntry entry in archive.Entries)
{
    using (Stream stream = entry.Open())
    {
        // Decompress the contents of the entry
    }
}
156 chars
8 lines

When you're done working with the archive, be sure to close it:

main.cs
archive.Dispose(); // or use a using statement
47 chars
2 lines

gistlibby LogSnag