这个功能没什么可介绍的,大家都懂,直接上代码了。。
实现功能:
选择多个文件压缩成ZIP文件和解压ZIP文件
开发环境:
开发工具: Visual Studio 2013
.NET Framework版本:4.5
实现代码:
//需要添加ICS到自己项目 private void btnCompressFile_Click(object sender, EventArgs e) { li(); OpenFileDialog ofd = new OpenFileDialog(); o = true; if () == DialogRe) { li); } } private void btnCompress_Click(object sender, EventArgs e) { if == 0) { Me("请先选择需要压缩的文件"); return; } SaveFileDialog sfd = new SaveFileDialog(); = "压缩文件|*.zip"; if () == DialogRe) { string[] files = new string[li]; for (int i = 0; i < li; i++) { files[i] = li[i].ToString(); } dynamic result; using (ZipOutputStream outStream = new ZipOutputStream))) { result = Zip(files, outStream, "123"); } Me); } } private void btnUnCompressFile_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); = true; if () == DialogRe) { = ; } } private void btnUnCompress_Click(object sender, EventArgs e) { if ()) { Me("请先选择解压路径"); return; } OpenFileDialog ofd = new OpenFileDialog(); o = "压缩文件|*.zip"; if () == DialogRe) { dynamic result = UnZi, ,"123"); Me); } } public dynamic Zip(string[] files, ZipOutputStream outStream, string pwd) { try { for (int i = 0; i < ; i++) { if (!File.Exists(files[i])) { throw new Exception("文件不存在"); } using (FileStream fs = File.OpenRead(files[i])) { byte[] buffer = new byte[]; (buffer, 0, bu); if (!(pwd)) { ou = pwd; } ZipEntry ZipEntry = new ZipEntry(files[i])); ou(ZipEntry); ou(buffer, 0, bu); } } 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 (!Direc(outPath)) { Direc(outPath); } using (ZipInputStream zipInputStream = new ZipInputStream(zipFile))) { if (!(pwd)) { zipInpu = pwd; } ZipEntry theEntry; while ((theEntry = zi()) != null) { using (FileStream streamWriter = File.Create(outPath + "\\" + )) { byte[] data = new byte[1024 * 1024]; int dataLength = 0; while ((dataLength = zi(data, 0, da)) > 0) { (data, 0, dataLength); } } } } return new { result = true, msg = "解压成功" }; } catch (Exception ex) { return new { result = true, msg = "解压失败:" + ex.Message }; } }
实现效果:
若需要源码,请私信 文件压缩与解压 获取;
由简入繁,拿来即用
后续精彩,持续关注