Checking the Contents of a ZIP Without Extracting It — Directly Using .NET's ZipFile from PowerShell

PowerShellカテゴリを表すパンダのイラスト PowerShell

About this article
This article was created using an automated generation workflow leveraging generative AI. It reviews the official .NET APIs `ZipFile.OpenRead` and `ZipArchiveEntry`, organizing them as a PowerShell tip for inspecting ZIP contents without extracting them.

Verification Status: 📘 .NET Official API Verified / Sample Implemented / PowerShell Actual Machine Verification Pending

Inspecting ZIP Contents Without Extracting — Using .NET ZipFile Directly from PowerShell

If you only want to check the contents of a ZIP file, there is no need to extract it every time. From PowerShell, .NET of System.IO.Compression.ZipFile.OpenRead() allows you to open a ZIP file for reading and list the name and size of each entry.

Minimal Example

$path = (Resolve-Path -LiteralPath ".sample.zip").Path

# ZIPを読み取り専用として開きます。ここでは展開しません。

$archive = [System.IO.Compression.ZipFile]::OpenRead($path)

try {
    $archive.Entries | Select-Object FullName, Length, CompressedLength
}
finally {

    # ファイルを掴んだままにしないよう、必ず閉じます。

    $archive.Dispose()
}

Entries is the list of entries inside the ZIP. From each entry, you can retrieve properties such as the original size Length, the compressed size CompressedLength, and so on.

What Does "Without Extracting" Mean?

flowchart LR
    A[ZIPファイル] --> B[ZipFile.OpenRead]
    B --> C[ZipArchive]
    C --> D[Entries]
    D --> E[ファイル名]
    D --> F[元サイズ Length]
    D --> G[圧縮サイズ CompressedLength]

This process reads the list of entries and their metadata; it does not extract each file to a destination. Therefore, it is suitable for checking "what files are inside" or "if there are any unexpectedly huge entries" before extraction.

However, simply reading a ZIP file does not make unknown files safe. If you plan to extract or execute an untrusted archive later, you must perform other security checks, such as verifying paths and file contents.

The Full Version Also Outputs Aggregates

The GitHub version displays the following for each entry:

  • Name: Path inside the ZIP

  • Size: Original size

  • CompressedSize: Compressed size

  • LastWriteTime: Last modified time recorded in the entry

  • IsDirectory: Whether it is a directory entry

It also outputs the total number of files, total original size, and total compressed size.

With this approach, you can streamline verification tasks, such as when you receive a ZIP file and want to check only its size and file structure before extracting it.

Do Not Skip Dispose

OpenRead() opened with ZipArchive holds a lock on the file. If you attempt to move or overwrite the ZIP in another process without properly cleaning up, it can cause "file in use" errors depending on the environment.

Therefore, even if an error occurs midway, we ensure that finally is called in a Dispose() block. Even in short scripts, opening and closing external resources should always be handled as a single unit.

GitHub Sample

Official & Primary Information

Document information

Article title
Checking the Contents of a ZIP Without Extracting It — Directly Using .NET's ZipFile from PowerShell
Published
Updated
Source
https://papanda925.com/?p=15305&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL