(转)Unity中使用C#实现Zip包的压缩与解压
使用SharpZipLib库,下载地址为:http://icsharpcode.github.io/SharpZipLib/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | /****************************************************** * DESCRIPTION: Zip包的压缩与解压 * * Copyright (c) 2017, 谭伟俊 (TanWeijun) * All rights reserved * * CREATED: 2017.03.11, 08:37, CST ******************************************************/ using System.IO; using System.Collections; using UnityEngine; using ICSharpCode.SharpZipLib.Zip; public static class ZipUtility { #region ZipCallback public abstract class ZipCallback { /// <summary> /// 压缩单个文件或文件夹前执行的回调 /// </summary> /// <param name="_entry"></param> /// <returns>如果返回true,则压缩文件或文件夹,反之则不压缩文件或文件夹</returns> public virtual bool OnPreZip(ZipEntry _entry) { return true ; } /// <summary> /// 压缩单个文件或文件夹后执行的回调 /// </summary> /// <param name="_entry"></param> public virtual void OnPostZip(ZipEntry _entry) {} /// <summary> /// 压缩执行完毕后的回调 /// </summary> /// <param name="_result">true表示压缩成功,false表示压缩失败</param> public virtual void OnFinished( bool _result) {} } #endregion #region UnzipCallback public abstract class UnzipCallback { /// <summary> /// 解压单个文件或文件夹前执行的回调 /// </summary> /// <param name="_entry"></param> /// <returns>如果返回true,则压缩文件或文件夹,反之则不压缩文件或文件夹</returns> public virtual bool OnPreUnzip(ZipEntry _entry) { return true ; } /// <summary> /// 解压单个文件或文件夹后执行的回调 /// </summary> /// <param name="_entry"></param> public virtual void OnPostUnzip(ZipEntry _entry) {} /// <summary> /// 解压执行完毕后的回调 /// </summary> /// <param name="_result">true表示解压成功,false表示解压失败</param> public virtual void OnFinished( bool _result) {} } #endregion /// <summary> /// 压缩文件和文件夹 /// </summary> /// <param name="_fileOrDirectoryArray">文件夹路径和文件名</param> /// <param name="_outputPathName">压缩后的输出路径文件名</param> /// <param name="_password">压缩密码</param> /// <param name="_zipCallback">ZipCallback对象,负责回调</param> /// <returns></returns> public static bool Zip( string [] _fileOrDirectoryArray, string _outputPathName, string _password = null , ZipCallback _zipCallback = null ) { if (( null == _fileOrDirectoryArray) || string .IsNullOrEmpty(_outputPathName)) { if ( null != _zipCallback) _zipCallback.OnFinished( false ); return false ; } ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(_outputPathName)); zipOutputStream.SetLevel(6); // 压缩质量和压缩速度的平衡点 if (! string .IsNullOrEmpty(_password)) zipOutputStream.Password = _password; for ( int index = 0; index < _fileOrDirectoryArray.Length; ++index) { bool result = false ; string fileOrDirectory = _fileOrDirectoryArray[index]; if (Directory.Exists(fileOrDirectory)) result = ZipDirectory(fileOrDirectory, string .Empty, zipOutputStream, _zipCallback); else if (File.Exists(fileOrDirectory)) result = ZipFile(fileOrDirectory, string .Empty, zipOutputStream, _zipCallback); if (!result) { if ( null != _zipCallback) _zipCallback.OnFinished( false ); return false ; } } zipOutputStream.Finish(); zipOutputStream.Close(); if ( null != _zipCallback) _zipCallback.OnFinished( true ); return true ; } /// <summary> /// 解压Zip包 /// </summary> /// <param name="_filePathName">Zip包的文件路径名</param> /// <param name="_outputPath">解压输出路径</param> /// <param name="_password">解压密码</param> /// <param name="_unzipCallback">UnzipCallback对象,负责回调</param> /// <returns></returns> public static bool UnzipFile( string _filePathName, string _outputPath, string _password = null , UnzipCallback _unzipCallback = null ) { if ( string .IsNullOrEmpty(_filePathName) || string .IsNullOrEmpty(_outputPath)) { if ( null != _unzipCallback) _unzipCallback.OnFinished( false ); return false ; } try { return UnzipFile(File.OpenRead(_filePathName), _outputPath, _password, _unzipCallback); } catch (System.Exception _e) { Debug.LogError( "[ZipUtility.UnzipFile]: " + _e.ToString()); if ( null != _unzipCallback) _unzipCallback.OnFinished( false ); return false ; } } /// <summary> /// 解压Zip包 /// </summary> /// <param name="_fileBytes">Zip包字节数组</param> /// <param name="_outputPath">解压输出路径</param> /// <param name="_password">解压密码</param> /// <param name="_unzipCallback">UnzipCallback对象,负责回调</param> /// <returns></returns> public static bool UnzipFile( byte [] _fileBytes, string _outputPath, string _password = null , UnzipCallback _unzipCallback = null ) { if (( null == _fileBytes) || string .IsNullOrEmpty(_outputPath)) { if ( null != _unzipCallback) _unzipCallback.OnFinished( false ); return false ; } bool result = UnzipFile( new MemoryStream(_fileBytes), _outputPath, _password, _unzipCallback); if (!result) { if ( null != _unzipCallback) _unzipCallback.OnFinished( false ); } return result; } /// <summary> /// 解压Zip包 /// </summary> /// <param name="_inputStream">Zip包输入流</param> /// <param name="_outputPath">解压输出路径</param> /// <param name="_password">解压密码</param> /// <param name="_unzipCallback">UnzipCallback对象,负责回调</param> /// <returns></returns> public static bool UnzipFile(Stream _inputStream, string _outputPath, string _password = null , UnzipCallback _unzipCallback = null ) { if (( null == _inputStream) || string .IsNullOrEmpty(_outputPath)) { if ( null != _unzipCallback) _unzipCallback.OnFinished( false ); return false ; } // 创建文件目录 if (!Directory.Exists(_outputPath)) Directory.CreateDirectory(_outputPath); // 解压Zip包 ZipEntry entry = null ; using (ZipInputStream zipInputStream = new ZipInputStream(_inputStream)) { if (! string .IsNullOrEmpty(_password)) zipInputStream.Password = _password; while ( null != (entry = zipInputStream.GetNextEntry())) { if ( string .IsNullOrEmpty(entry.Name)) continue ; if (( null != _unzipCallback) && !_unzipCallback.OnPreUnzip(entry)) continue ; // 过滤 string filePathName = Path.Combine(_outputPath, entry.Name); // 创建文件目录 if (entry.IsDirectory) { Directory.CreateDirectory(filePathName); continue ; } // 写入文件 try { using (FileStream fileStream = File.Create(filePathName)) { byte [] bytes = new byte [1024]; while ( true ) { int count = zipInputStream.Read(bytes, 0, bytes.Length); if (count > 0) fileStream.Write(bytes, 0, count); else { if ( null != _unzipCallback) _unzipCallback.OnPostUnzip(entry); break ; } } } } catch (System.Exception _e) { Debug.LogError( "[ZipUtility.UnzipFile]: " + _e.ToString()); if ( null != _unzipCallback) _unzipCallback.OnFinished( false ); return false ; } } } if ( null != _unzipCallback) _unzipCallback.OnFinished( true ); return true ; } /// <summary> /// 压缩文件 /// </summary> /// <param name="_filePathName">文件路径名</param> /// <param name="_parentRelPath">要压缩的文件的父相对文件夹</param> /// <param name="_zipOutputStream">压缩输出流</param> /// <param name="_zipCallback">ZipCallback对象,负责回调</param> /// <returns></returns> private static bool ZipFile( string _filePathName, string _parentRelPath, ZipOutputStream _zipOutputStream, ZipCallback _zipCallback = null ) { //Crc32 crc32 = new Crc32(); ZipEntry entry = null ; FileStream fileStream = null ; try { string entryName = _parentRelPath + '/' + Path.GetFileName(_filePathName); entry = new ZipEntry(entryName); entry.DateTime = System.DateTime.Now; if (( null != _zipCallback) && !_zipCallback.OnPreZip(entry)) return true ; // 过滤 fileStream = File.OpenRead(_filePathName); byte [] buffer = new byte [fileStream.Length]; fileStream.Read(buffer, 0, buffer.Length); fileStream.Close(); entry.Size = buffer.Length; //crc32.Reset(); //crc32.Update(buffer); //entry.Crc = crc32.Value; _zipOutputStream.PutNextEntry(entry); _zipOutputStream.Write(buffer, 0, buffer.Length); } catch (System.Exception _e) { Debug.LogError( "[ZipUtility.ZipFile]: " + _e.ToString()); return false ; } finally { if ( null != fileStream) { fileStream.Close(); fileStream.Dispose(); } } if ( null != _zipCallback) _zipCallback.OnPostZip(entry); return true ; } /// <summary> /// 压缩文件夹 /// </summary> /// <param name="_path">要压缩的文件夹</param> /// <param name="_parentRelPath">要压缩的文件夹的父相对文件夹</param> /// <param name="_zipOutputStream">压缩输出流</param> /// <param name="_zipCallback">ZipCallback对象,负责回调</param> /// <returns></returns> private static bool ZipDirectory( string _path, string _parentRelPath, ZipOutputStream _zipOutputStream, ZipCallback _zipCallback = null ) { ZipEntry entry = null ; try { string entryName = Path.Combine(_parentRelPath, Path.GetFileName(_path) + '/' ); entry = new ZipEntry(entryName); entry.DateTime = System.DateTime.Now; entry.Size = 0; if (( null != _zipCallback) && !_zipCallback.OnPreZip(entry)) return true ; // 过滤 _zipOutputStream.PutNextEntry(entry); _zipOutputStream.Flush(); string [] files = Directory.GetFiles(_path); for ( int index = 0; index < files.Length; ++index) ZipFile(files[index], Path.Combine(_parentRelPath, Path.GetFileName(_path)), _zipOutputStream, _zipCallback); } catch (System.Exception _e) { Debug.LogError( "[ZipUtility.ZipDirectory]: " + _e.ToString()); return false ; } string [] directories = Directory.GetDirectories(_path); for ( int index = 0; index < directories.Length; ++index) { if (!ZipDirectory(directories[index], Path.Combine(_parentRelPath, Path.GetFileName(_path)), _zipOutputStream, _zipCallback)) return false ; } if ( null != _zipCallback) _zipCallback.OnPostZip(entry); return true ; } } |
作者:EnigmaJJ
链接:http://www.jianshu.com/p/acc3d79d93f7
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步