C#实现多文件压缩与解压功能
作者:Csharp 发布时间:2022-03-05 04:45:54
标签:C#,文件,压缩,解压
这个功能没什么可介绍的,大家都懂,直接上代码了。。
实现功能
选择多个文件压缩成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#使用文件流读取文件的方法。分享给大家供大家参考。具体如下:using System;using System.IO;nam
- 前言:小伙伴说能不能用springboot整合一下mybatis多数据源不使用JPA进行数据库连接操作。那么说干就干创建一个springbo
- 本文实例讲述了Java实现整数分解质因数的方法。分享给大家供大家参考,具体如下:题目内容:每个非素数(合数)都可以写成几个素数(也可称为质数
- 1、spring-cloud-starter-alibaba-nacos-discovery 这里依赖报红,无法引入,或显示无法找到,更换版
- 本文实例为大家分享了Unity shader实现遮罩效果的具体代码,供大家参考,具体内容如下效果:shader代码:Shader "
- 前言容器是用于存放数据的载体。容器分为数组、集合。 Kotlin 作为一门全新的语言,肯定还是要有自己的容器类,不然哪天 Java 跟 Ko
- 本文为大家分享了Unity3D实现虚拟按钮控制人物移动的具体代码,供大家参考,具体内容如下创建Image的UI组件,在Image下新建一个B
- 1.基本概念首先我们需要弄清楚几个概念:面向对象是什么、类是什么、对象又是什么?还是逐个来说1.1面向对象我们常说Java是面向对象的语言,
- 文件上传大部分通过web前端判断后尾名或者service后端判断后尾名,这种操作具有一定的风险,比如:我可以将一个jsp页面,修改后尾名改成
- 功能介绍功能:群聊+私发+上线提醒+下线提醒+查询在线用户文件Utils需要用maven导入下面两个包 <dependency>
- C#申请一个大数组(Use a large array in C#)在C#里,有时候我需要能够申请一个很大的数组、使用之、然后立即释放其占用
- 本文实例讲述了java实现的RSA加密算法。分享给大家供大家参考,具体如下:一、什么是非对称加密1、加密的密钥与加密的密钥不相同,这样的加密
- 使用第三方json转换工具,阿里巴巴json转换工具Fastjson1.2.7。https://www.jb51.net/softs/530
- 应用启动的时候有短暂的白屏,如图:可以通过设置theme的方式来解决 <style name="AppTheme"
- 目录一、.NET 体系结构二、Hello world三、类型和变量四、程序结构前言:C#(读作“See Sharp”)是一种新式编程语言,不
- 欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demo
- 本文实例讲述了C#使用foreach语句遍历队列(Queue)的方法。分享给大家供大家参考。具体如下:using System;using
- 一、spring-boot-devtools在pom中直接引入依赖<dependency> <groupId&
- 目录存储权限内部存储 外部存储适配存储权限Android Q 仍然使用 READ_EXTRNAL_STORAGE 和 WRITE_EXTRN
- 在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载。所以自己写了一个压缩文件的工具类。该工具类支持单个文件和文件夹压缩。放代码: