c# 解压zip 进度_C#实现Zip压缩解压实例

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.IO;6 usingICSharpCode.SharpZipLib;7 usingICSharpCode.SharpZipLib.Zip;8 usingICSharpCode.SharpZipLib.Checksums;9

10 namespaceCLeopardZip11 {12 ///

13 ///适用与ZIP压缩14 ///

15 public classZipHelper16 {17 #region 压缩

18

19 ///

20 ///递归压缩文件夹的内部方法21 ///

22 /// 要压缩的文件夹路径

23 /// 压缩输出流

24 /// 此文件夹的上级文件夹

25 ///

26 private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, stringparentFolderName)27 {28 bool result = true;29 string[] folders, files;30 ZipEntry ent = null;31 FileStream fs = null;32 Crc32 crc = newCrc32();33

34 try

35 {36 ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/"));37 zipStream.PutNextEntry(ent);38 zipStream.Flush();39

40 files =Directory.GetFiles(folderToZip);41 foreach (string file infiles)42 {43 fs =File.OpenRead(file);44

45 byte[] buffer = new byte[fs.Length];46 fs.Read(buffer, 0, buffer.Length);47 ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" +Path.GetFileName(file)));48 ent.DateTime =DateTime.Now;49 ent.Size =fs.Length;50

51 fs.Close();52

53 crc.Reset();54 crc.Update(buffer);55

56 ent.Crc =crc.Value;57 zipStream.PutNextEntry(ent);58 zipStream.Write(buffer, 0, buffer.Length);59 }60

61 }62 catch

63 {64 result = false;65 }66 finally

67 {68 if (fs != null)69 {70 fs.Close();71 fs.Dispose();72 }73 if (ent != null)74 {75 ent = null;76 }77 GC.Collect();78 GC.Collect(1);79 }80

81 folders =Directory.GetDirectories(folderToZip);82 foreach (string folder infolders)83 if (!ZipDirectory(folder, zipStream, folderToZip))84 return false;85

86 returnresult;87 }88

89 ///

90 ///压缩文件夹91 ///

92 /// 要压缩的文件夹路径

93 /// 压缩文件完整路径

94 /// 密码

95 /// 是否压缩成功

96 public static bool ZipDirectory(string folderToZip, string zipedFile, stringpassword)97 {98 bool result = false;99 if (!Directory.Exists(folderToZip))100 returnresult;101

102 ZipOutputStream zipStream = newZipOutputStream(File.Create(zipedFile));103 zipStream.SetLevel(6);104 if (!string.IsNullOrEmpty(password)) zipStream.Password =password;105

106 result = ZipDirectory(folderToZip, zipStream, "");107

108 zipStream.Finish();109 zipStream.Close();110

111 returnresult;112 }113

114 ///

115 ///压缩文件夹116 ///

117 /// 要压缩的文件夹路径

118 /// 压缩文件完整路径

119 /// 是否压缩成功

120 public static bool ZipDirectory(string folderToZip, stringzipedFile)121 {122 bool result = ZipDirectory(folderToZip, zipedFile, null);123 returnresult;124 }125

126 ///

127 ///压缩文件128 ///

129 /// 要压缩的文件全名

130 /// 压缩后的文件名

131 /// 密码

132 /// 压缩结果

133 public static bool ZipFile(string fileToZip, string zipedFile, stringpassword)134 {135 bool result = true;136 ZipOutputStream zipStream = null;137 FileStream fs = null;138 ZipEntry ent = null;139

140 if (!File.Exists(fileToZip))141 return false;142

143 try

144 {145 fs =File.OpenRead(fileToZip);146 byte[] buffer = new byte[fs.Length];147 fs.Read(buffer, 0, buffer.Length);148 fs.Close();149

150 fs =File.Create(zipedFile);151 zipStream = newZipOutputStream(fs);152 if (!string.IsNullOrEmpty(password)) zipStream.Password =password;153 ent = newZipEntry(Path.GetFileName(fileToZip));154 zipStream.PutNextEntry(ent);155 zipStream.SetLevel(6);156

157 zipStream.Write(buffer, 0, buffer.Length);158

159 }160 catch

161 {162 result = false;163 }164 finally

165 {166 if (zipStream != null)167 {168 zipStream.Finish();169 zipStream.Close();170 }171 if (ent != null)172 {173 ent = null;174 }175 if (fs != null)176 {177 fs.Close();178 fs.Dispose();179 }180 }181 GC.Collect();182 GC.Collect(1);183

184 returnresult;185 }186

187 ///

188 ///压缩文件189 ///

190 /// 要压缩的文件全名

191 /// 压缩后的文件名

192 /// 压缩结果

193 public static bool ZipFile(string fileToZip, stringzipedFile)194 {195 bool result = ZipFile(fileToZip, zipedFile, null);196 returnresult;197 }198

199 ///

200 ///压缩文件或文件夹201 ///

202 /// 要压缩的路径

203 /// 压缩后的文件名

204 /// 密码

205 /// 压缩结果

206 public static bool Zip(string fileToZip, string zipedFile, stringpassword)207 {208 bool result = false;209 if(Directory.Exists(fileToZip))210 result =ZipDirectory(fileToZip, zipedFile, password);211 else if(File.Exists(fileToZip))212 result =ZipFile(fileToZip, zipedFile, password);213

214 returnresult;215 }216

217 ///

218 ///压缩文件或文件夹219 ///

220 /// 要压缩的路径

221 /// 压缩后的文件名

222 /// 压缩结果

223 public static bool Zip(string fileToZip, stringzipedFile)224 {225 bool result = Zip(fileToZip, zipedFile, null);226 returnresult;227

228 }229

230 #endregion

231

232 #region 解压

233

234 ///

235 ///解压功能(解压压缩文件到指定目录)236 ///

237 /// 待解压的文件

238 /// 指定解压目标目录

239 /// 密码

240 /// 解压结果

241 public static bool UnZip(string fileToUnZip, string zipedFolder, stringpassword)242 {243 bool result = true;244 FileStream fs = null;245 ZipInputStream zipStream = null;246 ZipEntry ent = null;247 stringfileName;248

249 if (!File.Exists(fileToUnZip))250 return false;251

252 if (!Directory.Exists(zipedFolder))253 Directory.CreateDirectory(zipedFolder);254

255 try

256 {257 zipStream = newZipInputStream(File.OpenRead(fileToUnZip));258 if (!string.IsNullOrEmpty(password)) zipStream.Password =password;259 while ((ent = zipStream.GetNextEntry()) != null)260 {261 if (!string.IsNullOrEmpty(ent.Name))262 {263 fileName =Path.Combine(zipedFolder, ent.Name);264 fileName = fileName.Replace('/', '\\');//change by Mr.HopeGi

265

266 int index = ent.Name.LastIndexOf('/');267 if (index != -1 || fileName.EndsWith("\\"))268 {269 string tmpDir = (index != -1 ? fileName.Substring(0, fileName.LastIndexOf('\\')) : fileName) + "\\";270 if (!Directory.Exists(tmpDir))271 {272 Directory.CreateDirectory(tmpDir);273 }274 if (tmpDir ==fileName)275 {276 continue;277 }278 }279

280 fs =File.Create(fileName);281 int size = 2048;282 byte[] data = new byte[size];283 while (true)284 {285 size = zipStream.Read(data, 0, data.Length);286 if (size > 0)287 fs.Write(data, 0, data.Length);288 else

289 break;290 }291 }292 }293 }294 catch

295 {296 result = false;297 }298 finally

299 {300 if (fs != null)301 {302 fs.Close();303 fs.Dispose();304 }305 if (zipStream != null)306 {307 zipStream.Close();308 zipStream.Dispose();309 }310 if (ent != null)311 {312 ent = null;313 }314 GC.Collect();315 GC.Collect(1);316 }317 returnresult;318 }319

320 ///

321 ///解压功能(解压压缩文件到指定目录)322 ///

323 /// 待解压的文件

324 /// 指定解压目标目录

325 /// 解压结果

326 public static bool UnZip(string fileToUnZip, stringzipedFolder)327 {328 bool result = UnZip(fileToUnZip, zipedFolder, null);329 returnresult;330 }331

332 #endregion

333 }334 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值