C# 用SharpZipLib实现加密压缩、加密解压

      网上盛传一种代码,转的昏天暗地,尼玛一堆错误有木有!

      整了一下午,多文件压缩包就是搞不定,后来在官网上下了一个新的dll,看了源码的实现和例子,豁然开朗,神奇的写完了。谢谢那些同志的分享,但请负责人的转载!

      贴上我的代码:有问题可以相互探讨。

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 王小白.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
}

}

你可能感兴趣的:(zip)