/// <summary>
/// 压缩目录
/// </summary>
/// <param name="dir">目录路径</param>
/// <param name="targetFileName">压缩后的文件路径</param>
public bool Compress(string dir, string targetFileName)
{
bool rv = false;
bool isOverWrite = false;
ZipHelpEventArgs e = new ZipHelpEventArgs(targetFileName, isOverWrite);
//如果已经存在目标文件,询问用户是否覆盖
if (File.Exists(targetFileName))
{
_ProcessOverwrite(e);
if (e.IsOverWrite == false)
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);
}
zipOut.Finish();
zipOut.Close();
if (File.Exists(targetFileName))
rv = true;
else
rv = false;
}
catch (Exception ex)
{
rv = false;
throw new System.NotImplementedException(ex.Message);
}
return rv;
}
/// <summary>
/// 解压缩文件
/// </summary>
/// <remarks>支持递归解压缩(限1层递归),即压缩后文件中若出现zip文件,则继续解压缩。</remarks>
/// <param name="fileName">要解压缩的文件</param>
/// <param name="targetDir">解压缩目录</param>
/// <param name="allowRecursive">是否允许递归解压缩</param>
public 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 ex;
}
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 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>
/// <param name="dir">目录路径</param>
/// <param name="targetFileName">压缩后的文件路径</param>
public bool Compress(string dir, string targetFileName)
{
bool rv = false;
bool isOverWrite = false;
ZipHelpEventArgs e = new ZipHelpEventArgs(targetFileName, isOverWrite);
//如果已经存在目标文件,询问用户是否覆盖
if (File.Exists(targetFileName))
{
_ProcessOverwrite(e);
if (e.IsOverWrite == false)
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);
}
zipOut.Finish();
zipOut.Close();
if (File.Exists(targetFileName))
rv = true;
else
rv = false;
}
catch (Exception ex)
{
rv = false;
throw new System.NotImplementedException(ex.Message);
}
return rv;
}
/// <summary>
/// 解压缩文件
/// </summary>
/// <remarks>支持递归解压缩(限1层递归),即压缩后文件中若出现zip文件,则继续解压缩。</remarks>
/// <param name="fileName">要解压缩的文件</param>
/// <param name="targetDir">解压缩目录</param>
/// <param name="allowRecursive">是否允许递归解压缩</param>
public 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 ex;
}
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 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;
}