ZipArchiveEntry.Name 属性

定义

获取 zip 存档中条目的文件名。

public:
 property System::String ^ Name { System::String ^ get(); };
public string Name { get; }
member this.Name : string
Public ReadOnly Property Name As String

属性值

zip 存档中条目的文件名。

注解

Name 属性包含遵循最终目录分隔符(\)的属性部分 FullName ,不包括子目录层次结构。 例如,如果使用该方法在 zip 存档CreateEntryFromFile中创建两个条目,并作为第一个条目和NewEntry.txt第二个条目的名称提供AddedFolder\\NewEntry.txt,则这两个条目都将在NewEntry.txt属性中具有Name。 第一个条目也将在NewEntry.txt属性中FullName,但第二个条目将在属性中AddedFolder\\NewEntry.txt具有FullName。 在Windows,由于 NTFS 规则,冒号(:)也被视为分隔符,这可能导致不同平台Name不同。 对于独立于平台的行为,可以使用 FullName,该行为始终反映存档中存储的完整条目名称。

示例

以下示例演示如何从 zip 存档检索条目并评估条目的属性。 它使用 Name 属性来显示条目的名称,以及 Length 用于计算压缩文件的量和 CompressedLength 属性。

using System;
using System.IO;
using System.IO.Compression;

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

            using (ZipArchive archive = ZipFile.OpenRead(zipPath))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    float compressedRatio = (float)entry.CompressedLength / entry.Length;
                    float reductionPercentage = 100 - (compressedRatio * 100);

                    Console.WriteLine (string.Format("File: {0}, Compressed {1:F2}%", entry.Name, reductionPercentage));
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\example\result.zip"

        Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
            For Each entry As ZipArchiveEntry In archive.Entries
                Dim compressedRatio As Single = entry.CompressedLength / entry.Length
                Dim reductionPercentage As Single = 100 - (compressedRatio * 100)

                Console.WriteLine(String.Format("File: {0}, Compressed {1:F2}%", entry.Name, reductionPercentage))
            Next
        End Using
    End Sub

End Module

适用于