using System;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Checksums;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections;
/// <summary>
/// Common 的摘要说明。
/// </summary>
public class commRar
{
public commRar()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="sourceFileNames">压缩文件名称集合</param>
/// <param name="destFileName">压缩后文件名称</param>
/// <param name="password">密码</param>
public static void zipFile(string path, string destFileName)
{
Crc32 crc = new Crc32();
string strparpath = path;
//定义
System.IO.DirectoryInfo myDir = new DirectoryInfo(path);
// 0 - store only to 9 - means best compression
if (myDir.Exists == true)
{
System.IO.FileInfo[] myFileAry = myDir.GetFiles();
ZipOutputStream s = new ZipOutputStream(File.Create(destFileName));
s.Password = "";
s.SetLevel(6);
//循环提取文件夹下每一个文件,提取信息,
foreach (FileInfo objFiles in myFileAry)
{
if (objFiles.FullName.IndexOf(".rar") > 0) continue;
FileStream fs = File.OpenRead(objFiles.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(objFiles.FullName.Substring(path.Length));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
foreach (DirectoryInfo objdire in myDir.GetDirectories())
{
zipFolder(s, objdire.FullName, crc, strparpath);
}
s.Finish();
s.Close();
}
}
private static void zipFolder(ZipOutputStream s, string path, Crc32 crc, string strparpath)
{
//定义
System.IO.DirectoryInfo myDir = new DirectoryInfo(path);
System.IO.FileInfo[] myFileAry = myDir.GetFiles();
//循环提取文件夹下每一个文件,提取信息,
foreach (FileInfo objFiles in myFileAry)
{
if (objFiles.FullName.IndexOf(".rar") > 0) continue;
FileStream fs = File.OpenRead(objFiles.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(objFiles.FullName.Substring(strparpath.Length));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
/// <summary>
/// 解压文件
/// </summary>
/// <param name="sourceFileName">被解压文件名称</param>
/// <param name="destPath">解压后文件目录</param>
/// <param name="password">密码</param>
public static void unzipFile(string sourceFileName, string destPath, string fileType)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFileName));
ZipEntry theEntry;
ArrayList al = new ArrayList();
string strRootpath = "";
while ((theEntry = s.GetNextEntry()) != null)
{
string fileName = (theEntry.Name);
if (fileName != "")
{
fileName = destPath + "\\" + fileName;
strRootpath = Path.GetDirectoryName(fileName);
if (!Directory.Exists(strRootpath))
{
Directory.CreateDirectory(strRootpath);
}
FileStream streamWriter = File.Create(fileName);
int size = 2048;
byte[] data = new byte[2048];
s.Password = "";
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
s.Close();
}
}