Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


ZipFile.OpenRead(String) Method

Definition

Opens a zip archive for reading at the specified path.

C#
public static System.IO.Compression.ZipArchive OpenRead(string archiveFileName);

Parameters

archiveFileName
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.

Returns

The opened zip archive.

Exceptions

archiveFileName is Empty, contains only white space, or contains at least one invalid character.

archiveFileName is null.

In archiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

archiveFileName is invalid or does not exist (for example, it is on an unmapped drive).

archiveFileName could not be opened.

-or-

An unspecified I/O error occurred while opening the file.

archiveFileName specifies a directory.

-or-

The caller does not have the required permission to access the file specified in archiveFileName.

The file specified in archiveFileName is not found.

archiveFileName contains an invalid format.

archiveFileName could not be interpreted as a zip archive.

Examples

The following example shows how to open a zip archive for reading.

C#
using System;
using System.IO;
using System.IO.Compression;

class Program
{
    static void Main(string[] args)
    {
        string zipPath = @".\result.zip";

        Console.WriteLine("Provide path where to extract the zip file:");
        string extractPath = Console.ReadLine();

        // Normalizes the path.
        extractPath = Path.GetFullPath(extractPath);

        // Ensures that the last character on the extraction path
        // is the directory separator char.
        // Without this, a malicious zip file could try to traverse outside of the expected
        // extraction path.
        if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
            extractPath += Path.DirectorySeparatorChar;

        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    // Gets the full path to ensure that relative segments are removed.
                    string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName));

                    // Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                    // are case-insensitive.
                    if (destinationPath.StartsWith(extractPath, StringComparison.Ordinal))
                        entry.ExtractToFile(destinationPath);
                }
            }
        }
    }
}

Remarks

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.

Applies to

Termék Verziók
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0