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

 

这里我们选用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     }

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     }

C# 文件压缩加解密

 

1.这种方式也可以做到对文件的加密压缩,解密解压缩,只是在压缩和解压缩时会出现压缩窗口

1.1加密压缩

strzipPath:压缩包路径
strtxtPath:待压缩的文件路径
password:加密密码
public bool Zip(string strzipPath, string strtxtPath,string password)
        {
            try
            {
                System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
                Process1.StartInfo.FileName = "Winrar.exe";
                Process1.StartInfo.CreateNoWindow = true;
                Process1.StartInfo.Arguments = " a -p" + password + " " + strzipPath + " " + strtxtPath;
                //strtxtPath = "c://freezip//";
                //Process1.StartInfo.Arguments = " x -p123456 " + strzipPath + " " + strtxtPath;
                Process1.Start();
                if (Process1.HasExited)
                {
                    return true;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

1.2解密解压

 1  public bool UZip(string strzipPath, string strtxtPath,string password)
 2         {
 3             try
 4             {
 5                 System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
 6                 Process1.StartInfo.FileName = "Winrar.exe";
 7                 Process1.StartInfo.CreateNoWindow = true;
 8                 //Process1.StartInfo.Arguments = " a -p123456 " + strzipPath + " " + strtxtPath;
 9                 //strtxtPath = "c://freezip//";
10                 Process1.StartInfo.Arguments = " x -p" + password + " " + strzipPath + " " + strtxtPath;
11                 Process1.Start();
12                 if (Process1.HasExited)
13                 {
14                     return true;
15                 }
16                 return true;
17             }
18             catch (Exception)
19             {
20
21                 return false;
22             }
23
24         }

C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 C# 文件压缩加解密的更多相关文章

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

    这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http://icsharpcode.github.io/SharpZipLib/ 1.单个或多个文件加 ...

  2. C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件

    我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...

  3. 利用ICSharpCode.SharpZipLib.Zip进行文件压缩

    官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...

  4. 利用ICSharpCode.SharpZipLib进行压缩

    #ZipLib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is im ...

  5. C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩

    ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...

  6. PHP和.NET通用的加密解密函数类,均使用3DES加解密 .

    以下为php代码 <PRE class=PHP name="code"> </PRE><PRE class=PHP name="code&q ...

  7. 你想不到的压缩方法:将javascript文件压缩成PNG图像存储

    这样可以做到很高的压缩比,到底有多高,下面会提到.这种方法用到了 canvas 控件,这也意味着只有支持 canvas 控件的浏览器下才有效. 现在你可以看到,上面的图像类似一个噪声图像,但它实际上是 ...

  8. java加密工具类,可设置对应的加解密key

    public class AesEncryptUtil { //使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同! private static String KEY =& ...

  9. 用ICSharpCode.SharpZipLib进行压缩

    今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...

随机推荐

  1. python3 保存一个网页为html文件

    我使用的python版本为3.5.2. 最近租房子,恨透了中介,想绕过中介去租.结果发现豆瓣同城里有好多二房东,感觉人都还不错.但是豆瓣这里没有信息检索的功能,只能人工地看房子的地址,非常地不方便.所 ...

  2. 【Luogu】P3174毛毛虫(树形DP)

    题目链接 树形DP水题,设f[x][0]是以x为根的子树,内部只有半条链(就是链的两个端点一个在子树里,一个不在子树里)的最大值,f[x][1]是以x为根的子树,内部有一条完整的链(选两个内部的子树作 ...

  3. vue 当中出现dom操作

    在mounted当中进行dom相关操作 this.$refs

  4. 应用css3制作loading效果

    参考秒味课堂 代码发出来备忘 html <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  5. 防止cpu 一直被占用 sleep(0) 和 yield

    在java的Thread类中有两个有用的函数,sleep和yield,sleep就是线程睡眠一定的时间,也就是交出cpu一段时间,yield用来暗示系统交出cpu控制权.这两个函数在多线程开发的时候特 ...

  6. hdu 5444(构造二叉树然后遍历)

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. 如何解决div里面img图片下方有空白的问题?

    空白产生原因: 图片默认的vertical-align是baseline,(元素放置在父元素的基线上,也就是xx下边距) 一个inline-block元素,如果里面没有inline元素,或者overf ...

  8. HDU 5988最小网络流(浮点数)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 哇,以前的模版一直T,加了优先队列优化才擦边过. 建图很好建,概率乘法化成概率加法不 ...

  9. HDU 5266 pog loves szh III(区间LCA)

    题目链接 pog loves szh III 题意就是  求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...

  10. 2016Unite Shanghai 总结

    有幸参加了Unite 2016 Shanghai unity开发者大会,这里做一些简单总结 一.日本Marza 分享 <The Gift> Marza用unity做渲染,加上一些自己的扩展 ...