このクイック記事には、** C#でフォルダーを圧縮する方法に関する情報が含まれています。フォルダのZIPファイルを作成するためのツールを作成する際に実行する詳細な手順を提供します。サードパーティのツールをインストールせずに、 C#zipフォルダー**の数行のコードとそのすべてのサブフォルダーのファイル。
C#でフォルダを圧縮する手順
- NuGetパッケージマネージャーからAspose.ZIPを追加して、フォルダーを圧縮します
- 出力ZIPファイル名を使用してFileStreamクラスオブジェクトをインスタンス化します
- ZIPArchiveファイルオブジェクトをインスタンス化します
- ターゲットフォルダ名を指定して、アーカイブにエントリを作成します
- アーカイブを保存して、すべてのファイルとサブフォルダーを含むZIPファイルを作成します
これらの手順では、最初に環境構成を提供し、次にフォルダーのZIPファイルを作成するために必要なすべての主要なクラスを紹介することにより、プロセスを詳細に説明します。完全に理解するために、* zipフォルダーC#*のコーディング手順を後で説明します。
C#でフォルダを圧縮するためのコード
using System.IO; | |
using Aspose.Zip; | |
namespace ZipFolderInCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to zip a complete folder in CSharp | |
{ | |
// Create and instantiate a license to zip as many files as required | |
// instead of 8 files only in the absence of the license | |
Aspose.Zip.License licZipFolder= new Aspose.Zip.License(); | |
licZipFolder.SetLicense("Aspose.Zip.lic"); | |
// Create a file stream object by providing the output zip file name | |
using (FileStream ZippedFolder = File.Open("AnimationImages.zip", FileMode.Create)) | |
{ | |
// Create a Zip archive file class object | |
using (Archive archiveFile = new Archive()) | |
{ | |
// Add all the files and folders recursively | |
archiveFile.CreateEntries("AnimationImages"); | |
// Save the output ZIP file | |
archiveFile.Save(ZippedFolder); | |
} | |
} | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
- C#のこれらのコード行は、フォルダー*からZIPファイルを作成します。タスクを実行するために可能ないくつかのバリエーションがあります。たとえば、ターゲットフォルダー名を指定する代わりに、出力ZIPファイルのファイルのソースとしてDirectoryInfoクラスオブジェクトを指定できます。同様に、出力ZIPファイルにルートフォルダを含めるようにフラグを設定することもできます。
このチュートリアルでは、完全なフォルダーを圧縮する方法について説明しました。逆のプロセス、つまりZIPファイルの抽出について知りたい場合は、C#でZIPファイルを抽出する方法の記事を参照してください。