PH.CompressionUtility
0.0.1
dotnet add package PH.CompressionUtility --version 0.0.1
NuGet\Install-Package PH.CompressionUtility -Version 0.0.1
<PackageReference Include="PH.CompressionUtility" Version="0.0.1" />
<PackageVersion Include="PH.CompressionUtility" Version="0.0.1" />
<PackageReference Include="PH.CompressionUtility" />
paket add PH.CompressionUtility --version 0.0.1
#r "nuget: PH.CompressionUtility, 0.0.1"
#:package PH.CompressionUtility@0.0.1
#addin nuget:?package=PH.CompressionUtility&version=0.0.1
#tool nuget:?package=PH.CompressionUtility&version=0.0.1
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
Create ZIP from Files:
- Converts a collection of
FileInfo
objects into a ZIP archive as a byte array or stream. - Skips non-existent files.
- Converts a collection of
Create ZIP from Directories:
- Converts a collection of
DirectoryInfo
objects into a ZIP archive as a stream. - Recursively includes all files and subdirectories.
- Converts a collection of
Customizable Compression Levels:
- Supports
CompressionLevel.Optimal
by default, but can be customized.
- Supports
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:
Empty File Collection:
- Ensures that an empty collection returns an empty ZIP archive.
Single File:
- Verifies that a single file is correctly added to the ZIP archive.
Multiple Files:
- Confirms that multiple files are included in the ZIP archive.
Empty Directory Collection:
- Ensures that an empty directory collection returns an empty ZIP archive.
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 | Versions 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. |
-
.NETStandard 2.0
- System.IO.Compression.ZipFile (>= 4.3.0)
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