PH.CompressionUtility 0.0.1

dotnet add package PH.CompressionUtility --version 0.0.1
                    
NuGet\Install-Package PH.CompressionUtility -Version 0.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PH.CompressionUtility" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PH.CompressionUtility" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="PH.CompressionUtility" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PH.CompressionUtility --version 0.0.1
                    
#r "nuget: PH.CompressionUtility, 0.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PH.CompressionUtility@0.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PH.CompressionUtility&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=PH.CompressionUtility&version=0.0.1
                    
Install as a Cake Tool

PH.CompressionUtility

Documentation: Using ZipUtilityExtensions

The ZipUtilityExtensions class provides extension methods for creating ZIP archives from collections of files or directories. It supports asynchronous operations and allows customization of compression levels.

Features

  1. Create ZIP from Files:

    • Converts a collection of FileInfo objects into a ZIP archive as a byte array or stream.
    • Skips non-existent files.
  2. Create ZIP from Directories:

    • Converts a collection of DirectoryInfo objects into a ZIP archive as a stream.
    • Recursively includes all files and subdirectories.
  3. Customizable Compression Levels:

    • Supports CompressionLevel.Optimal by default, but can be customized.

Methods

ToZipAsync

Creates a ZIP archive from a collection of files and returns it as a byte array.

public static Task<byte[]> ToZipAsync(
    this IEnumerable<FileInfo> files,
    CancellationToken token,
    CompressionLevel level = CompressionLevel.Optimal
)

ToZipStreamAsync

Creates a ZIP archive from a collection of files or directories and returns it as a stream.

public static Task<Stream> ToZipStreamAsync(
    this IEnumerable<FileInfo> files,
    CancellationToken token,
    CompressionLevel level = CompressionLevel.Optimal
)

public static Task<Stream> ToZipStreamAsync(
    this IEnumerable<DirectoryInfo> directories,
    CancellationToken token,
    CompressionLevel level = CompressionLevel.Optimal
)

Usage Examples

Example 1: Create ZIP from Files (Byte Array)

var file1 = new FileInfo("file1.txt");
var file2 = new FileInfo("file2.txt");
await File.WriteAllTextAsync(file1.FullName, "Content of file 1");
await File.WriteAllTextAsync(file2.FullName, "Content of file 2");

var files = new List<FileInfo> { file1, file2 };
var token = CancellationToken.None;

byte[] zipBytes = await files.ToZipAsync(token);

File.WriteAllBytes("output.zip", zipBytes);

Example 2: Create ZIP from Files (Stream)

var file1 = new FileInfo("file1.txt");
await File.WriteAllTextAsync(file1.FullName, "Content of file 1");

var files = new List<FileInfo> { file1 };
var token = CancellationToken.None;

using var zipStream = await files.ToZipStreamAsync(token);
using var fileStream = new FileStream("output.zip", FileMode.Create, FileAccess.Write);

await zipStream.CopyToAsync(fileStream);

Example 3: Create ZIP from Directories

var dir = new DirectoryInfo("testdir");
dir.Create();
await File.WriteAllTextAsync(Path.Combine(dir.FullName, "file1.txt"), "Content of file 1");

var directories = new List<DirectoryInfo> { dir };
var token = CancellationToken.None;

using var zipStream = await directories.ToZipStreamAsync(token);
using var fileStream = new FileStream("output.zip", FileMode.Create, FileAccess.Write);

await zipStream.CopyToAsync(fileStream);

Tests

The ZipUtilityExtensionsTest class provides comprehensive test coverage for the utility. Below are some key test cases:

  1. Empty File Collection:

    • Ensures that an empty collection returns an empty ZIP archive.
  2. Single File:

    • Verifies that a single file is correctly added to the ZIP archive.
  3. Multiple Files:

    • Confirms that multiple files are included in the ZIP archive.
  4. Empty Directory Collection:

    • Ensures that an empty directory collection returns an empty ZIP archive.
  5. Directory with Files:

    • Verifies that all files in a directory (including subdirectories) are included in the ZIP archive.

Conclusion

The ZipUtilityExtensions class is a powerful utility for creating ZIP archives from files and directories. Its asynchronous methods and support for customizable compression levels make it suitable for a wide range of applications. The provided tests ensure reliability and correctness.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on PH.CompressionUtility:

Package Downloads
PH.NlogExtensions

A tiny c# class for extend Nlog

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1 129 5/30/2025

First release