TG.WXCRM.V4/Common/ZipFileHelper.cs

51 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ICSharpCode.SharpZipLib.Zip;
using System.Collections.Generic;
using System.IO;
namespace WX.CRM.Common
{
public class ZipFileHelper
{
public static MemoryStream ListZip(List<string> pathList, bool isformatting)
{
MemoryStream ms = new MemoryStream();
using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
if (isformatting)
file.NameTransform = new MyNameTransfom();//通过这个名称格式化器可以将里面的文件名进行一些处理。默认情况下会自动根据文件的路径在zip中创建有关的文件夹。
pathList.ForEach(m =>
{
file.Add(m);
});
file.CommitUpdate();
//var buffer = new byte[ms.Length];
ms.Position = 0;
//ms.Read(buffer, 0, buffer.Length);
ms.Flush();
}
return ms;
}
}
public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform
{
#region INameTransform
public string TransformDirectory(string name)
{
return null;
}
public string TransformFile(string name)
{
return Path.GetFileName(name);
}
#endregion
}
}