You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
8.5 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System;
namespace Estsh.Core.Util
{
public class ZipHelper
{
/// <summary>
/// 压缩文件和文件夹
/// </summary>
/// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
/// <param name="Password">压缩文件的密码</param>
/// <returns></returns>
public static bool Zip(String FileToZip, String ZipedFile, String Password)
{
if (Directory.Exists(FileToZip))
{
return ZipFileDictory(FileToZip, ZipedFile, Password);
}
else if (File.Exists(FileToZip))
{
return ZipFile(FileToZip, ZipedFile, Password);
}
else
{
return false;
}
}
/// <summary>
/// 压缩目录
/// </summary>
/// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后的文件名,全路径格式</param>
/// <param name="Password">压缩文件的密码</param>
/// <returns></returns>
private static bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password)
{
bool res = true;
if (!Directory.Exists(FolderToZip))
{
return false;
}
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
s.Password = Password;
s.SetLevel(6);
_CompressFolder(FolderToZip, s, FolderToZip);
s.Finish();
s.Close();
return res;
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="FileToZip">要进行压缩的文件名</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名</param>
/// <returns></returns>
private static bool ZipFile(string FileToZip, string ZipedFile, String Password)
{
bool res = true;
//如果文件没有找到,则报错
if (!File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
}
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
s.Password = Password;
s.SetLevel(6);
_AddFile(FileToZip, s, FileToZip);
s.Finish();
s.Close();
return res;
}
/// <summary>
/// 压缩单独文件
/// </summary>
/// <param name="FileToZip">要进行压缩的文件名</param>
/// <param name="zips">压缩后文件的输出流</param>
/// <param name="zipfolername">被压缩目录名称</param>
private static void _CreateZipFile(string FileToZip, ZipOutputStream zips, string zipfolername)
{
FileStream StreamToZip = null;
ZipEntry ZipEn = null;
try
{
StreamToZip = new FileStream(FileToZip, FileMode.Open, FileAccess.Read);
FileToZip = FileToZip.Substring(zipfolername.LastIndexOf("\\") + 1);
ZipEn = new ZipEntry(FileToZip);
zips.PutNextEntry(ZipEn);
byte[] buffer = new byte[16384];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
zips.Write(buffer, 0, size);
while (size < StreamToZip.Length)
{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
zips.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (StreamToZip != null)
{
StreamToZip.Close();
StreamToZip = null;
}
if (ZipEn != null)
{
ZipEn = null;
}
}
}
/// <summary>
/// 压缩某个子文件
/// </summary>
/// <param name="fileName"></param>
/// <param name="zips"></param>
/// <param name="zipfolername"></param>
private static void _AddFile(string fileName, ZipOutputStream zips, string zipfolername)
{
if (File.Exists(fileName))
{
_CreateZipFile(fileName, zips, zipfolername);
}
}
/// <summary>
/// 压缩某个子文件夹
/// </summary>
/// <param name="basePath"></param>
/// <param name="zips"></param>
/// <param name="zipfolername"></param>
private static void _CompressFolder(string basePath, ZipOutputStream zips, string zipfolername)
{
if (File.Exists(basePath))
{
_AddFile(basePath, zips, zipfolername);
return;
}
string[] names = Directory.GetFiles(basePath);
foreach (string fileName in names)
{
_AddFile(fileName, zips, zipfolername);
}
names = Directory.GetDirectories(basePath);
foreach (string folderName in names)
{
_CompressFolder(folderName, zips, zipfolername);
}
}
/// <summary>
/// 解压功能(解压压缩文件到指定目录)
/// </summary>
/// <param name="FileToUpZip">待解压的文件</param>
/// <param name="ZipedFolder">指定解压目标目录</param>
public static void UnZip(string FileToUpZip, string ZipedFolder, string Password)
{
byte[] buffer = new byte[2048];
while (ZipedFolder.LastIndexOf("\\") + 1 == ZipedFolder.Length)//检查路径是否以"\"结尾
{
ZipedFolder = ZipedFolder.Substring(0, ZipedFolder.Length - 1);//如果是则去掉末尾的"\"
}
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(FileToUpZip)))
{
//判断Password
if (Password != null && Password.Length > 0)
{
zipStream.Password = Password;
}
ZipEntry zipEntry = null;
while ((zipEntry = zipStream.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(zipEntry.Name);
string fileName = Path.GetFileName(zipEntry.Name);
if (!string.IsNullOrEmpty(directoryName) && !Directory.Exists(ZipedFolder + @"\" + directoryName))
{
Directory.CreateDirectory(ZipedFolder + @"\" + directoryName);
}
if (!string.IsNullOrEmpty(fileName))
{
if (zipEntry.CompressedSize == 0)
break;
if (zipEntry.IsDirectory)//如果压缩格式为文件夹方式压缩
{
directoryName = Path.GetDirectoryName(ZipedFolder + @"\" + zipEntry.Name);
Directory.CreateDirectory(directoryName);
}
else//2012-5-28修改支持单个文件压缩时自己创建目标文件夹
{
if (!Directory.Exists(ZipedFolder))
{
Directory.CreateDirectory(ZipedFolder);
}
}
using (FileStream stream = File.Create(ZipedFolder + @"\" + zipEntry.Name))
{
while (true)
{
int size = zipStream.Read(buffer, 0, buffer.Length);
if (size > 0)
{
stream.Write(buffer, 0, size);
}
else
{
break;
}
}
}
}
}
}
}
}
}