c# 用DotNetZip来解压/压缩文件
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 | //https://archive.codeplex.com/?p=dotnetzip //最新在Nuget 下载DotNetZip using Ionic.Zip; private void button1_Click( object sender, EventArgs e) { string msg = "" ; ExtractFile( "D:\\1.zip" , "d:\\OK\\" ); ExtractFile( "D:\\1.zip" , "d:\\OK\\" , false , out msg); } public static bool ZipFolder( String sourceFilePath, String targetFileFullPath, Boolean isUsePassword, Int32 maxOutputSegmentSiez, out String errMessage) { try { using (ZipFile zip = new ZipFile(Encoding.Default)) { errMessage = String.Empty; zip.Comment = "压缩文件时间" + System.DateTime.Now.ToString( "G" ); zip.Name = Guid.NewGuid().ToString().ToUpper() + ".zip" ; if (isUsePassword) zip.Password = "123" ; zip.MaxOutputSegmentSize = maxOutputSegmentSiez * 1000; zip.BufferSize = 1024; zip.CaseSensitiveRetrieval = true ; zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression; zip.AddDirectory(sourceFilePath); zip.Save(targetFileFullPath); return true ; } } catch (Exception ex) { errMessage = ex.Message; return false ; } } public static bool ExtractFile(String sourceFileFullPath, String targetFolderPath, Boolean isUsePassword, out String errMessage) { try { Encoding encoding = Encoding.UTF8; errMessage = String.Empty; var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = encoding }; using (ZipFile zip = ZipFile.Read(sourceFileFullPath, options)) { if (isUsePassword) { //假设有密码123 zip.Password = "123" ; } zip.AlternateEncoding = encoding; zip.ExtractAll(targetFolderPath, ExtractExistingFileAction.OverwriteSilently); //一次批量解压 return true ; } } catch (Exception ex) { errMessage = ex.Message; return false ; } } public static bool ExtractFile(String sourceFileFullPath, String targetFolderPath) { try { Encoding encoding = Encoding.UTF8; var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = encoding }; using (ZipFile zip = ZipFile.Read(sourceFileFullPath, options)) { zip.AlternateEncoding = encoding; foreach ( var f in zip.Entries) { //一个个解压 f.Extract(targetFolderPath, ExtractExistingFileAction.OverwriteSilently); } return true ; } } catch (Exception ex) { return false ; } } |
fffffffffffffffff
test red font.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步