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