Export (0) Print
Expand All

ZipFile.OpenRead Method

.NET Framework 4.5

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)

public static ZipArchive OpenRead(
	string archiveFileName
)

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.

Return Value

Type: System.IO.Compression.ZipArchive
The opened zip archive.

ExceptionCondition
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));
                    }
                }
            } 
        }
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5

Windows 8.1, Windows Server 2012 R2, 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.

Show:
© 2014 Microsoft