.NET Framework 4.5 以前では DotNetZip ライブラリを利用して圧縮処理を実装できます。詳細はこちらの記事を参照してください。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
namespace DotNetZipArchive
{
public partial class FormStreamZipArchive : Form
{
public FormStreamZipArchive()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("test.zip", FileMode.CreateNew);
ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Create);
ZipArchiveEntry zae = za.CreateEntry("file1.txt");
StreamWriter sw = new StreamWriter(zae.Open());
sw.WriteLine("ぺんぎんクッキー");
sw.WriteLine("らくだキャラメル");
sw.WriteLine("しろくまアイス");
sw.WriteLine("あひるサブレ");
sw.WriteLine("ぺりかんマカロン");
sw.Close();
za.Dispose();
fs.Close();
}
}
}
FileStream fs = new FileStream("test.zip", FileMode.CreateNew);
ZipArchiveMode.Create
を与えます。 ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Create);
ZipArchiveEntry zae = za.CreateEntry("file1.txt");
CreateEntry メソッドで作成された ZipArchiveEntry オブジェクトのOpen メソッドを呼び出します。Zipアーカイブ内の圧縮ファイルのストリームオブジェクトが返されますので、
ストリームオブジェクトの WriteLine メソッドを呼び出してZipアーカイブ内のファイルに情報を書き込みます。 StreamWriter sw = new StreamWriter(zae.Open());
sw.WriteLine("ぺんぎんクッキー");
sw.WriteLine("らくだキャラメル");
sw.WriteLine("しろくまアイス");
sw.WriteLine("あひるサブレ");
sw.WriteLine("ぺりかんマカロン");
sw.Close();
za.Dispose();
fs.Close();
private void button2_Click(object sender, EventArgs e)
{
using (FileStream fs = new FileStream("test.zip", FileMode.CreateNew)) {
using (ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Create)) {
ZipArchiveEntry zae = za.CreateEntry("file1.txt");
using (StreamWriter sw = new StreamWriter(zae.Open())) {
sw.WriteLine("ぺんぎんクッキー");
sw.WriteLine("らくだキャラメル");
sw.WriteLine("しろくまアイス");
sw.WriteLine("あひるサブレ");
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("test.zip", FileMode.CreateNew);
try {
ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Create);
try {
ZipArchiveEntry zae = za.CreateEntry("file1.txt");
StreamWriter sw = null;
try {
sw = new StreamWriter(zae.Open());
sw.WriteLine("ぺんぎんクッキー");
sw.WriteLine("らくだキャラメル");
sw.WriteLine("しろくまアイス");
sw.WriteLine("あひるサブレ");
sw.WriteLine("ぺりかんマカロン");
}
finally {
sw.Close();
}
}
finally {
za.Dispose();
}
}
finally {
fs.Close();
}
}
Multiselect
プロパティを Trueに変更します。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
namespace DotNetZipArchive
{
public partial class FormFileZipArchive : Form
{
public FormFileZipArchive()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
if (File.Exists("test.zip") == true) File.Delete("test.zip");
FileStream fs = new FileStream("test.zip", FileMode.CreateNew);
ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Create);
foreach (string filename in openFileDialog1.FileNames) {
za.CreateEntryFromFile(filename, Path.GetFileName(filename));
textBox1.Text += filename+"を圧縮しました\r\n";
}
za.Dispose();
fs.Close();
textBox1.Text += "圧縮処理が終了しました\r\n";
}
}
}
}
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
//中略
}
既にtest.zipファイルが存在している場合、ストリームを開いた際にIOException例外が発生するため、test.zipファイルが存在する場合はファイルを削除します。 if (File.Exists("test.zip") == true) File.Delete("test.zip");
FileStream fs = new FileStream("test.zip", FileMode.CreateNew);
ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Create);
foreach (string filename in openFileDialog1.FileNames) {
za.CreateEntryFromFile(filename, Path.GetFileName(filename));
textBox1.Text += filename+"を圧縮しました\r\n";
}
za.Dispose();
fs.Close();
textBox1.Text += "圧縮処理が終了しました\r\n";
ZipFile.CreateFromDirectory()
メソッドを利用します。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
namespace DotNetZipArchive
{
public partial class FormDirectoryZipArchive : Form
{
public FormDirectoryZipArchive()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
if (File.Exists("test.zip") == true) File.Delete("test.zip");
ZipFile.CreateFromDirectory(folderBrowserDialog1.SelectedPath, "test.zip");
textBox1.Text = folderBrowserDialog1.SelectedPath + "ディレクトリのファイル圧縮しました";
}
}
}
}
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
/* 中略 */
}
if (File.Exists("test.zip") == true) File.Delete("test.zip");
ZipFile.CreateFromDirectory()
メソッドを呼び出します。第一引数に圧縮するディレクトリのパスを与えます。
第二引数に出力先のZIPファイル名を与えます。今回は FolderBrowserダイアログで指定したディレクトリをCreateFromDirectory メソッドの
第一引数に与え、第二引数には "test.zip" を与えて実行ファイルを配置しているパスの位置に test.zip ファイルを作成します。 ZipFile.CreateFromDirectory(folderBrowserDialog1.SelectedPath, "test.zip");
textBox1.Text = folderBrowserDialog1.SelectedPath + "ディレクトリのファイル圧縮しました";
c:\data\comp
フォルダに配置します。c:\data\comp
フォルダを選択します。