using System;
using System.IO;
using System.Data;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.Checksums;
using Msse.DocShare.NR_System;
namespace Msse.DocShare.Spl.Public
{
public class zip
{
public static void UnZip( error oe,string strFile, string strDir )
{
#region 说明
///
/// 功能:将一个压缩文件解压缩到一个目录下
///
/// 参数:
/// strFile:压缩文件
/// strDir:解压缩目录
///
/// 返回:
/// 无
///
#endregion
if (strDir == "") strDir = Directory.GetCurrentDirectory();
if (!strDir.EndsWith(@"/")) strDir = strDir + @"/";
ZipInputStream s = new ZipInputStream(File.OpenRead(strFile));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;
if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + @"/";
string fileName = Path.GetFileName(pathToZip);
Directory.CreateDirectory(strDir + directoryName);
if (fileName != "")
{
FileStream streamWriter = File.Create(strDir + directoryName + fileName);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
s.Close();
}
public static void ZipFile( error oe,string strSrcFile,string strZipFile )
{
#region 说明
///
/// 功能:压缩一个文件,不包含路径信息
///
/// 参数:
/// strSrcFile:待压缩文件
/// strZipFile: 压缩后文件
///
/// 返回:
/// 无
///
#endregion
string strFileName = Path.GetFileName(strSrcFile);// 文件名,不含路径信息
#region 读取文件信息
FileStream fs = File.OpenRead(strSrcFile);
long iLength = fs.Length;// 文件长度
byte[] buffer = new byte[iLength];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
#endregion
System.IO.FileStream fsZipFile = System.IO.File.Create(strZipFile);
ZipOutputStream outStream = new ZipOutputStream(fsZipFile);
ZipEntry entry = new ZipEntry( strFileName);
entry.CompressionMethod = CompressionMethod.Deflated; // deflate
entry.DateTime = DateTime.Now;
entry.Size = iLength;
#region CRC校验
Crc32 crc = new Crc32();
crc.Update(buffer);
entry.Crc = crc.Value;
#endregion
outStream.PutNextEntry(entry);
outStream.Write(buffer, 0, buffer.Length);
outStream.Finish();
outStream.Close();
}
#region 压缩多个文件
public static void ZipFile( error oe,string[] saSrcFile,string strZipFile )
{
#region 说明
///
/// 功能:将多个文件压缩成一个文件
///
/// 参数:
/// saSrcFile:多个文件,采用全路径,例:e:/tmp/tmp1/DD.cs
///
#endregion
ZipOutputStream outStream = new ZipOutputStream(File.Create(strZipFile));
Crc32 crc = new Crc32();
foreach (string strSrcFile in saSrcFile)
{
ZipFile(strSrcFile,crc,outStream);
}
outStream.Finish();
outStream.Close();
}
private static void ZipFile(string strSrcFile,Crc32 crc,ZipOutputStream outStream )
{
#region 说明
///
/// 功能:压缩一个文件,不包含路径信息
///
/// 参数:
/// strSrcFile:待压缩文件
/// strZipFile: 压缩后文件
///
///
#endregion
string strFileName = Path.GetFileName(strSrcFile);// 文件名,不含路径信息
#region 读取文件信息
FileStream fs = File.OpenRead(strSrcFile);
long iLength = fs.Length;// 文件长度
byte[] buffer = new byte[iLength];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
#endregion
ZipEntry entry = new ZipEntry( strFileName);
entry.CompressionMethod = CompressionMethod.Deflated; // deflate
entry.DateTime = DateTime.Now;
entry.Size = iLength;
#region CRC校验
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
#endregion
outStream.PutNextEntry(entry);
outStream.Write(buffer, 0, buffer.Length);
}
#endregion
}
}