.NET Framework Reading and writing Zip files

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

The ZipFile class lives in the System.IO.Compression namespace. It can be used to read from, and write to Zip files.

Remarks

  • You can also use a MemoryStream instead of a FileStream.

  • Exceptions

ExceptionCondition
ArgumentExceptionThe stream has already been closed, or the capabilities of the stream does not match the mode (eg: trying to write to a read only stream)
ArgumentNullExceptioninput stream is null
ArgumentOutOfRangeExceptionmode has an invalid value
InvalidDataExceptionSee list below

When a InvalidDataException is thrown, it can have 3 causes:

  • The contents of the stream could not be interpreted as a zip archive
  • mode is Update and an entry is missing from the archive or is corrupt and cannot be read
  • mode is Update and an entry is too large to fit into memory

All information has been taken from this MSDN page



Got any .NET Framework Question?