ZipFile.OpenRead Method
Opens a zip archive for reading at the specified path.
Namespace: System.IO.Compression
Assembly: System.IO.Compression.FileSystem (in System.IO.Compression.FileSystem.dll)
Parameters
- archiveFileName
- Type: System.String
The path to the archive to open, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.
| Exception | Condition |
|---|---|
| ArgumentException | archiveFileName is Empty, contains only white space, or contains at least one invalid character. |
| ArgumentNullException | archiveFileName is null. |
| PathTooLongException | In archiveFileName, the specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must not exceed 248 characters, and file names must not exceed 260 characters. |
| DirectoryNotFoundException | archiveFileName is invalid or does not exist (for example, it is on an unmapped drive). |
| IOException | archiveFileName could not be opened. |
| UnauthorizedAccessException | archiveFileName specifies a directory. -or- The caller does not have the required permission to access the file specified in archiveFileName. |
| FileNotFoundException | The file specified in archiveFileName is not found. |
| NotSupportedException | archiveFileName contains an invalid format. |
| InvalidDataException | archiveFileName could not be interpreted as a zip archive. |
This method is equivalent to calling the Open method and setting the mode parameter to Read. The archive is opened with FileMode.Open as the file mode value. If the archive does not exist, a FileNotFoundException exception is thrown.
The following example shows how to open a zip archive for reading.
using System; using System.IO; using System.IO.Compression; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string zipPath = @"c:\example\start.zip"; string extractPath = @"c:\example\extract"; using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)) { entry.ExtractToFile(Path.Combine(extractPath, entry.FullName)); } } } } } }
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.