C# 多文件压缩与解压

这个功能没什么可介绍的,大家都懂,直接上代码了。。

4c1cefaa7496f8ee03ca0b1828fa6556.png

实现功能:

    • 选择多个文件压缩成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 };
     }
 }

实现效果:

bc2e161bd648ad44b2eef60ef6bb8bf3.gif

若需要解决方案源码,请私信 文件压缩与解压 获取;

由简入繁,拿来即用

后续精彩,持续关注

欢迎关注公众号: dotnet编程大全

技术群: 需要进技术群的添加小编微信mm1552923,备注:加群;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值