这篇文章主要为大家详细介绍了如何利用C#语言实现多文件压缩与解压功能,即选择多个文件压缩成ZIP文件和解压ZIP文件,需要的可以参考一下
这个功能没什么可介绍的,大家都懂,直接上代码了。。
实现功能
选择多个文件压缩成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 };
}
}
实现效果
到此这篇关于C#实现多文件压缩与解压功能的文章就介绍到这了,更多相关C#文件压缩 解压内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
沃梦达教程
本文标题为:C#实现多文件压缩与解压功能
基础教程推荐
猜你喜欢
- C#控制台实现飞行棋小游戏 2023-04-22
- unity实现动态排行榜 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- winform把Office转成PDF文件 2023-06-14
- ZooKeeper的安装及部署教程 2023-01-22
- C#类和结构详解 2023-05-30
- C# windows语音识别与朗读实例 2023-04-27
- C# List实现行转列的通用方案 2022-11-02
- C# 调用WebService的方法 2023-03-09
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26