首页 c# 压缩 zip rar

c# 压缩 zip rar

举报
开通vip

c# 压缩 zip rarc# 压缩 zip rar using System; using System.IO; using ICSharpCode.SharpZipLib.Zip; namespace LogPost { public class ZipClass { #region 加压 /// /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略) /// /// 被压缩的文件夹夹路径 /// 生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip...

c# 压缩 zip rar
c# 压缩 zip rar using System; using System.IO; using ICSharpCode.SharpZipLib.Zip; namespace LogPost { public class ZipClass { #region 加压 /// /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略) /// /// 被压缩的文件夹夹路径 /// 生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip /// 出错信息 /// 是否压缩成功 public bool ZipFile(string dirPath, string zipFilePath, out string err) { err = ""; if (dirPath == string.Empty) { err = "要压缩的文件夹不能为空~"; return false; } if (!Directory.Exists(dirPath)) { err = "要压缩的文件夹不存在~"; return false; } //压缩文件名为空时使用文件夹名,.zip if (zipFilePath == string.Empty) { if (dirPath.EndsWith("\\")) { dirPath = dirPath.Substring(0, dirPath.Length - 1); } zipFilePath = dirPath + ".zip"; } try { string[] filenames = Directory.GetFiles(dirPath); using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath))) { s.SetLevel(9); byte[] buffer = new byte[4096]; foreach (string file in filenames) { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } s.Finish(); s.Close(); } } catch (Exception ex) { err = ex.Message; return false; } return true; } #endregion #region 解压 方法 快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载 /// /// 功能:解压zip格式的文件。 /// /// 压缩文件路径 /// 解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 /// 出错信息 /// 解压是否成功 public bool UnZipFile(string zipFilePath, string unZipDir, out string err) { err = ""; if (zipFilePath == string.Empty) { err = "压缩文件不能为空~"; return false; } if (!File.Exists(zipFilePath)) { err = "压缩文件不存在~"; return false; } //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 if (unZipDir == string.Empty) unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath)); if (!unZipDir.EndsWith("\\")) unZipDir += "\\"; if (!Directory.Exists(unZipDir)) Directory.CreateDirectory(unZipDir); try { using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName.Length > 0) { Directory.CreateDirectory(unZipDir + directoryName); } if (!directoryName.EndsWith("\\")) directoryName += "\\"; if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } }//while } } catch (Exception ex) { err = ex.Message; return false; } return true; }//解压结束 #endregion } }
本文档为【c# 压缩 zip rar】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_321635
暂无简介~
格式:doc
大小:20KB
软件:Word
页数:7
分类:生产制造
上传时间:2017-12-12
浏览量:23