压缩文件

using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace day012
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//选择文件
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;//选择多个文件
string basePath = "";//路径
//判断是否选中文件
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
basePath = Path.GetDirectoryName(openFileDialog.FileName);
//创建压缩包集合
using (ZipFile zip = ZipFile.Create(basePath + "压缩.zip"))
{
zip.BeginUpdate();//开始压缩
foreach (string item in openFileDialog.FileNames)
{
zip.Add(item);
}
zip.CommitUpdate();//终止压缩
}
}
MessageBox.Show("压缩成功!");

}
}
}

原文地址:https://www.cnblogs.com/ryzryz/p/12159918.html