C# 用SharpZipLib实现加密压缩、加密解压
SharpZipLib 是一个免费的Zip操作类库,可以利用它对 ZIP 等多种格式进行压缩与解压。下载网址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx。目前的版本为0.86。
1、创建zip文件,并添加文件:
{
zip.BeginUpdate();
zip.Add(@”E:\文件1.txt”);
zip.Add(@”E:\文件2.txt”);
zip.CommitUpdate();
}
2、将文件夹压缩为文件
最后一个参数是使用正则表达式表示的过滤文件规则。CreateZip方法有3个重载版本,其中有目录过滤参数、文件过滤参数及用于指定是否进行子目录递归的一个bool类型的参数。
3、将文件添加到已有zip文件中
{
zip.BeginUpdate();
zip.Add(@”E:\test.doc”);
zip.CommitUpdate();
}
4、列出zip文件中文件
{
string list = string.Empty;
foreach (ZipEntry entry in zip)
{
list += entry.Name + “\r\n”;
}
MessageBox.Show(list);
}
5、删除zip文件中的一个文件
{
zip.BeginUpdate();
zip.Delete(@”test.doc”);
zip.Delete(@”test22.txt”);
zip.CommitUpdate();
}
6、解压zip文件中文件到指定目录下
7、常用类
ZipInputStream、GZipInputStream用于解压缩Deflate、GZip格式流,ZipOutputStream、GZipOutputStream用于压缩Deflate、GZip格式流。
StreamUtil类包含了几个Stream处理辅助方法:
1) Copy(Stream, Stream, Byte[])用于从一个Stream对象中复制数据到另一Stream对象。有多个重写。
2) ReadFully(Stream, Byte [])用于从Stream对象中读取所有的byte数据。有多个重写。
代码如下:
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 XB.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 } }
123