ZipArchiveEntry.CompressedLength 属性

定义

获取 zip 存档中条目的压缩大小(以字节为单位)。

public:
 property long CompressedLength { long get(); };
public long CompressedLength { get; }
member this.CompressedLength : int64
Public ReadOnly Property CompressedLength As Long

属性值

在 zip 存档中的项的压缩大小。

例外

由于项已修改,属性的值不可用。

示例

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

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

注解

当模式设置为 Create时,或模式设置为 Update 且已打开条目时,无法检索此属性。

适用于