前面有生成Excel或Word的示例,所以就不再重新写了。
这里只提供将指定文件以ZIP的方式下载。
创建一个 Zip工具类
public class ZIPCompressUtil { public static byte[] Zip(List<string> AllFilesPath) { try { if (AllFilesPath.Count > 0) { MemoryStream ms = new MemoryStream(); byte[] buffer = null; using (ZipFile file = ZipFile.Create(ms)) { file.BeginUpdate(); file.NameTransform = new MyNameTransfom(); //通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。 for (int i = 0; i < AllFilesPath.Count; i++) { string strFile = AllFilesPath[i]; file.Add(strFile); } file.CommitUpdate(); buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); } return buffer; } return null; } catch { return null; } } }