准备工作: 在程序包管理器控制台安装 Install-Package SharpZipLib 引用 using ICSharpCode.SharpZipLib.Zip;
页面:
代码如下:
//打开要压缩的文件
private void button1_Click(object sender, EventArgs e)
{
//打开文件
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog()==DialogResult.OK)
{
textBox1.Text = openFile.FileName;
}
}
//选择压缩后保存的路径
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog openFile2 = new FolderBrowserDialog();
if (openFile2.ShowDialog()==DialogResult.OK)
{
textBox2.Text = openFile2.SelectedPath+@" ext.Zip";
}
}
//确认压缩
private void button3_Click(object sender, EventArgs e)
{
//测试保存的路径是否正确
MessageBox.Show(textBox2.Text);
using (ZipFile zip = ZipFile.Create(textBox2.Text))
{
zip.BeginUpdate();
zip.Add(textBox1.Text);
zip.CommitUpdate();
}
}