这里我们选用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. pandas 学习(1): pandas 数据结构之Series

    1. Series Series 是一个类数组的数据结构,同时带有标签(lable)或者说索引(index). 1.1 下边生成一个最简单的Series对象,因为没有给Series指定索引,所以此时会 ...

  2. JS 生成GUID 方法

    var Guid={NewGuid: function () { var guid = (this._G() + this._G() +"-"+ this._G() +" ...

  3. Python for Infomatics 第12章 网络编程六(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.9 词汇表 Beautif ...

  4. .NET 二维码生成(ThoughtWorks.QRCode)

    引用ThoughtWorks.QRCode.dll (源代码里有) 1.简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string n ...

  5. 【转】安装第三方库出现 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 内容如下. 然 ...

  6. React简谈

    这段时间学习了react.js,在此做个总结. react.js在优势上我认为是两个大点,第一是性能,作者从dom操作实际出发,通过虚拟dom(这里虚拟dom实际上是以jsx格式的语法来预先构建dom ...

  7. 使用TouchScript做2D按钮实现长按功能

    导入TouchScript 下载地址:https://www.assetstore.unity3d.com/#/content/7394 把TouchScript和Touch Debugger两个预设 ...

  8. springmvc+spring+hibernate

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  9. 第一个python程序

    一个python程序的两种执行方式: 1.第一种方式是通过python解释器: cmd->python->进入python解释器->编写python代码->回车. 2.第二种方 ...

  10. Nginx 和 Apache 开启目录浏览功能

    1.Nginx 在相应项目的 Server 段中的 location 段中,添加 autoindex on.例如: server { listen ; server_name www.dee.prac ...