用SharpZipLib对文件夹进行压缩;FastZip用于快速压缩目录
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="zipFileName">zip路径</param>
/// <param name="sourceDirectory">目标文件夹</param>
/// <returns></returns>
public static void CompressFilesToZip(string zipFileName, string sourceDirectory)
{
try
{
if (File.Exists(zipFileName))
{
File.Delete(zipFileName);
}
FastZipEvents events = new FastZipEvents();
ZipNameTransform zipName = new ZipNameTransform(Directory.GetParent(sourceDirectory).FullName);
FastZip packZip = new FastZip(events);
events.ProcessFile = (sender, args) =>
{
packZip.NameTransform = zipName; // 为了把指定的父目录也打包到zip里面
};
events.ProcessDirectory += (sender, args) =>
{
packZip.NameTransform = zipName;
};
packZip.CreateEmptyDirectories = true;
packZip.RestoreAttributesOnExtract = true;
packZip.RestoreDateTimeOnExtract = true;
packZip.CreateZip(zipFileName, sourceDirectory, true, String.Empty);
}
catch (Exception e)
{
LogHelper.LogInstance.Log("CompressFilesToZip Error! " + e.ToString(), Category.Exception);
}
}
递归复制文件夹及文件
/// <summary>
/// 复制文件夹及文件
/// </summary>
/// <param name="sourceFolder">原文件路径</param>
/// <param name="destFolder">目标文件路径</param>
/// <returns></returns>
private static void CopyFolder(string sourceFolder, string destFolder)
{
//如果目标路径不存在,则创建目标路径
if (!System.IO.Directory.Exists(destFolder))
{
System.IO.Directory.CreateDirectory(destFolder);
}
//得到原文件根目录下的所有文件
string[] files = System.IO.Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
string name = System.IO.Path.GetFileName(file);
string dest = System.IO.Path.Combine(destFolder, name);
System.IO.File.Copy(file, dest);//复制文件
}
//得到原文件根目录下的所有文件夹
string[] folders = System.IO.Directory.GetDirectories(sourceFolder);
foreach (string folder in folders)
{
string name = System.IO.Path.GetFileName(folder);
string dest = System.IO.Path.Combine(destFolder, name);
CopyFolder(folder, dest);//构建目标路径,递归复制文件
}
}
项目需求是对多层级的文件夹进行压缩,第一次尝试使用Ionic.Zip进行压缩,但是在多层级文件夹压缩时总户卡到Save()方法,暂不清楚什么原因,在调整压缩等级后,可以压缩成功,但是压缩的文件会有损坏,最后没有采用Ionic.Zip。
如下是网上找的SharpZipLib的另一种压缩方式,但该方式最后打的压缩包再解压时会偶发文件损坏,在用7zip解压缩时会提示“文件末端损坏”的异常。
/// <summary>
/// 压缩多层目录
/// </summary>
/// <param name="strDirectory"></param>
/// <param name="zipedFile"></param>
public static void ZipFileDirectory(string strDirectory, string zipedFile)
{
using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
{
using (ZipOutputStream s = new ZipOutputStream(ZipFile))
{
ZipSetp(strDirectory, s, "");
}
}
}
/// <summary>
/// 递归遍历目录
/// </summary>
/// <param name="strDirectory"></param>
/// <param name="s"></param>
/// <param name="parentPath"></param>
private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
{
if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
{
strDirectory += Path.DirectorySeparatorChar;
}
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strDirectory);
foreach (string file in filenames)// 遍历所有的文件和目录
{
// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (Directory.Exists(file))
{
string pPath = parentPath;
pPath += file.Substring(file.LastIndexOf("\\") + 1);
pPath += "\\";
ZipSetp(file, s, pPath);
}
else // 否则直接压缩文件
{
//打开压缩文件
using (FileStream fs = File.OpenRead(file))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(fileName);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
}
}
SharpZipLib和Ionic.Zip都支持加密压缩。