标签:evel ring 压缩文件 logs com entry write compress set
//压缩类 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); } } } }
//MD5加密类 class 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; } }
标签:evel ring 压缩文件 logs com entry write compress set
原文地址:http://www.cnblogs.com/mqxs/p/7450107.html