目的
指定一个文件夹路径,想要连这个文件夹也进行压缩。
比如:J:\ABC\Android
结果:
public static string 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)
{
return e.Message;
}
return String.Empty;
}
[UnityEditor.MenuItem("程序工具/测试压缩")]
public static void Test()
{
string str = CompressFilesToZip("abc.zip", @"J:\ABC\Android");
Debug.Log(str);
}