• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求。

下载地址:http://icsharpcode.github.io/SharpZipLib/

1.单个或多个文件加密压缩

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();
}
/// <summary>
/// 文件加密压缩
/// </summary>
/// <param name="FileToZip">需要压缩的文件路径</param>
/// <param name="ZipedFile">压缩包路径(压缩包文件类型看自己需求)</param>
/// <param name="password">加密密码</param>
public void ZipFileMain(string FileToZip, string ZipedFile, string password)
{
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));

s.SetLevel(6); // 0 - store only to 9 - means best compression

s.Password = md5.encrypt(password);

//打开压缩文件 
FileStream fs = File.OpenRead(FileToZip);

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);

Array arr = FileToZip.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();
}

}

2.单个或多个加密压缩包解压

 1 class UnZipClass
 2     {
 3         public void UnZip(string directoryName, string ZipedFile, string password)
 4         {
 5             using (FileStream fileStreamIn = new FileStream(ZipedFile, FileMode.Open, FileAccess.Read))
 6             {
 7                 using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn))
 8                 {
 9                     zipInStream.Password = md5.encrypt(password);
10                     ZipEntry entry = zipInStream.GetNextEntry();
11                     WebContext.SqlfilePath =directoryName+"\\"+ entry.Name;
12                     do
13                     {
14                         using (FileStream fileStreamOut = new FileStream(directoryName + @"\" + entry.Name, FileMode.Create, FileAccess.Write))
15                         {
16 
17                             int size = 2048;
18                             byte[] buffer = new byte[2048];
19                             do
20                             {
21                                 size = zipInStream.Read(buffer, 0, buffer.Length);
22                                 fileStreamOut.Write(buffer, 0, size);
23                             } while (size > 0);
24                         }
25                     } while ((entry = zipInStream.GetNextEntry()) != null);
26                 }
27             }
28         }
29     }
View Code

3.Md5

 1  class md5
 2     {
 3         #region "MD5加密"
 4         /// <summary>
 5         ///32位 MD5加密
 6         /// </summary>
 7         /// <param name="str">加密字符</param>
 8         /// <returns></returns>
 9         public static string encrypt(string str)
10         {
11             string cl = str;
12             string pwd = "";
13             MD5 md5 = MD5.Create();
14             byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
15             for (int i = 0; i < s.Length; i++)
16             {
17                 pwd = pwd + s[i].ToString("X");
18             }
19             return pwd;
20         }
21         #endregion
22     }
View Code

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#SkinCrafter换肤控件的使用发布时间:2022-07-13
下一篇:
C#中采用treeview递归生成目录树(Winform和Webform两种)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap