51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
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
|
||
}
|
||
|
||
|
||
}
|