本篇内容主要讲解“怎么用C#实现多文件压缩与解压功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用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 }; } }
到此,相信大家对“怎么用C#实现多文件压缩与解压功能”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。