using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
namespace IStation
{
///
/// 压缩辅助类
///
public class ZipHelper
{
///
/// 压缩文件夹
///
public static void ZipFolder(string src, string dest)
{
if (File.Exists(dest))
File.Delete(dest);
var zip = new FastZip();
zip.CreateZip(dest, src, true, String.Empty);
}
///
/// 解压文件夹
///
public static void UnZipFolder(string src, string dest)
{
var zip = new FastZip();
zip.ExtractZip(src, dest, String.Empty);
}
///
/// 压缩文件
///
public static void ZipFile(string src, string dest)
{
using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(dest)))
{
s.SetLevel(6);
using (FileStream fs = System.IO.File.OpenRead(src))
{
byte[] buffer = new byte[4 * 1024];
ZipEntry entry = new ZipEntry(Path.GetFileName(src));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);//将文件写入压缩包
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
s.CloseEntry();
}
}
}
///
/// 解压文件
///
public static void UnZipFile(string src, string dest)
{
using (ZipInputStream s = new ZipInputStream(System.IO.File.OpenRead(src)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string fileName = Path.Combine(dest, Path.GetFileName(theEntry.Name));
if (fileName == string.Empty)
continue;
using (FileStream streamWriter = System.IO.File.Create(fileName))
{
int size = 4096;
byte[] data = new byte[4 * 1024];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else break;
}
}
}
}
}
}
}