这里我们选用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, , buffer.Length);
ZipStream.Write(buffer, , size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead = StreamToZip.Read(buffer, , buffer.Length);
ZipStream.Write(buffer, , 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(); // 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, , buffer.Length); Array arr = FileToZip.Split('\\');
string le = arr.GetValue(arr.Length - ).ToString();
ZipEntry entry = new ZipEntry(le);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
s.PutNextEntry(entry);
s.Write(buffer, , buffer.Length);
s.Finish();
s.Close();
} }

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

 class UnZipClass
{
public void UnZip(string directoryName, string ZipedFile, string password)
{
using (FileStream fileStreamIn = new FileStream(ZipedFile, FileMode.Open, FileAccess.Read))
{
using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn))
{
zipInStream.Password = md5.encrypt(password);
ZipEntry entry = zipInStream.GetNextEntry();
WebContext.SqlfilePath =directoryName+"\\"+ entry.Name;
do
{
using (FileStream fileStreamOut = new FileStream(directoryName + @"\" + entry.Name, FileMode.Create, FileAccess.Write))
{ int size = ;
byte[] buffer = new byte[];
do
{
size = zipInStream.Read(buffer, , buffer.Length);
fileStreamOut.Write(buffer, , size);
} while (size > );
}
} while ((entry = zipInStream.GetNextEntry()) != null);
}
}
}
}

3.Md5

  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 = ; i < s.Length; i++)
{
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
#endregion
}

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

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

    C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩   这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http:// ...

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

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

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

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

  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. SevenZipSharp的入门教程(包含如何加密压缩,解密压缩)

    (一)为什么选择7z              7z 是一种主流高效的压缩格式,它拥有极高的压缩比.在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式.该格式最初被7-Zip实现并采 ...

  7. tar 加密压缩和解密解压

    加密压缩 tar -czvf - file | openssl des3 -salt -k password -out /path/to/file.tar.gz 解密解压 openssl des3 - ...

  8. 利用 BASE64Encoder 对字符串进行加密 BASE64Decoder进行解密

    转自:https://blog.csdn.net/chenyongtu110/article/details/51694323

  9. 用ICSharpCode.SharpZipLib进行压缩

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

随机推荐

  1. Django+Tastypie作后端,RequireJS+Backbone作前端的TodoMVC

    一.配置好环境 接着前一篇的例子,顺带测试一下已下载下来example里面的backbone_require的例子 注意:直接本地用backbone.localStorage插件运行TodoMVC会报 ...

  2. css变形 transform【转】

    transition:过度属性 transition-property 规定设置过度效果的css属性的名称,默认可以写all transition-duration 规定完成过度效果需要多少秒或毫秒 ...

  3. shell在一个大文件找出想要的一段字符串操作技巧

    昨天端午,晚上的时候接了一个电话,我朋友的公司,数据库被两个工作没多久的phper给弄坏了,具体就是把一个字段值,给全表弄成一个了名字了,当然这个是可以配置了禁止全表更新数据库,这下可急坏了,找到我, ...

  4. 修改AspNetPager的CustomInfoHTML,添加自定义样式

    AspNetPager控件有一个属性叫CustomInfoHTML,可以把它写在前台页面,如下: <webdiyer:AspNetPager ID=" HorizontalAlign= ...

  5. 安装并配置前端自动化工具——grunt

    Grunt和所有Grunt插件都是基于nodeJs来运行的,因此在你的电脑上需要安装nodeJs.安装nodeJs非常简单,点击访问nodeJs官网https://nodejs.org,然后nodeJ ...

  6. 【转】安装第三方库出现 Python version 2.7 required, which was not found in the registry

    安装第三方库出现 Python version 2.7 required, which was not found in the registry 建立一个文件 register.py 内容如下. 然 ...

  7. 【python】安装python第三方库lxml时,遇到问题:[ERROR: 'xslt-config' 不是内部或外部命令,也不是可运行的程序]

    一.概述 lxml介绍http://lxml.de/ 二.问题 ERROR: 'xslt-config' 不是内部或外部命令,也不是可运行的程序 三.解决方法 Scrapy在Windows上的安装笔记 ...

  8. html()、text()、val()、innerHTML、value()的区分

    以上的方法可用于一般的html标签(div)与input中分别进行讨论 1.html(): jQuery方法,用于一般标签中,可读写,可以获得写入html标签. 2.text(): jQuery方法, ...

  9. Java 对象拷贝方式

    (1)BeanUtils.cloneBean()使用: http://www.cnblogs.com/fervour/archive/2009/12/18/1627868.html package c ...

  10. Javascript初学篇章_3(注释/if/switch)

    五.javascript的注释 // 单行注释 /* …*/ 多行注释 注:多行注释不能互相嵌套 六.程序流程控制 一.条件语句 if 语法: if(condition) statements1 el ...