It is common to want to check the contents of a ZIP file or verify if a specific file exists before extracting everything. By using the System.IO.Compression.ZipFile.OpenRead method, you can quickly access the information (entries) within the archive without actually extracting the files.
Implementation Sample: Displaying Filenames and Compression Ratios
The following code opens a ZIP file and displays the names of all stored files along with their file sizes (both original and compressed).
Sample Code
using System;
using System.IO;
using System.IO.Compression; // Reference may be required depending on the environment
public class Program
{
public static void Main()
{
string zipPath = "Material.zip";
// 1. Prepare the test environment
// Create a ZIP file for demonstration
if (File.Exists(zipPath)) File.Delete(zipPath);
using (ZipArchive zip = ZipFile.Open(zipPath, ZipArchiveMode.Create))
{
// Create files inside folders and in the root directory
zip.CreateEntry("images/logo.png");
zip.CreateEntry("readme.txt");
zip.CreateEntry("src/program.cs");
}
Console.WriteLine($"Target File: {zipPath}\n");
// 2. Open the ZIP file in "Read-only" mode
// ZipFile.OpenRead internally calls File.Open with Read access
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
Console.WriteLine("--- Stored File List ---");
// 3. Get all entries using the Entries property
// archive.Entries returns a ReadOnlyCollection<ZipArchiveEntry>
foreach (ZipArchiveEntry entry in archive.Entries)
{
// FullName: The complete path including folder hierarchy
Console.WriteLine($"Path: {entry.FullName}");
// Other information such as size can also be retrieved
// entry.Length : Original size
// entry.CompressedLength : Compressed size
Console.WriteLine($" - Size: {entry.Length} bytes (Compressed: {entry.CompressedLength})");
}
}
}
}
Execution Result
Target File: Material.zip
--- Stored File List ---
Path: images/logo.png
- Size: 0 bytes (Compressed: 0)
Path: readme.txt
- Size: 0 bytes (Compressed: 0)
Path: src/program.cs
- Size: 0 bytes (Compressed: 0)
Explanation and Technical Points
1. ZipFile.OpenRead
If you only need to view the contents of a ZIP, it is most efficient and safe to use ZipFile.OpenRead (or ZipFile.Open(path, ZipArchiveMode.Read)), which does not require write permissions. This minimizes the time the file is locked and eliminates the risk of accidentally modifying the data.
2. ZipArchive.Entries Property
This property returns all file information in the archive as a ReadOnlyCollection<ZipArchiveEntry>. You can easily perform searches by using LINQ on this collection:
// Example: Searching only for files with a ".png" extension
var images = archive.Entries.Where(e => e.FullName.EndsWith(".png"));
3. Main Properties of ZipArchiveEntry
- FullName: The relative path inside the ZIP file (e.g.,
folder/sub/file.txt). - Name: The filename without the path (e.g.,
file.txt). - LastWriteTime: The last time the file was modified.
- Length: The file size after extraction.
- CompressedLength: The file size in its compressed state.
This covers the basic operations for ZIP archives: creating, adding, deleting, and listing files. By combining these techniques, you can build your own backup or deployment tools.
