using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace Campton.Tools
{
public class ZipManager
{
构造函数#region 构造函数
public ZipManager() { }
#endregion
私有静态变量#region 私有静态变量
private static ZipManager _instance;
#endregion
静态方法#region 静态方法
/**//// <summary>
/// 创建实例方法
/// </summary>
/// <returns>返回ZipManager对象实例</returns>
public static ZipManager Instance()
{
if (_instance == null)
_instance = new ZipManager();
return _instance;
}
#endregion
公有方法#region 公有方法
/**//// <summary>
/// 解压文件
/// </summary>
/// <param name="zipFile">zip文件(完整的绝对路径)</param>
/// <param name="destFolder">目标目录(绝对路径)</param>
/// <returns>是否解压成功(true 为成功 false 为失败)</returns>
public bool UnCompress(string zipFile, string destFolder)
{
if (!File.Exists(zipFile))
return false;
string exts = Path.GetExtension(zipFile);
if (string.Compare(exts, ".zip", true) == 0)
{
return UnZip(zipFile, destFolder);
}
else if (string.Compare(exts, ".rar", true) == 0)
{
return UnRar(zipFile, destFolder);
}
else
{
return false;
}
}
#endregion
私有方法#region 私有方法
/**//// <summary>
/// 对Zip文件进行解压
/// </summary>
/// <param name="zipFile">zip文件(完整的绝对路径)</param>
/// <param name="destFolder">目标目录(绝对路径)</param>
/// <returns>是否解压成功(true 为成功 false 为失败)</returns>
private bool UnZip(string zipFile, string destFolder)
{
if (!File.Exists(zipFile))
return false;
if (!Directory.Exists(destFolder))
Directory.CreateDirectory(destFolder);
FileInfo fi = new FileInfo(zipFile);
try
{
using (ZipInputStream zis = new ZipInputStream(fi.OpenRead()))
{
ZipEntry ze;
while ((ze = zis.GetNextEntry()) != null)
{
string path = Path.Combine(destFolder, ze.Name);
if (ze.IsDirectory)
{
Directory.CreateDirectory(path);
}
else
{
using (Stream outStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
{
int size = 2048;
byte[] data = new byte[size];
while (size > 0)
{
size = zis.Read(data, 0, data.Length);
outStream.Write(data, 0, size);
}
outStream.Flush();
}
}
}
}
}
catch (Exception)
{
return false;
}
return true;
}
/**//// <summary>
/// 对RAR所支持的压缩文件进行解压
/// </summary>
/// <param name="zipFile">压缩文件(完整的绝对路径)</param>
/// <param name="destFolder">目标目录(绝对路径)</param>
/// <returns>是否解压成功(true 为成功 false 为失败)</returns>
private bool UnRar(string zipFile, string destFolder)
{
if (!File.Exists(zipFile))
return false;
if (!Directory.Exists(destFolder))
Directory.CreateDirectory(destFolder);
Unrar unRar = new Unrar();
try
{
unRar.DestinationPath = destFolder;
unRar.Open(zipFile, Unrar.OpenMode.Extract);
while (unRar.ReadHeader())
{
unRar.Extract();
}
return true;
}
catch (Exception)
{
return false;
}
finally
{
if (unRar != null)
{
unRar.Close();
unRar.Dispose();
}
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace Campton.Tools
{
public class ZipManager
{
构造函数#region 构造函数
public ZipManager() { }
#endregion
私有静态变量#region 私有静态变量
private static ZipManager _instance;
#endregion
静态方法#region 静态方法
/**//// <summary>
/// 创建实例方法
/// </summary>
/// <returns>返回ZipManager对象实例</returns>
public static ZipManager Instance()
{
if (_instance == null)
_instance = new ZipManager();
return _instance;
}
#endregion
公有方法#region 公有方法
/**//// <summary>
/// 解压文件
/// </summary>
/// <param name="zipFile">zip文件(完整的绝对路径)</param>
/// <param name="destFolder">目标目录(绝对路径)</param>
/// <returns>是否解压成功(true 为成功 false 为失败)</returns>
public bool UnCompress(string zipFile, string destFolder)
{
if (!File.Exists(zipFile))
return false;
string exts = Path.GetExtension(zipFile);
if (string.Compare(exts, ".zip", true) == 0)
{
return UnZip(zipFile, destFolder);
}
else if (string.Compare(exts, ".rar", true) == 0)
{
return UnRar(zipFile, destFolder);
}
else
{
return false;
}
}
#endregion
私有方法#region 私有方法
/**//// <summary>
/// 对Zip文件进行解压
/// </summary>
/// <param name="zipFile">zip文件(完整的绝对路径)</param>
/// <param name="destFolder">目标目录(绝对路径)</param>
/// <returns>是否解压成功(true 为成功 false 为失败)</returns>
private bool UnZip(string zipFile, string destFolder)
{
if (!File.Exists(zipFile))
return false;
if (!Directory.Exists(destFolder))
Directory.CreateDirectory(destFolder);
FileInfo fi = new FileInfo(zipFile);
try
{
using (ZipInputStream zis = new ZipInputStream(fi.OpenRead()))
{
ZipEntry ze;
while ((ze = zis.GetNextEntry()) != null)
{
string path = Path.Combine(destFolder, ze.Name);
if (ze.IsDirectory)
{
Directory.CreateDirectory(path);
}
else
{
using (Stream outStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
{
int size = 2048;
byte[] data = new byte[size];
while (size > 0)
{
size = zis.Read(data, 0, data.Length);
outStream.Write(data, 0, size);
}
outStream.Flush();
}
}
}
}
}
catch (Exception)
{
return false;
}
return true;
}
/**//// <summary>
/// 对RAR所支持的压缩文件进行解压
/// </summary>
/// <param name="zipFile">压缩文件(完整的绝对路径)</param>
/// <param name="destFolder">目标目录(绝对路径)</param>
/// <returns>是否解压成功(true 为成功 false 为失败)</returns>
private bool UnRar(string zipFile, string destFolder)
{
if (!File.Exists(zipFile))
return false;
if (!Directory.Exists(destFolder))
Directory.CreateDirectory(destFolder);
Unrar unRar = new Unrar();
try
{
unRar.DestinationPath = destFolder;
unRar.Open(zipFile, Unrar.OpenMode.Extract);
while (unRar.ReadHeader())
{
unRar.Extract();
}
return true;
}
catch (Exception)
{
return false;
}
finally
{
if (unRar != null)
{
unRar.Close();
unRar.Dispose();
}
}
}
#endregion
}
}