转载于:https://www.cnblogs.com/long_/archive/2011/09/14/2176486.html
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Security.Cryptography; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; namespace 王小白.imp_exp { class ZipClass { public void ZipFile( string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize) { //不加密压缩 if (!System.IO.File.Exists(FileToZip)) { throw new System.IO.FileNotFoundException( "The specified file " + FileToZip + " could not be found. Zipping aborderd" ); } System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile); ZipOutputStream ZipStream = new ZipOutputStream(ZipFile); ZipEntry ZipEntry = new ZipEntry( "ZippedFile" ); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(CompressionLevel); byte [] buffer = new byte [BlockSize]; System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, size); try { while (size < StreamToZip.Length) { int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (System.Exception ex) { throw ex; } ZipStream.Finish(); ZipStream.Close(); StreamToZip.Close(); } public void ZipFileMain( string [] args, string password) { //加密压缩 string [] filenames = Directory.GetFiles(args[0]); ZipOutputStream s = new ZipOutputStream(File.Create(args[1])); s.SetLevel(6); // 0 - store only to 9 - means best compression s.Password = md5.encrypt(password); foreach ( string file in filenames) { //打开压缩文件 FileStream fs = File.OpenRead(file); byte [] buffer = new byte [fs.Length]; fs.Read(buffer, 0, buffer.Length); Array arr = file.Split( '\\' ); string le = arr.GetValue(arr.Length - 1).ToString(); ZipEntry entry = new ZipEntry(le); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } s.Finish(); s.Close(); } } class UnZipClass { //解压加密文件 public void UnZip( string [] args, string password) { string directoryName = Path.GetDirectoryName(args[1]); using (FileStream fileStreamIn = new FileStream(args[0], FileMode.Open, FileAccess.Read)) { using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn)) { zipInStream.Password = md5.encrypt(password); ZipEntry entry = zipInStream.GetNextEntry(); do { using (FileStream fileStreamOut = new FileStream(directoryName + @"\" + entry.Name, FileMode.Create, FileAccess.Write)) { int size = 2048; byte [] buffer = new byte [2048]; do { size = zipInStream.Read(buffer, 0, buffer.Length); fileStreamOut.Write(buffer, 0, size); } while (size > 0); } } while ((entry = zipInStream.GetNextEntry()) != null ); } } } } class md5 { #region "MD5加密" /// <summary> ///32位 MD5加密 /// </summary> /// <param name="str">加密字符</param> /// <returns></returns> public static string encrypt( string str) { string cl = str; string pwd = "" ; MD5 md5 = MD5.Create(); byte [] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); for ( int i = 0; i < s.Length; i++) { pwd = pwd + s[i].ToString( "X" ); } return pwd; } #endregion } } |
SharpZipLib下载地址:https://github.com/icsharpcode/SharpZipLib
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步