C#实现多文件压缩与解压功能
作者:Csharp 时间:2022-03-05 04:45:54
这个功能没什么可介绍的,大家都懂,直接上代码了。。
实现功能
选择多个文件压缩成ZIP文件和解压ZIP文件
开发环境
开发工具: Visual Studio 2013
.NET Framework版本:4.5
实现代码
//需要添加ICSharpCode.SharpZipLib.Zip.dll到自己项目
private void btnCompressFile_Click(object sender, EventArgs e)
{
listFiles.Items.Clear();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
listFiles.Items.AddRange(ofd.FileNames);
}
}
private void btnCompress_Click(object sender, EventArgs e)
{
if (listFiles.Items.Count == 0)
{
MessageBox.Show("请先选择需要压缩的文件");
return;
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "压缩文件|*.zip";
if (sfd.ShowDialog() == DialogResult.OK)
{
string[] files = new string[listFiles.Items.Count];
for (int i = 0; i < listFiles.Items.Count; i++)
{
files[i] = listFiles.Items[i].ToString();
}
dynamic result;
using (ZipOutputStream outStream = new ZipOutputStream(File.Create(sfd.FileName)))
{
result = Zip(files, outStream, "123");
}
MessageBox.Show(result.msg);
}
}
private void btnUnCompressFile_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() == DialogResult.OK)
{
txtOutFile.Text = fbd.SelectedPath;
}
}
private void btnUnCompress_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtOutFile.Text))
{
MessageBox.Show("请先选择解压路径");
return;
}
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "压缩文件|*.zip";
if (ofd.ShowDialog() == DialogResult.OK)
{
dynamic result = UnZip(ofd.FileName, txtOutFile.Text,"123");
MessageBox.Show(result.msg);
}
}
public dynamic Zip(string[] files, ZipOutputStream outStream, string pwd)
{
try
{
for (int i = 0; i < files.Length; i++)
{
if (!File.Exists(files[i]))
{
throw new Exception("文件不存在");
}
using (FileStream fs = File.OpenRead(files[i]))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
if (!string.IsNullOrWhiteSpace(pwd))
{
outStream.Password = pwd;
}
ZipEntry ZipEntry = new ZipEntry(Path.GetFileName(files[i]));
outStream.PutNextEntry(ZipEntry);
outStream.Write(buffer, 0, buffer.Length);
}
}
return new { result = true, msg = "压缩成功" };
}
catch (Exception ex)
{
return new { result = true, msg = "压缩失败:" + ex.Message };
}
}
public dynamic UnZip(string zipFile, string outPath, string pwd)
{
try
{
if (!Directory.Exists(outPath))
{
Directory.CreateDirectory(outPath);
}
using (ZipInputStream zipInputStream = new ZipInputStream(File.OpenRead(zipFile)))
{
if (!string.IsNullOrWhiteSpace(pwd))
{
zipInputStream.Password = pwd;
}
ZipEntry theEntry;
while ((theEntry = zipInputStream.GetNextEntry()) != null)
{
using (FileStream streamWriter = File.Create(outPath + "\\" + theEntry.Name))
{
byte[] data = new byte[1024 * 1024];
int dataLength = 0;
while ((dataLength = zipInputStream.Read(data, 0, data.Length)) > 0)
{
streamWriter.Write(data, 0, dataLength);
}
}
}
}
return new { result = true, msg = "解压成功" };
}
catch (Exception ex)
{
return new { result = true, msg = "解压失败:" + ex.Message };
}
}
实现效果
来源:https://blog.csdn.net/qq_27410185/article/details/121975741
标签:C#,文件,压缩,解压
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java开发之spring security实现基于MongoDB的认证功能
2022-06-29 15:52:32
C# 修改文件的创建、修改和访问时间的示例
2023-06-09 23:18:50
MyBatis查询时属性名和字段名不一致问题的解决方法
2023-10-23 16:56:36
解决idea 暂存文件或idea切换分支代码丢失的问题
2023-09-11 07:52:41
![](https://img.aspxhome.com/file/2023/0/66220_0s.jpg)
Feign调用可重试的最佳方案分享
2021-06-09 12:19:23
Java实现简单的扫雷小程序
2022-11-18 02:14:07
![](https://img.aspxhome.com/file/2023/6/63806_0s.jpg)
Mybatis环境配置及测试详解
2023-11-24 07:06:25
java swing实现简单的五子棋游戏
2022-04-16 22:10:10
![](https://img.aspxhome.com/file/2023/4/83124_0s.jpg)
剑指Offer之Java算法习题精讲链表专项训练
2023-11-29 16:31:48
![](https://img.aspxhome.com/file/2023/1/60611_0s.png)
spring mvc url匹配禁用后缀访问操作
2021-11-19 23:57:34
java 键盘输入一个数,输出数组中指定元素的示例
2023-11-24 20:31:14
Mybatis基础概念与高级应用小结
2023-11-26 20:18:22
![](https://img.aspxhome.com/file/2023/8/59988_0s.png)
c# 空合并运算符“??”的使用详解
2022-11-23 13:47:36
Java实现五子棋AI算法
2022-02-23 05:32:57
简单了解java标识符的作用和命名规则
2022-06-18 17:49:09
![](https://img.aspxhome.com/file/2023/1/72601_0s.jpg)
Java @Autowired报错原因分析和4种解决方案
2022-10-30 04:22:03
![](https://img.aspxhome.com/file/2023/6/79286_0s.webp)
c#字符串编码编码(encoding)使用方法示例
2022-10-04 07:24:58
Java图形用户界面设计(Swing)的介绍
2022-08-23 03:29:37
![](https://img.aspxhome.com/file/2023/4/75654_0s.jpg)
Java获取接口所有实现类的方式详解
2022-06-11 14:44:27
C#中LINQ多条件JOIN时为什么可以使用匿名类
2023-01-24 15:38:21
![](https://img.aspxhome.com/file/2023/5/84435_0s.png)