博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#压缩文件为zip格式
阅读量:4486 次
发布时间:2019-06-08

本文共 5718 字,大约阅读时间需要 19 分钟。

需要ICSharpCode.SharpZipLib.dll,网上下载的到。

代码是从网上找来的:

1 public class ZipClass  2     {  3         #region 加压  4         ///   5         /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略)  6         ///   7         ///  被压缩的文件夹夹路径   8         ///  生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名 +.zip  9         ///  出错信息 10         /// 
是否压缩成功
11 static public bool ZipFile( string dirPath, string zipFilePath, out string err) 12 { 13 err = ""; 14 if (dirPath == string .Empty) 15 { 16 err = "要压缩的文件夹不能为空! "; 17 return false ; 18 } 19 if (!Directory .Exists(dirPath)) 20 { 21 err = "要压缩的文件夹不存在! "; 22 return false ; 23 } 24 //压缩文件名为空时使用文件夹名+ zip 25 if (zipFilePath == string .Empty) 26 { 27 if (dirPath.EndsWith("\\" )) 28 { 29 dirPath = dirPath.Substring(0, dirPath.Length - 1); 30 } 31 zipFilePath = dirPath + ".zip"; 32 } 33 34 try 35 { 36 string[] filenames = Directory .GetFiles(dirPath); 37 using (ZipOutputStream s = new ZipOutputStream(File .Create(zipFilePath))) 38 { 39 s.SetLevel(9); 40 byte[] buffer = new byte[4096]; 41 foreach (string file in filenames) 42 { 43 ZipEntry entry = new ZipEntry( Path.GetFileName(file)); 44 entry.DateTime = DateTime.Now; 45 s.PutNextEntry(entry); 46 using (FileStream fs = File.OpenRead(file)) 47 { 48 int sourceBytes; 49 do 50 { 51 sourceBytes = fs.Read(buffer, 0, buffer.Length); 52 s.Write(buffer, 0, sourceBytes); 53 } while (sourceBytes > 0); 54 } 55 } 56 s.Finish(); 57 s.Close(); 58 } 59 } 60 catch (Exception ex) 61 { 62 err = ex.Message; 63 return false ; 64 } 65 return true ; 66 } 67 68 #endregion 69 70 #region 解压方法 71 /// 72 /// 功能:解压 zip格式的文件。 73 /// 74 /// 压缩文件路径 75 /// 解压文件存放路径 ,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 76 /// 出错信息 77 ///
解压是否成功
78 static public bool UnZipFile( string zipFilePath, string unZipDir, out string err) 79 { 80 err = ""; 81 if (zipFilePath == string .Empty) 82 { 83 err = "压缩文件不能为空!"; 84 return false ; 85 } 86 if (!File .Exists(zipFilePath)) 87 { 88 err = "压缩文件不存在!"; 89 return false ; 90 } 91 //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 92 if (unZipDir == string .Empty) 93 unZipDir = zipFilePath.Replace( Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath)); 94 if (!unZipDir.EndsWith("\\" )) 95 unZipDir += "\\"; 96 if (!Directory .Exists(unZipDir)) 97 Directory.CreateDirectory(unZipDir); 98 99 try100 {101 using (ZipInputStream s = new ZipInputStream(File .OpenRead(zipFilePath)))102 {103 104 ZipEntry theEntry;105 while ((theEntry = s.GetNextEntry()) != null)106 {107 string directoryName = Path .GetDirectoryName(theEntry.Name);108 string fileName = Path .GetFileName(theEntry.Name);109 if (directoryName.Length > 0)110 {111 Directory.CreateDirectory(unZipDir + directoryName);112 }113 if (!directoryName.EndsWith("\\"))114 directoryName += "\\";115 if (fileName != String .Empty)116 {117 using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))118 {119 120 int size = 2048;121 byte[] data = new byte[2048];122 while (true )123 {124 size = s.Read(data, 0, data.Length);125 if (size > 0)126 {127 streamWriter.Write(data, 0, size);128 }129 else130 {131 break;132 }133 }134 }135 }136 } //while137 }138 }139 catch (Exception ex)140 {141 err = ex.Message;142 return false ;143 }144 return true ;145 } //解压结束146 #endregion147 }

 

转载于:https://www.cnblogs.com/Vercher/archive/2012/09/29/2707964.html

你可能感兴趣的文章
Android之条码扫描二维码扫描
查看>>
C++ ofstream和ifstream
查看>>
方法--动手又动脑 2018/10/14
查看>>
工作日志WebRoot--时间插件弹出层被遮挡
查看>>
常用的按键/输入口检测程序
查看>>
清晰易懂!关于PS入门的超详细笔记!
查看>>
linux下系统对于sigsegv错误时的处理
查看>>
Bat脚本学习-5:Oracle自动备份还原脚本
查看>>
洛谷 P4248: bzoj 3238: [AHOI2013]差异
查看>>
PHP中利用PHPMailer配合QQ邮箱实现发邮件
查看>>
sql 同一张表查询不同数据合并之后关联查询
查看>>
浅谈http请求数据分析
查看>>
linux下错误的捕获:errno和strerror的使用
查看>>
LeetCode | 区间合并
查看>>
Vue实现对数组、对象的深拷贝、复制
查看>>
linux之epoll
查看>>
分区分表
查看>>
快速将yum/dnf/apt-get等默认源改为国内源
查看>>
js & Number & String
查看>>
关于C++ const 的全面总结
查看>>