How to Zip Files in Folder Using C#
ZIPファイルは、ZIP形式を使用して1つまたは複数の圧縮ファイルまたはフォルダーを含むファイルです。 複数のファイルやフォルダを1つのファイルに圧縮し、アーカイブする一般的な方法です。データのサイズを減少させ、ディスクスペースを節約し、インターネット上でファイルを転送しやすくします。 この記事では、ZIPファイルをC#で使用してIronZIPライブラリで扱う方法を学びます。 プログラム的にZIPファイルを作成、読み取り、抽出、更新する方法や、IronZIPの暗号化、パスワード保護、圧縮レベルなどのさまざまな機能を使用する方法を紹介します。 この記事の最後までには、IronZIPを使用してC#のアプリケーションでZIPファイルを簡単に扱えるようになります。
この記事で取り上げる内容
- プロジェクトにIronZIPをインストール
- ZIPファイルを作成
- パスワード保護されたZIPファイルを作成
- ZIPファイルを抽出
- パスワード保護されたZIPファイルを抽出
- 既存のZIPアーカイブにアクセス
IronZIPとは何か?
IronZIPは、プログラムでZIPファイルを作成、読み取り、抽出するための強力で多目的なC# ZIPアーカイブライブラリです。 それはZIP、TAR、GZIP、およびBZIP2など、さまざまなアーカイブ形式をサポートしています。 また、パスワード保護、暗号化、および圧縮レベルもサポートしています。 IronZIPは.NET 8、7、6、Core、Standard、Frameworkと互換性があります。
IronZIPは、さまざまなユースケースやZIPファイルの作業の利点を扱うのに役立ちます。
1.バックアップシステムの作成: IronZIPを使用して、重要なファイルやフォルダをZIPアーカイブに圧縮および暗号化し、安全な場所に保存することができます。 この方法でディスクスペースを節約し、不正アクセスからデータを保護することができます。 2.メール添付ファイルの送信: IronZIPを使用すると、メール添付ファイルをZIPファイルに圧縮することでサイズを小さくできます。これにより、ファイルサイズの制限を超えることを回避し、送信処理を高速化できます。 3. Webからのファイルのダウンロード: IronZIPを使用すると、ソフトウェアパッケージ、ドキュメント、画像、その他の種類のファイルなど、WebからZIPファイルをダウンロードして解凍できます。 これにより、帯域幅と時間を節約し、必要なファイルに簡単にアクセスできます。
IronZIPの始め方
コードを書く前に、C#プロジェクトにIronZIP NuGetパッケージをインストールする必要があります。 IronZIPは、NuGet経由で入手可能な人気のある圧縮ライブラリです。
IronZIPライブラリのインストール
IronZIPをインストールするには、Visual StudioのNuGetパッケージマネージャーコンソールを使用できます。 以下のコマンドを実行するだけです。
また、公式IronZIPウェブサイトから直接パッケージをダウンロードすることもできます。インストールが完了したら、次の名前空間をC#コードの先頭に追加して始められます。
using IronZip;Imports IronZipImports IronZipフォルダ内のC# ZIPファイルを作成する
IronZIPを使用してフォルダ内に簡単にZIPファイルを作成できます。次のコードは、指定されたディレクトリからすべてのファイルをZIP化します。
using System;
using System.IO;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Get all files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Docs\");
// Create a new ZIP archive
using (var archive = new IronZipArchive())
{
// Iterate through each file and add it to the archive
foreach (var file in fileArray)
{
archive.Add(file); // Add files to the ZIP
}
// Save the archive to a file
archive.SaveAs("myZipFile.zip");
}
}
}using System;
using System.IO;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Get all files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Docs\");
// Create a new ZIP archive
using (var archive = new IronZipArchive())
{
// Iterate through each file and add it to the archive
foreach (var file in fileArray)
{
archive.Add(file); // Add files to the ZIP
}
// Save the archive to a file
archive.SaveAs("myZipFile.zip");
}
}
}Imports System
Imports System.IO
Imports IronZip
Module Program
Sub Main(ByVal args As String())
' Get all files from the specified directory
Dim fileArray As String() = Directory.GetFiles("D:\Docs\")
' Create a new ZIP archive
Using archive As New IronZipArchive()
' Iterate through each file and add it to the archive
For Each file As String In fileArray
archive.Add(file) ' Add files to the ZIP
Next
' Save the archive to a file
archive.SaveAs("myZipFile.zip")
End Using
End Sub
End ModuleImports System
Imports System.IO
Imports IronZip
Module Program
Sub Main(ByVal args As String())
' Get all files from the specified directory
Dim fileArray As String() = Directory.GetFiles("D:\Docs\")
' Create a new ZIP archive
Using archive As New IronZipArchive()
' Iterate through each file and add it to the archive
For Each file As String In fileArray
archive.Add(file) ' Add files to the ZIP
Next
' Save the archive to a file
archive.SaveAs("myZipFile.zip")
End Using
End Sub
End Module上記のC#コードでは、IronZIPライブラリを使用してすべてのファイルを1つのZIPファイルに圧縮します。このコードは以下のことを行います。
- 文字列配列
fileArrayを宣言し、ディレクトリのパス ("D:\Docs") をパラメータとして渡し、Directory.GetFilesメソッドの結果を代入します。 このメソッドは、指定されたディレクトリ内のすべてのファイルの完全な名前を含む文字列の配列を返します。 - メモリ上の ZIP アーカイブを表す
IronZipArchiveクラスの新しいインスタンスを作成します。 インスタンスは変数archiveに代入され、コードブロックが終了した時に ZIP アーカイブが破棄されることを保証するusing文でラップされています。 fileArray配列をforeachループで反復処理し、各ファイルに対してファイル名をパラメータとして渡しarchiveオブジェクトのAddメソッドを呼び出します。 このメソッドは、ファイルと同じ名前と内容の新しいエントリをZIPアーカイブに追加します。- ZIPファイルの名前("myZipFile.zip")をパラメータとして渡し
archiveオブジェクトのSaveAsメソッドを呼び出します。 このメソッドは、現在のファイルシステムにZIPアーカイブをファイルとして保存します。
このようにして、少しのコードで新しいZIPアーカイブを簡単に作成できます。
出力
出力は次の通りです。

パスワード保護されたZIPファイルの作成
IronZIPはパスワード保護されたZIPファイルを作成する簡単な方法を提供します。 次のコードサンプルは、ファイルを圧縮し、パスワードを持つ新しいZIPファイルを作成します。
using System;
using System.IO;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Get all files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Docs\");
// Create a new ZIP archive
using (var archive = new IronZipArchive())
{
// Iterate through each file and add it to the archive
foreach (var file in fileArray)
{
archive.Add(file); // Add files to the ZIP
}
// Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256);
// Save the archive to a file
archive.SaveAs("myZipFile.zip");
}
}
}using System;
using System.IO;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Get all files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Docs\");
// Create a new ZIP archive
using (var archive = new IronZipArchive())
{
// Iterate through each file and add it to the archive
foreach (var file in fileArray)
{
archive.Add(file); // Add files to the ZIP
}
// Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256);
// Save the archive to a file
archive.SaveAs("myZipFile.zip");
}
}
}Imports System
Imports System.IO
Imports IronZip
Module Program
Sub Main(args As String())
' Get all files from the specified directory
Dim fileArray As String() = Directory.GetFiles("D:\Docs\")
' Create a new ZIP archive
Using archive As New IronZipArchive()
' Iterate through each file and add it to the archive
For Each file In fileArray
archive.Add(file) ' Add files to the ZIP
Next
' Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256)
' Save the archive to a file
archive.SaveAs("myZipFile.zip")
End Using
End Sub
End ModuleImports System
Imports System.IO
Imports IronZip
Module Program
Sub Main(args As String())
' Get all files from the specified directory
Dim fileArray As String() = Directory.GetFiles("D:\Docs\")
' Create a new ZIP archive
Using archive As New IronZipArchive()
' Iterate through each file and add it to the archive
For Each file In fileArray
archive.Add(file) ' Add files to the ZIP
Next
' Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256)
' Save the archive to a file
archive.SaveAs("myZipFile.zip")
End Using
End Sub
End Module行 archive.Encrypt("myPa55word", EncryptionMethods.AES256); は、IronZIPを使用してZIPアーカイブにパスワード("myPa55word")を設定します。これにより、AES-256暗号化をアーカイブに適用してセキュリティが強化され、正しいパスワードを持つユーザーのみがコンテンツにアクセスできるようにします。 この機能は、C#アプリケーション内での保管や転送時に機密データを保護するのに有効です。 暗号化アルゴリズムの指定モードを第2パラメータに渡す必要があります。
ファイルは以下のように暗号化されます。
出力

指定されたパスからディレクトリをループしてZIPファイルを作成するデモンストレーションを見てきました。 次に、ファイルの解凍の例に進みましょう。
ZIPアーカイブからファイルを抽出する
IronZIPはC#でZIPアーカイブからファイルを抽出するメソッドを提供します。 次のコードサンプルは、ZIPアーカイブ内の圧縮ファイルを抽出します。
using IronZip;
class Program
{
static void Main(string[] args)
{
// Extract all files from the ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles");
}
}using IronZip;
class Program
{
static void Main(string[] args)
{
// Extract all files from the ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles");
}
}Imports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Extract all files from the ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles")
End Sub
End ClassImports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Extract all files from the ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles")
End Sub
End Classコード IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles"); は、IronZIPを使用してすべてのファイルを"myZipFile.zip"から抽出し、"myExtractedFiles"ディレクトリに配置します。 この簡潔なメソッドはC#でのZIPアーカイブの抽出プロセスを簡素化し、ファイル抽出タスクのための便利なソリューションを提供します。
出力
出力は次の通りです。

パスワード保護されたZIPファイルからの抽出方法
IronZIPはまた、パスワード保護されたZIPファイルを抽出するメソッドを提供します。 次のコードは、指定されたZIPファイルから既存のすべてのファイルとディレクトリを抽出するためにIronZIPメソッドを使用します。
using IronZip;
class Program
{
static void Main(string[] args)
{
// Extract all files from the password-protected ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word");
}
}using IronZip;
class Program
{
static void Main(string[] args)
{
// Extract all files from the password-protected ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word");
}
}Imports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Extract all files from the password-protected ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word")
End Sub
End ClassImports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Extract all files from the password-protected ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word")
End Sub
End ClassIronZipArchive クラスの ExtractArchiveToDirectory メソッドは、ZIPアーカイブからすべてのエントリを指定されたディレクトリに抽出します。 メソッドに渡す3つの引数:ZIPファイルのパス("myZipFile.zip")、宛先ディレクトリのパス("myExtractedFiles")、ZIPファイルのパスワード("myPa55word")。
この方法で、パスワード保護されたZIPファイルを簡単に抽出できます。
既存のアーカイブにアクセスする方法
IronZIPは、既存のアーカイブにアクセスし、ファイルに存在するすべてのエントリを表示するメソッドを提供します。
using System;
using System.Collections.Generic;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Open an existing ZIP archive with a password
using (var archive = new IronZipArchive("myZipFile.zip", "myPa55word"))
{
// Get entries list
List<string> names = archive.GetArchiveEntryNames();
// Iterate through each entry name and print it
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
}using System;
using System.Collections.Generic;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Open an existing ZIP archive with a password
using (var archive = new IronZipArchive("myZipFile.zip", "myPa55word"))
{
// Get entries list
List<string> names = archive.GetArchiveEntryNames();
// Iterate through each entry name and print it
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
}Imports System
Imports System.Collections.Generic
Imports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Open an existing ZIP archive with a password
Using archive = New IronZipArchive("myZipFile.zip", "myPa55word")
' Get entries list
Dim names As List(Of String) = archive.GetArchiveEntryNames()
' Iterate through each entry name and print it
For Each name As String In names
Console.WriteLine(name)
Next name
End Using
End Sub
End ClassImports System
Imports System.Collections.Generic
Imports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Open an existing ZIP archive with a password
Using archive = New IronZipArchive("myZipFile.zip", "myPa55word")
' Get entries list
Dim names As List(Of String) = archive.GetArchiveEntryNames()
' Iterate through each entry name and print it
For Each name As String In names
Console.WriteLine(name)
Next name
End Using
End Sub
End Class提供されたC#コードは、IronZIPを使用して、パスワード"myPa55word"で"myZipFile.zip"という名前のZIPファイルをロードすることにより、安全な IronZipArchive インスタンスを作成します。 ファイルが暗号化されていない場合、パスワードパラメータを渡さないでください。 その後、暗号化されたZIPアーカイブ内のエントリ名(ファイル名とフォルダー名)のリストを取得して印刷します。
GetArchiveEntryNames メソッドはエントリ名を収集し、foreach ループが各名前をコンソールに出力します。 この例は、IronZIPがパスワード保護されたZIPアーカイブからエントリ情報を安全にアクセスおよび取得する方法を示しています。
出力

結論
結論として、IronZIPはZIPファイルを操作するための強力で多用途なC#ライブラリであることが証明されています。 その機能は、基本的な圧縮および抽出を超えて、パスワード保護、暗号化、およびさまざまなアーカイブフォーマットとの互換性を提供します。 バックアップシステムの作成、メール添付ファイルの管理、またはインターネットからのファイルのダウンロードなど、IronZIPはこれらのタスクをシンプルかつ効率的に合理化します。
IronZIPをC#アプリケーションに統合することで、ZIPファイルを扱うための強力なツールを手に入れ、データのセキュリティを強化し、ファイル転送プロセスを最適化できます。 必要に応じて無料のお試しを利用できます。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。