ZipHelper.cs
using
System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.DirectoryServices;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;
namespace BackupTool
... {
/**//// <summary>
/// Zip助手类,提供对ZIP格式文件的压缩和解压缩服务。
/// </summary>
/// <remarks>本类为静态类,所有方法皆是静态方法。</remarks>
public class ZipHelper
...{
事件处理 提示用户有相同文件时是否覆盖#region 事件处理 提示用户有相同文件时是否覆盖
//定义委托
public delegate void ZipHelperEventHandler(ZipHelpEventArgs e);
//定义事件
public static event ZipHelperEventHandler ZipHelperProcessOverWrite;
//事件调用
public static void OnZipHelperProcessOverWrite(ZipHelpEventArgs e)
...{
if (ZipHelperProcessOverWrite != null)
ZipHelperProcessOverWrite(e);
}
//事件参数类
public class ZipHelpEventArgs : EventArgs
...{
public string FileName; //文件名
public bool IsOverWrite; //是否覆盖
public ZipHelpEventArgs()
...{
}
public ZipHelpEventArgs(string fileName, bool isOverWrite)
...{
this.FileName = fileName;
this.IsOverWrite = isOverWrite;
}
}
#endregion
枚举#region 枚举
/**//// <summary>
/// 用户替换文件操作类型
/// </summary>
public enum ZipOverWrite
...{
/**//// <summary>
/// 提示用户是否替换
/// </summary>
Prompt,
/**//// <summary>
/// 重不替换
/// </summary>
Never,
/**//// <summary>
/// 总是替换
/// </summary>
Always
}
#endregion
构造函数#region 构造函数
/**//// <summary>
/// 构造函数
/// </summary>
public ZipHelper()
...{
}
#endregion
方法#region 方法
/**//// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileList">文件路径数组</param>
/// <param name="targetFileName">压缩后的文件路径</param>
public static bool Compress(string[] fileList, string targetFileName)
...{
bool rv = false;
//如果已经存在目标文件,询问用户是否覆盖
if (File.Exists(targetFileName))
...{
if (!_ProcessOverwrite(targetFileName))
return false;
}
ZipOutputStream zipOut = new ZipOutputStream(File.Create(targetFileName));
try
...{
foreach (string fName in fileList)
...{
FileInfo fi = new FileInfo(fName);
ZipEntry entry = new ZipEntry(fi.Name);
FileStream sReader = File.OpenRead(fName);
byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
sReader.Read(buff, 0, (int)sReader.Length);
entry.Size = sReader.Length;
sReader.Close();
entry.DateTime = fi.LastWriteTime;
zipOut.PutNextEntry(entry);
zipOut.Write(buff, 0, buff.Length);
}
if (File.Exists(targetFileName))
rv = true;
else
rv = false;
}
catch (Exception ex)
...{
rv = false;
throw ex;
}
finally
...{
zipOut.Finish();
zipOut.Close();
}
return rv;
}
/**//// <summary>
/// 压缩目录
/// </summary>
/// <param name="dir">目录路径</param>
/// <param name="targetFileName">压缩后的文件路径</param>
public static bool Compress(string dir, string targetFileName)
...{
bool rv = false;
//如果已经存在目标文件,询问用户是否覆盖
if (File.Exists(targetFileName))
...{
if (!_ProcessOverwrite(targetFileName))
return false;
}
ZipOutputStream zipOut = new ZipOutputStream(File.Create(targetFileName));
try
...{
foreach (string fName in Directory.GetFiles(dir))
...{
FileInfo fi = new FileInfo(fName);
ZipEntry entry = new ZipEntry(fi.Name);
FileStream sReader = File.OpenRead(fName);
byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
sReader.Read(buff, 0, (int)sReader.Length);
entry.Size = sReader.Length;
sReader.Close();
entry.DateTime = fi.LastWriteTime;
zipOut.PutNextEntry(entry);
zipOut.Write(buff, 0, buff.Length);
}
if (File.Exists(targetFileName))
rv = true;
else
rv = false;
}
catch (Exception ex)
...{
rv = false;
throw new System.NotImplementedException(ex.Message);
}
finally
...{
zipOut.Finish();
zipOut.Close();
}
return rv;
}
压缩文件夹,支持递归#region 压缩文件夹,支持递归
/**//// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="dir">待压缩的文件夹</param>
/// <param name="targetFileName">压缩后文件路径(包括文件名)</param>
/// <param name="recursive">是否递归压缩</param>
/// <returns></returns>
public static bool Compress(string dir, string targetFileName,bool recursive)
...{
//如果已经存在目标文件,询问用户是否覆盖
if (File.Exists(targetFileName))
...{
if (!_ProcessOverwrite(targetFileName))
return false;
}
if (recursive == false)
return Compress(dir, targetFileName);
FileStream ZipFile;
ZipOutputStream ZipStream;
//open
ZipFile = File.Create(targetFileName);
ZipStream = new ZipOutputStream(ZipFile);
if (dir != String.Empty)
...{
_CompressFolder(dir, ZipStream, dir);
}
//close
ZipStream.Finish();
ZipStream.Close();
if (File.Exists(targetFileName))
return true;
else
return false;
}
/**//// <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="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="FileToZip"></param>
/// <param name="zips"></param>
/// <param name="zipfolername"></param>
private static void _CreateZipFile(string FileToZip, ZipOutputStream zips,string zipfolername)
...{
try
...{
FileStream StreamToZip = new FileStream(FileToZip, FileMode.Open, FileAccess.Read);
string temp = FileToZip;
string temp1 = zipfolername;
if (temp1.Length > 0)
...{
int i = temp1.LastIndexOf('/') + 1;
int j = temp.Length - i;
temp = temp.Substring(i, j);
}
ZipEntry ZipEn = new ZipEntry(temp);
zips.PutNextEntry(ZipEn);
byte[] buffer = new byte[16384];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
zips.Write(buffer, 0, size);
try
...{
while (size < StreamToZip.Length)
...{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
zips.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
...{
throw ex;
}
StreamToZip.Close();
}
catch (Exception e)
...{
throw e;
}
}
#endregion
/**//// <summary>
/// 解压缩文件
/// </summary>
/// <remarks>支持递归解压缩(限1层递归),即压缩后文件中若出现zip文件,则继续解压缩。</remarks>
/// <param name="fileName">要解压缩的文件</param>
/// <param name="targetDir">解压缩目录</param>
/// <param name="allowRecursive">是否允许递归解压缩</param>
public static bool Decompress(string fileName, string targetDir, bool allowRecursive)
...{
bool rv = true;
FastZip fz = new FastZip();
fz.RestoreDateTimeOnExtract = true; //保留原文件最后修改时间
try
...{
fz.ExtractZip(fileName, targetDir, "");
}
catch (Exception ex)
...{
rv = false;
throw new System.NotImplementedException(ex.Message);
}
if (rv == false)
return rv;
DirectoryInfo dr = new DirectoryInfo(targetDir);
FileInfo[] fis = dr.GetFiles();
string tempex;
string tempdir;
string tempname;
try
...{
if (allowRecursive)
...{
foreach (FileInfo fi in fis)
...{
tempex = fi.Extension;
if (tempex == ".zip") //解压内层zip文件,放入以现有文件名称命名的文件夹中
...{
tempname = fi.Name.Substring(0, fi.Name.Length - tempex.Length);
tempdir = Path.Combine(fi.DirectoryName, tempname);
if (Directory.Exists(tempdir)) //如果在解压内层的zip文件时,遇到已经存在的文件夹,将以文件名[1]命名文件夹
...{
tempdir = Path.Combine(fi.DirectoryName, tempname + "[1]");
}
fz.ExtractZip(fi.FullName, tempdir, "");
}
}
}
}
catch (Exception ex)
...{
rv = false;
throw ex;
}
return rv;
}
/**//// <summary>
/// 解压缩文件
/// </summary>
/// <remarks>支持递归解压缩(限1层递归),即压缩后文件中若出现zip文件,则继续解压缩。</remarks>
/// <param name="fileName">要解压缩的文件</param>
/// <param name="targetDir">解压缩目录</param>
/// <param name="allowRecursive">是否允许递归解压缩</param>
/// <param name="overWrite">解压缩时,遇到已经存在的文件是否提示用户替换 ZipOverWrite 枚举类型</param>
public static bool Decompress(string fileName, string targetDir, bool allowRecursive, ZipOverWrite overWrite)
...{
bool rv = true;
FastZip fz = new FastZip();
//根据用户设置 遇到重文件时的操作,设置FastZip的OverWrite
int oi = (int)overWrite;
FastZip.Overwrite ov = FastZip.Overwrite.Prompt;
switch (oi)
...{
case 0:
ov = FastZip.Overwrite.Prompt;
break;
case 1:
ov = FastZip.Overwrite.Never;
break;
case 2:
ov = FastZip.Overwrite.Always;
break;
}
try
...{
fz.ExtractZip(fileName, targetDir, ov, _ProcessOverwrite, "", "", true);
}
catch (Exception ex)
...{
rv = false;
throw new System.NotImplementedException(ex.Message);
}
if (rv == false)
return rv;
DirectoryInfo dr = new DirectoryInfo(targetDir);
FileInfo[] fis = dr.GetFiles();
string tempex;
string tempdir;
string tempname;
try
...{
if (allowRecursive) //允许递归
...{
foreach (FileInfo fi in fis)
...{
tempex = fi.Extension;
if (tempex.ToLower() == ".zip") //解压内层zip文件,放入以现有文件名称命名的文件夹中
...{
tempname = fi.Name.Substring(0, fi.Name.Length - tempex.Length);
tempdir = Path.Combine(fi.DirectoryName, tempname);
if (Directory.Exists(tempdir)) //如果在解压内层的zip文件时,遇到已经存在的文件夹,将以文件名[1]命名文件夹
...{
tempdir = Path.Combine(fi.DirectoryName, tempname + "[1]");
}
fz.ExtractZip(fi.FullName, tempdir, ov, _ProcessOverwrite, "", "", true);
}
}
}
}
catch (Exception ex)
...{
rv = false;
throw ex;
}
return rv;
}
/**//// <summary>
/// 处理在解压缩时,如果在目标目录下已经存在相同的文件,提示用户是否替换已存在文件
/// </summary>
/// <param name="filename">解压后文件</param>
/// <returns>是否替换已存在的文件,是将替换,否不替换</returns>
private static bool _ProcessOverwrite(string filename)
...{
//updated by shen wei hua 20071217 遇到相同的包,将始终覆盖
bool isOverWrite = true;
// ZipHelpEventArgs e = new ZipHelpEventArgs(filename, isOverWrite);
// OnZipHelperProcessOverWrite(e);
// isOverWrite = e.IsOverWrite;
return isOverWrite;
}
/**//// <summary>
/// 获取文件压缩后大小
/// </summary>
/// <param name="filename">要压缩的文件路径</param>
/// <param name="tempDir">临时目录</param>
/// <returns>文件大小(以字节为单位)</returns>
public static long GetFileCompressedSize(string fileName, string tempDir)
...{
long rv = 0;
//以当前时间点为文件名,以防止文件冲突
string targetFile = Path.Combine(tempDir, _GetCurrDatetime() + ".zip");
//创建临时压缩文件
ZipOutputStream zipOut = new ZipOutputStream(File.Create(targetFile));
try
...{
FileInfo fi = new FileInfo(fileName);
ZipEntry entry = new ZipEntry(fi.Name);
FileStream sReader = File.OpenRead(fileName);
byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
sReader.Read(buff, 0, (int)sReader.Length);
entry.Size = sReader.Length;
sReader.Close();
entry.DateTime = fi.LastWriteTime;
zipOut.PutNextEntry(entry);
zipOut.Write(buff, 0, buff.Length);
zipOut.Finish();
zipOut.Close();
if (File.Exists(targetFile))
...{
FileInfo fi2 = new FileInfo(targetFile);
rv = fi2.Length;
}
}
catch (Exception ex)
...{
throw ex;
}
finally
...{
//删除临时文件
if (File.Exists(targetFile))
...{
File.Delete(targetFile);
}
}
return rv;
}
/**//// <summary>
/// 获取压缩文件里的文件列表
/// </summary>
/// <remarks>忽略压缩文件中的目录,只返回文件。</remarks>
/// <param name="filename">压缩文件路径</param>
/// <param name="tempDir">临时目录</param>
/// <returns>文件名数组</returns>
public static string[] GetFileList(string fileName, string tempDir)
...{
FastZip fz = new FastZip();
fz.RestoreDateTimeOnExtract = true;
string[] rv;
//在当前程序路径/Temp 下创建临时目录
string tempdir = Path.Combine(tempDir, "tempzip");
try
...{
fz.ExtractZip(fileName, tempdir, "");
rv = Directory.GetFiles(tempdir);
//删除临时文件
Directory.Delete(tempdir, true);
}
catch (Exception ex)
...{
rv = null;
throw ex;
}
return rv;
}
/**//// <summary>
/// 抽取压缩文件里的某个文件
/// </summary>
/// <param name="ZipFileName">压缩文件路径</param>
/// <param name="zipFileName">压缩文件路径</param>
/// <param name="extraFileName">要抽取的文件名</param>
/// <param name="targetDir">抽取文件保存目录</param>
/// <param name="tempDir">临时目录</param>
public static bool GetFile(string zipFileName, string extraFileName, string targetDir, string tempDir)
...{
bool rv = false;
FastZip fz = new FastZip();
fz.RestoreDateTimeOnExtract = true;
string targetFileName = "";
string[] tempfiles;
FileInfo fi;
//在当前程序路径/temp 下 创建临时目录
string tempdir = Path.Combine(tempDir, "tempzip");
try
...{
fz.ExtractZip(zipFileName, tempdir, "");
tempfiles = Directory.GetFiles(tempdir);
foreach (string fname in tempfiles)
...{
fi = new FileInfo(fname);
if (fi.Name.Equals(extraFileName))
...{
targetFileName = Path.Combine(targetDir, fi.Name);
File.Move(fi.FullName, targetFileName);
break;
}
}
//删除临时目录
Directory.Delete(tempdir, true);
//检查目标文件是否已经创建
if (File.Exists(targetFileName))
rv = true;
else
rv = false;
}
catch (Exception ex)
...{
rv = false;
throw ex;
}
return rv;
}
/**//// <summary>
/// 获取当前时间点,精确到毫秒
/// </summary>
/// <returns>时间点字符串</returns>
private static string _GetCurrDatetime()
...{
DateTime dt = System.DateTime.Now;
string rv = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString() + dt.Millisecond.ToString();
return rv;
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.DirectoryServices;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;
namespace BackupTool
... {
/**//// <summary>
/// Zip助手类,提供对ZIP格式文件的压缩和解压缩服务。
/// </summary>
/// <remarks>本类为静态类,所有方法皆是静态方法。</remarks>
public class ZipHelper
...{
事件处理 提示用户有相同文件时是否覆盖#region 事件处理 提示用户有相同文件时是否覆盖
//定义委托
public delegate void ZipHelperEventHandler(ZipHelpEventArgs e);
//定义事件
public static event ZipHelperEventHandler ZipHelperProcessOverWrite;
//事件调用
public static void OnZipHelperProcessOverWrite(ZipHelpEventArgs e)
...{
if (ZipHelperProcessOverWrite != null)
ZipHelperProcessOverWrite(e);
}
//事件参数类
public class ZipHelpEventArgs : EventArgs
...{
public string FileName; //文件名
public bool IsOverWrite; //是否覆盖
public ZipHelpEventArgs()
...{
}
public ZipHelpEventArgs(string fileName, bool isOverWrite)
...{
this.FileName = fileName;
this.IsOverWrite = isOverWrite;
}
}
#endregion
枚举#region 枚举
/**//// <summary>
/// 用户替换文件操作类型
/// </summary>
public enum ZipOverWrite
...{
/**//// <summary>
/// 提示用户是否替换
/// </summary>
Prompt,
/**//// <summary>
/// 重不替换
/// </summary>
Never,
/**//// <summary>
/// 总是替换
/// </summary>
Always
}
#endregion
构造函数#region 构造函数
/**//// <summary>
/// 构造函数
/// </summary>
public ZipHelper()
...{
}
#endregion
方法#region 方法
/**//// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileList">文件路径数组</param>
/// <param name="targetFileName">压缩后的文件路径</param>
public static bool Compress(string[] fileList, string targetFileName)
...{
bool rv = false;
//如果已经存在目标文件,询问用户是否覆盖
if (File.Exists(targetFileName))
...{
if (!_ProcessOverwrite(targetFileName))
return false;
}
ZipOutputStream zipOut = new ZipOutputStream(File.Create(targetFileName));
try
...{
foreach (string fName in fileList)
...{
FileInfo fi = new FileInfo(fName);
ZipEntry entry = new ZipEntry(fi.Name);
FileStream sReader = File.OpenRead(fName);
byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
sReader.Read(buff, 0, (int)sReader.Length);
entry.Size = sReader.Length;
sReader.Close();
entry.DateTime = fi.LastWriteTime;
zipOut.PutNextEntry(entry);
zipOut.Write(buff, 0, buff.Length);
}
if (File.Exists(targetFileName))
rv = true;
else
rv = false;
}
catch (Exception ex)
...{
rv = false;
throw ex;
}
finally
...{
zipOut.Finish();
zipOut.Close();
}
return rv;
}
/**//// <summary>
/// 压缩目录
/// </summary>
/// <param name="dir">目录路径</param>
/// <param name="targetFileName">压缩后的文件路径</param>
public static bool Compress(string dir, string targetFileName)
...{
bool rv = false;
//如果已经存在目标文件,询问用户是否覆盖
if (File.Exists(targetFileName))
...{
if (!_ProcessOverwrite(targetFileName))
return false;
}
ZipOutputStream zipOut = new ZipOutputStream(File.Create(targetFileName));
try
...{
foreach (string fName in Directory.GetFiles(dir))
...{
FileInfo fi = new FileInfo(fName);
ZipEntry entry = new ZipEntry(fi.Name);
FileStream sReader = File.OpenRead(fName);
byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
sReader.Read(buff, 0, (int)sReader.Length);
entry.Size = sReader.Length;
sReader.Close();
entry.DateTime = fi.LastWriteTime;
zipOut.PutNextEntry(entry);
zipOut.Write(buff, 0, buff.Length);
}
if (File.Exists(targetFileName))
rv = true;
else
rv = false;
}
catch (Exception ex)
...{
rv = false;
throw new System.NotImplementedException(ex.Message);
}
finally
...{
zipOut.Finish();
zipOut.Close();
}
return rv;
}
压缩文件夹,支持递归#region 压缩文件夹,支持递归
/**//// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="dir">待压缩的文件夹</param>
/// <param name="targetFileName">压缩后文件路径(包括文件名)</param>
/// <param name="recursive">是否递归压缩</param>
/// <returns></returns>
public static bool Compress(string dir, string targetFileName,bool recursive)
...{
//如果已经存在目标文件,询问用户是否覆盖
if (File.Exists(targetFileName))
...{
if (!_ProcessOverwrite(targetFileName))
return false;
}
if (recursive == false)
return Compress(dir, targetFileName);
FileStream ZipFile;
ZipOutputStream ZipStream;
//open
ZipFile = File.Create(targetFileName);
ZipStream = new ZipOutputStream(ZipFile);
if (dir != String.Empty)
...{
_CompressFolder(dir, ZipStream, dir);
}
//close
ZipStream.Finish();
ZipStream.Close();
if (File.Exists(targetFileName))
return true;
else
return false;
}
/**//// <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="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="FileToZip"></param>
/// <param name="zips"></param>
/// <param name="zipfolername"></param>
private static void _CreateZipFile(string FileToZip, ZipOutputStream zips,string zipfolername)
...{
try
...{
FileStream StreamToZip = new FileStream(FileToZip, FileMode.Open, FileAccess.Read);
string temp = FileToZip;
string temp1 = zipfolername;
if (temp1.Length > 0)
...{
int i = temp1.LastIndexOf('/') + 1;
int j = temp.Length - i;
temp = temp.Substring(i, j);
}
ZipEntry ZipEn = new ZipEntry(temp);
zips.PutNextEntry(ZipEn);
byte[] buffer = new byte[16384];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
zips.Write(buffer, 0, size);
try
...{
while (size < StreamToZip.Length)
...{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
zips.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
...{
throw ex;
}
StreamToZip.Close();
}
catch (Exception e)
...{
throw e;
}
}
#endregion
/**//// <summary>
/// 解压缩文件
/// </summary>
/// <remarks>支持递归解压缩(限1层递归),即压缩后文件中若出现zip文件,则继续解压缩。</remarks>
/// <param name="fileName">要解压缩的文件</param>
/// <param name="targetDir">解压缩目录</param>
/// <param name="allowRecursive">是否允许递归解压缩</param>
public static bool Decompress(string fileName, string targetDir, bool allowRecursive)
...{
bool rv = true;
FastZip fz = new FastZip();
fz.RestoreDateTimeOnExtract = true; //保留原文件最后修改时间
try
...{
fz.ExtractZip(fileName, targetDir, "");
}
catch (Exception ex)
...{
rv = false;
throw new System.NotImplementedException(ex.Message);
}
if (rv == false)
return rv;
DirectoryInfo dr = new DirectoryInfo(targetDir);
FileInfo[] fis = dr.GetFiles();
string tempex;
string tempdir;
string tempname;
try
...{
if (allowRecursive)
...{
foreach (FileInfo fi in fis)
...{
tempex = fi.Extension;
if (tempex == ".zip") //解压内层zip文件,放入以现有文件名称命名的文件夹中
...{
tempname = fi.Name.Substring(0, fi.Name.Length - tempex.Length);
tempdir = Path.Combine(fi.DirectoryName, tempname);
if (Directory.Exists(tempdir)) //如果在解压内层的zip文件时,遇到已经存在的文件夹,将以文件名[1]命名文件夹
...{
tempdir = Path.Combine(fi.DirectoryName, tempname + "[1]");
}
fz.ExtractZip(fi.FullName, tempdir, "");
}
}
}
}
catch (Exception ex)
...{
rv = false;
throw ex;
}
return rv;
}
/**//// <summary>
/// 解压缩文件
/// </summary>
/// <remarks>支持递归解压缩(限1层递归),即压缩后文件中若出现zip文件,则继续解压缩。</remarks>
/// <param name="fileName">要解压缩的文件</param>
/// <param name="targetDir">解压缩目录</param>
/// <param name="allowRecursive">是否允许递归解压缩</param>
/// <param name="overWrite">解压缩时,遇到已经存在的文件是否提示用户替换 ZipOverWrite 枚举类型</param>
public static bool Decompress(string fileName, string targetDir, bool allowRecursive, ZipOverWrite overWrite)
...{
bool rv = true;
FastZip fz = new FastZip();
//根据用户设置 遇到重文件时的操作,设置FastZip的OverWrite
int oi = (int)overWrite;
FastZip.Overwrite ov = FastZip.Overwrite.Prompt;
switch (oi)
...{
case 0:
ov = FastZip.Overwrite.Prompt;
break;
case 1:
ov = FastZip.Overwrite.Never;
break;
case 2:
ov = FastZip.Overwrite.Always;
break;
}
try
...{
fz.ExtractZip(fileName, targetDir, ov, _ProcessOverwrite, "", "", true);
}
catch (Exception ex)
...{
rv = false;
throw new System.NotImplementedException(ex.Message);
}
if (rv == false)
return rv;
DirectoryInfo dr = new DirectoryInfo(targetDir);
FileInfo[] fis = dr.GetFiles();
string tempex;
string tempdir;
string tempname;
try
...{
if (allowRecursive) //允许递归
...{
foreach (FileInfo fi in fis)
...{
tempex = fi.Extension;
if (tempex.ToLower() == ".zip") //解压内层zip文件,放入以现有文件名称命名的文件夹中
...{
tempname = fi.Name.Substring(0, fi.Name.Length - tempex.Length);
tempdir = Path.Combine(fi.DirectoryName, tempname);
if (Directory.Exists(tempdir)) //如果在解压内层的zip文件时,遇到已经存在的文件夹,将以文件名[1]命名文件夹
...{
tempdir = Path.Combine(fi.DirectoryName, tempname + "[1]");
}
fz.ExtractZip(fi.FullName, tempdir, ov, _ProcessOverwrite, "", "", true);
}
}
}
}
catch (Exception ex)
...{
rv = false;
throw ex;
}
return rv;
}
/**//// <summary>
/// 处理在解压缩时,如果在目标目录下已经存在相同的文件,提示用户是否替换已存在文件
/// </summary>
/// <param name="filename">解压后文件</param>
/// <returns>是否替换已存在的文件,是将替换,否不替换</returns>
private static bool _ProcessOverwrite(string filename)
...{
//updated by shen wei hua 20071217 遇到相同的包,将始终覆盖
bool isOverWrite = true;
// ZipHelpEventArgs e = new ZipHelpEventArgs(filename, isOverWrite);
// OnZipHelperProcessOverWrite(e);
// isOverWrite = e.IsOverWrite;
return isOverWrite;
}
/**//// <summary>
/// 获取文件压缩后大小
/// </summary>
/// <param name="filename">要压缩的文件路径</param>
/// <param name="tempDir">临时目录</param>
/// <returns>文件大小(以字节为单位)</returns>
public static long GetFileCompressedSize(string fileName, string tempDir)
...{
long rv = 0;
//以当前时间点为文件名,以防止文件冲突
string targetFile = Path.Combine(tempDir, _GetCurrDatetime() + ".zip");
//创建临时压缩文件
ZipOutputStream zipOut = new ZipOutputStream(File.Create(targetFile));
try
...{
FileInfo fi = new FileInfo(fileName);
ZipEntry entry = new ZipEntry(fi.Name);
FileStream sReader = File.OpenRead(fileName);
byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
sReader.Read(buff, 0, (int)sReader.Length);
entry.Size = sReader.Length;
sReader.Close();
entry.DateTime = fi.LastWriteTime;
zipOut.PutNextEntry(entry);
zipOut.Write(buff, 0, buff.Length);
zipOut.Finish();
zipOut.Close();
if (File.Exists(targetFile))
...{
FileInfo fi2 = new FileInfo(targetFile);
rv = fi2.Length;
}
}
catch (Exception ex)
...{
throw ex;
}
finally
...{
//删除临时文件
if (File.Exists(targetFile))
...{
File.Delete(targetFile);
}
}
return rv;
}
/**//// <summary>
/// 获取压缩文件里的文件列表
/// </summary>
/// <remarks>忽略压缩文件中的目录,只返回文件。</remarks>
/// <param name="filename">压缩文件路径</param>
/// <param name="tempDir">临时目录</param>
/// <returns>文件名数组</returns>
public static string[] GetFileList(string fileName, string tempDir)
...{
FastZip fz = new FastZip();
fz.RestoreDateTimeOnExtract = true;
string[] rv;
//在当前程序路径/Temp 下创建临时目录
string tempdir = Path.Combine(tempDir, "tempzip");
try
...{
fz.ExtractZip(fileName, tempdir, "");
rv = Directory.GetFiles(tempdir);
//删除临时文件
Directory.Delete(tempdir, true);
}
catch (Exception ex)
...{
rv = null;
throw ex;
}
return rv;
}
/**//// <summary>
/// 抽取压缩文件里的某个文件
/// </summary>
/// <param name="ZipFileName">压缩文件路径</param>
/// <param name="zipFileName">压缩文件路径</param>
/// <param name="extraFileName">要抽取的文件名</param>
/// <param name="targetDir">抽取文件保存目录</param>
/// <param name="tempDir">临时目录</param>
public static bool GetFile(string zipFileName, string extraFileName, string targetDir, string tempDir)
...{
bool rv = false;
FastZip fz = new FastZip();
fz.RestoreDateTimeOnExtract = true;
string targetFileName = "";
string[] tempfiles;
FileInfo fi;
//在当前程序路径/temp 下 创建临时目录
string tempdir = Path.Combine(tempDir, "tempzip");
try
...{
fz.ExtractZip(zipFileName, tempdir, "");
tempfiles = Directory.GetFiles(tempdir);
foreach (string fname in tempfiles)
...{
fi = new FileInfo(fname);
if (fi.Name.Equals(extraFileName))
...{
targetFileName = Path.Combine(targetDir, fi.Name);
File.Move(fi.FullName, targetFileName);
break;
}
}
//删除临时目录
Directory.Delete(tempdir, true);
//检查目标文件是否已经创建
if (File.Exists(targetFileName))
rv = true;
else
rv = false;
}
catch (Exception ex)
...{
rv = false;
throw ex;
}
return rv;
}
/**//// <summary>
/// 获取当前时间点,精确到毫秒
/// </summary>
/// <returns>时间点字符串</returns>
private static string _GetCurrDatetime()
...{
DateTime dt = System.DateTime.Now;
string rv = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString() + dt.Millisecond.ToString();
return rv;
}
#endregion
}
}