118 lines
3.5 KiB
C#
118 lines
3.5 KiB
C#
using ICSharpCode.SharpZipLib.Zip;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace YLPublishTool
|
|
{
|
|
/// <summary>
|
|
/// 压缩目录
|
|
/// </summary>
|
|
[Description("压缩目录")]
|
|
class ZipDirAction : IPublishAction
|
|
{
|
|
/// <summary>
|
|
/// 压缩目录
|
|
/// </summary>
|
|
public string SrcDir { get; set; }
|
|
|
|
/// <summary>
|
|
/// 目标文件路径
|
|
/// </summary>
|
|
public string TargetFilePath { get; set; }
|
|
|
|
public string IncludeFilePattern { get; set; }
|
|
public string ExcludeFilePattern { get; set; }
|
|
|
|
public void Execute(IPublishContext context)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(SrcDir))
|
|
{
|
|
throw new Exception("压缩目录不能为空");
|
|
}
|
|
|
|
var srcDir = new DirectoryInfo(SrcDir);
|
|
|
|
if (!srcDir.Exists)
|
|
{
|
|
throw new DirectoryNotFoundException("压缩文件夹不存在: " + SrcDir);
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(TargetFilePath))
|
|
{
|
|
throw new Exception("目标文件不能为空");
|
|
}
|
|
|
|
if (File.Exists(TargetFilePath))
|
|
{
|
|
File.Delete(TargetFilePath);
|
|
}
|
|
|
|
var fileFilter = CreateFileFilter();
|
|
var fastZip = new FastZip();
|
|
fastZip.CreateZip(TargetFilePath, SrcDir, true, fileFilter);
|
|
}
|
|
|
|
private string CreateFileFilter()
|
|
{
|
|
var filters = new List<string>(8);
|
|
if (!string.IsNullOrWhiteSpace(IncludeFilePattern))
|
|
{
|
|
var splits = IncludeFilePattern.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (var sp in splits)
|
|
{
|
|
var s = sp.Trim();
|
|
if (s.Length < 1)
|
|
{
|
|
continue;
|
|
}
|
|
var endDollar = !s.EndsWith("*");
|
|
s = s.Trim('*');
|
|
s = Regex.Escape(s).Replace("\\*", ".*") + (endDollar ? "$" : "");
|
|
filters.Add(s);
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(ExcludeFilePattern))
|
|
{
|
|
var splits = ExcludeFilePattern.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (var sp in splits)
|
|
{
|
|
var s = sp.Trim();
|
|
if (s.Length < 1)
|
|
{
|
|
continue;
|
|
}
|
|
var endDollar = !s.EndsWith("*");
|
|
s = s.Trim('*');
|
|
s = Regex.Escape(s).Replace("\\*", ".*") + (endDollar ? "$" : "");
|
|
filters.Add("-" + s);
|
|
}
|
|
}
|
|
|
|
return filters.Count > 0 ? string.Join(";", filters) : null;
|
|
}
|
|
|
|
public void SetProperty(string name, string value)
|
|
{
|
|
switch (name)
|
|
{
|
|
case "压缩目录":
|
|
SrcDir = value;
|
|
break;
|
|
case "目标文件":
|
|
TargetFilePath = value;
|
|
break;
|
|
case "包含文件":
|
|
IncludeFilePattern = value;
|
|
break;
|
|
case "排除文件":
|
|
ExcludeFilePattern = value;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|