using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
namespace Common.system
{
///
/// 适用与ZIP压缩
///
public class ZipHelper
{
#region 压缩
///
/// 递归压缩文件夹的内部方法
///
/// 要压缩的文件夹路径
/// 压缩输出流
/// 此文件夹的上级文件夹
///
private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName)
{
bool result = true;
string[] folders, files;
ZipEntry ent = null;
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/"));
zipStream.PutNextEntry(ent);
zipStream.Flush();
files = Directory.GetFiles(folderToZip);
foreach (string file in files)
{
fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ent = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file)))
{
DateTime = DateTime.Now,
Size = fs.Length
};
fs.Close();
crc.Reset();
crc.Update(buffer);
ent.Crc = crc.Value;
zipStream.PutNextEntry(ent);
zipStream.Write(buffer, 0, buffer.Length);
}
}
catch
{
result = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (ent != null)
{
ent = null;
}
GC.Collect();
GC.Collect(1);
}
folders = Directory.GetDirectories(folderToZip);
foreach (string folder in folders)
{
if (!ZipDirectory(folder, zipStream, folderToZip))
{
return false;
}
}
return result;
}
///
/// 压缩文件夹
///
/// 要压缩的文件夹路径
/// 压缩文件完整路径
/// 密码
/// 是否压缩成功
public static bool ZipDirectory(string folderToZip, string zipedFile, string password)
{
bool result = false;
if (!Directory.Exists(folderToZip))
{
return result;
}
ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile));
zipStream.SetLevel(6);
if (!string.IsNullOrEmpty(password))
{
zipStream.Password = password;
}
result = ZipDirectory(folderToZip, zipStream, "");
zipStream.Finish();
zipStream.Close();
return result;
}
///
/// 压缩文件夹
///
/// 要压缩的文件夹路径
/// 压缩文件完整路径
/// 是否压缩成功
public static bool ZipDirectory(string folderToZip, string zipedFile)
{
bool result = ZipDirectory(folderToZip, zipedFile, null);
return result;
}
///
/// 压缩文件
///
/// 要压缩的文件全名
/// 压缩后的文件名
/// 密码
/// 压缩结果
public static bool ZipFile(string fileToZip, string zipedFile, string password)
{
bool result = true;
ZipOutputStream zipStream = null;
FileStream fs = null;
ZipEntry ent = null;
if (!File.Exists(fileToZip))
{
return false;
}
try
{
fs = File.OpenRead(fileToZip);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
fs = File.Create(zipedFile);
zipStream = new ZipOutputStream(fs);
if (!string.IsNullOrEmpty(password))
{
zipStream.Password = password;
}
ent = new ZipEntry(Path.GetFileName(fileToZip));
zipStream.PutNextEntry(ent);
zipStream.SetLevel(6);
zipStream.Write(buffer, 0, buffer.Length);
}
catch
{
result = false;
}
finally
{
if (zipStream != null)
{
zipStream.Finish();
zipStream.Close();
}
if (ent != null)
{
ent = null;
}
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}
GC.Collect();
GC.Collect(1);
return result;
}
///
/// 压缩文件
///
/// 要压缩的文件全名
/// 压缩后的文件名
/// 压缩结果
public static bool ZipFile(string fileToZip, string zipedFile)
{
bool result = ZipFile(fileToZip, zipedFile, null);
return result;
}
///
/// 压缩文件或文件夹
///
/// 要压缩的路径
/// 压缩后的文件名
/// 密码
/// 压缩结果
public static bool Zip(string fileToZip, string zipedFile, string password)
{
bool result = false;
if (Directory.Exists(fileToZip))
{
result = ZipDirectory(fileToZip, zipedFile, password);
}
else if (File.Exists(fileToZip))
{
result = ZipFile(fileToZip, zipedFile, password);
}
return result;
}
///
/// 压缩文件或文件夹
///
/// 要压缩的路径
/// 压缩后的文件名
/// 压缩结果
public static bool Zip(string fileToZip, string zipedFile)
{
bool result = Zip(fileToZip, zipedFile, null);
return result;
}
#endregion
#region 解压
///
/// 解压功能(解压压缩文件到指定目录)
///
/// 待解压的文件
/// 指定解压目标目录
/// 密码
/// 解压结果
public static bool UnZip(string fileToUnZip, string zipedFolder, string password)
{
bool result = true;
FileStream fs = null;
ZipInputStream zipStream = null;
ZipEntry ent = null;
string fileName;
if (!File.Exists(fileToUnZip))
{
return false;
}
if (!Directory.Exists(zipedFolder))
{
Directory.CreateDirectory(zipedFolder);
}
try
{
zipStream = new ZipInputStream(File.OpenRead(fileToUnZip));
if (!string.IsNullOrEmpty(password))
{
zipStream.Password = password;
}
while ((ent = zipStream.GetNextEntry()) != null)
{
if (!string.IsNullOrEmpty(ent.Name))
{
fileName = Path.Combine(zipedFolder, ent.Name);
fileName = fileName.Replace('/', '\\');//change by Mr.HopeGi
if (fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
continue;
}
fs = File.Create(fileName);
int size = 2048;
byte[] data = new byte[size];
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
{
fs.Write(data, 0, data.Length);
}
else
{
break;
}
}
}
}
}
catch
{
result = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (zipStream != null)
{
zipStream.Close();
zipStream.Dispose();
}
if (ent != null)
{
ent = null;
}
GC.Collect();
GC.Collect(1);
}
return result;
}
///
/// 解压功能(解压压缩文件到指定目录)
///
/// 待解压的文件
/// 指定解压目标目录
/// 解压结果
public static bool UnZip(string fileToUnZip, string zipedFolder)
{
bool result = NewUnZip(fileToUnZip, zipedFolder, null, true);
return result;
}
///
/// ZIP:解压一个zip文件
///
/// 需要解压的Zip文件(绝对路径)
/// 解压到的目录
/// 解压密码
/// 是否覆盖已存在的文件
public static bool NewUnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
{
try
{
//如果解压到的目录不存在,则报错
if (!System.IO.Directory.Exists(TargetDirectory))
{
throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
}
//目录结尾
if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; }
using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
{
zipfiles.Password = Password;
ZipEntry theEntry;
while ((theEntry = zipfiles.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;
if (pathToZip != "")
{
directoryName = Path.GetDirectoryName(pathToZip) + "\\";
}
string fileName = Path.GetFileName(pathToZip);
Directory.CreateDirectory(TargetDirectory + directoryName);
if (fileName != "")
{
if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
{
try
{
using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = zipfiles.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
catch (Exception ex)
{
string message = ex.Message;
//解压出错//若文件无损坏则可能为无法覆盖文件
}
}
}
}
zipfiles.Close();
}
return true;
}
catch (Exception)
{
return false;
}
}
#endregion
#region 压缩文件
///
/// 压缩文件
///
/// 要打包的文件列表
/// 目标文件名
/// 压缩品质级别(0~9)
/// 是否删除原文件
public static void CompressFile(List fileNames, string GzipFileName, int CompressionLevel, bool deleteFile)
{
ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
try
{
s.SetLevel(CompressionLevel); //0 - store only to 9 - means best compression
foreach (FileInfo file in fileNames)
{
FileStream fs = null;
try
{
fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
}
catch
{ continue; }
// 方法二,将文件分批读入缓冲区
byte[] data = new byte[2048];
int size = 2048;
ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name))
{
DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime)
};
s.PutNextEntry(entry);
while (true)
{
size = fs.Read(data, 0, size);
if (size <= 0)
{
break;
}
s.Write(data, 0, size);
}
fs.Close();
if (deleteFile)
{
file.Delete();
}
}
}
finally
{
s.Finish();
s.Close();
}
}
public static void CompressFile(string fileName, string GzipFileName, int CompressionLevel, bool deleteFile)
{
ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
try
{
s.SetLevel(CompressionLevel); //0 - store only to 9 - means best compression
FileInfo file = new FileInfo(fileName);
FileStream fs = null;
try
{
fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
}
catch
{ return; }
// 方法二,将文件分批读入缓冲区
byte[] data = new byte[2048];
int size = 2048;
ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name))
{
DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime)
};
s.PutNextEntry(entry);
while (true)
{
size = fs.Read(data, 0, size);
if (size <= 0)
{
break;
}
s.Write(data, 0, size);
}
fs.Close();
if (deleteFile)
{
file.Delete();
}
}
finally
{
s.Finish();
s.Close();
}
}
///
/// 压缩文件夹
///
/// 要打包的文件夹
/// 目标文件名
/// 压缩品质级别(0~9)
/// 是否删除原文件夹
public static void CompressDirectory(string dirPath, string GzipFileName, int CompressionLevel, bool deleteDir)
{
//压缩文件为空时默认与压缩文件夹同一级目录
if (GzipFileName == string.Empty)
{
GzipFileName = dirPath.Substring(dirPath.LastIndexOf("//") + 1);
GzipFileName = dirPath.Substring(0, dirPath.LastIndexOf("//")) + "//" + GzipFileName + ".zip";
}
//if (Path.GetExtension(GzipFileName) != ".zip")
//{
// GzipFileName = GzipFileName + ".zip";
//}
using (ZipOutputStream zipoutputstream = new ZipOutputStream(File.Create(GzipFileName)))
{
zipoutputstream.SetLevel(CompressionLevel);
Crc32 crc = new Crc32();
Dictionary fileList = GetAllFies(dirPath);
foreach (KeyValuePair item in fileList)
{
FileStream fs = File.OpenRead(item.Key.ToString());
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(item.Key.Substring(dirPath.Length))
{
DateTime = item.Value,
Size = fs.Length
};
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipoutputstream.PutNextEntry(entry);
zipoutputstream.Write(buffer, 0, buffer.Length);
}
}
if (deleteDir)
{
Directory.Delete(dirPath, true);
}
}
///
/// 获取所有文件
///
///
private static Dictionary GetAllFies(string dir)
{
Dictionary FilesList = new Dictionary();
DirectoryInfo fileDire = new DirectoryInfo(dir);
if (!fileDire.Exists)
{
throw new System.IO.FileNotFoundException("目录:" + fileDire.FullName + "没有找到!");
}
GetAllDirFiles(fileDire, FilesList);
GetAllDirsFiles(fileDire.GetDirectories(), FilesList);
return FilesList;
}
///
/// 获取一个文件夹下的所有文件夹里的文件
///
///
///
private static void GetAllDirsFiles(DirectoryInfo[] dirs, Dictionary filesList)
{
foreach (DirectoryInfo dir in dirs)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName, file.LastWriteTime);
}
GetAllDirsFiles(dir.GetDirectories(), filesList);
}
}
///
/// 获取一个文件夹下的文件
///
/// 目录名称
/// 文件列表HastTable
private static void GetAllDirFiles(DirectoryInfo dir, Dictionary filesList)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName, file.LastWriteTime);
}
}
#endregion
#region 解压缩文件
///
/// 解压缩文件
///
/// 压缩包文件名
/// 解压缩目标路径
public static void Decompress(string GzipFile, string targetPath)
{
//string directoryName = Path.GetDirectoryName(targetPath + "//") + "//";
string directoryName = targetPath;
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);//生成解压目录
}
string CurrentDirectory = directoryName;
byte[] data = new byte[2048];
int size = 2048;
ZipEntry theEntry = null;
using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
{
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.IsDirectory)
{// 该结点是目录
if (!Directory.Exists(CurrentDirectory + theEntry.Name))
{
Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
}
}
else
{
if (theEntry.Name != string.Empty)
{
// 检查多级目录是否存在
if (theEntry.Name.Contains("\\"))
{
string parentDirPath = theEntry.Name.Remove(theEntry.Name.LastIndexOf("\\") + 1);
if (!Directory.Exists(parentDirPath))
{
Directory.CreateDirectory(CurrentDirectory + parentDirPath);
}
}
//解压文件到指定的目录
using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
{
while (true)
{
size = s.Read(data, 0, data.Length);
if (size <= 0)
{
break;
}
streamWriter.Write(data, 0, size);
}
streamWriter.Close();
}
}
}
}
s.Close();
}
}
#endregion
}
}