这个功能没什么可介绍的,大家都懂,直接上代码了。。
实现功能
开发环境
开发工具: 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#文件压缩 解压内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!