.net core 下文件压缩打包下载
前言
在一些场景中我们需要将word、pdf等文件批量导出,此篇用来记录在.net core下文件压缩打包下载。以下是本篇文章正文内容,下面案例可供参考
一、添加NuGet包(SharpCompress)
二、ZipHelp帮助类
代码如下(示例):
命名空间:
using Microsoft.AspNetCore.Mvc;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using SharpCompress.Writers;
using SharpCompress.Writers.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Web;
代码:
public class ZipHelp
{
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="directory">要压缩文件夹</param>
/// <param name="zipName">压缩包名称</param>
/// <param name="tempPatn">压缩包临时目录</param>
/// <returns></returns>
public static IActionResult ZipDownload(DirectoryInfo directory, string zipName,string tempPatn)
{
string path = tempPatn + @"\temp.zip";
var filesList = directory.GetFiles();
//var stream = File.OpenWrite(path);
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (var zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate))
{
foreach (var file in filesList)
{
zipWriter.Write(file.Name, file);
}
}
}
byte[] buffer = null;
using (FileStream fileStream = File.OpenRead(path))
{
buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
}
File.Delete(path);
return new FileStreamResult(new MemoryStream(buffer), "application/zip") { FileDownloadName = JointZip(zipName) };
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileList">要压缩文件列表</param>
/// <param name="zipName">压缩包名称</param>
/// <param name="tempPatn">压缩包临时目录</param>
/// <returns></returns>
public static IActionResult ZipDownload(FileInfo[] filesList, string zipName, string tempPatn)
{
string path = tempPatn + @"\temp.zip";
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (var zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate))
{
foreach (var file in filesList)
{
zipWriter.Write(file.Name, file);
}
}
}
byte[] buffer = null;
using (FileStream fileStream = File.OpenRead(path))
{
buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
}
File.Delete(path);
return new FileStreamResult(new MemoryStream(buffer), "application/zip") { FileDownloadName = JointZip(zipName) };
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileList">要压缩文件列表</param>
/// <param name="zipName">压缩包名称</param>
/// <param name="tempPatn">压缩包临时目录</param>
/// <returns></returns>
public static IActionResult ZipDownload(List<FileInfo> filesList, string zipName, string tempPatn)
{
string path = tempPatn + @"\temp.zip";
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (var zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate))
{
foreach (var file in filesList)
{
zipWriter.Write(file.Name, file);
}
}
}
byte[] buffer = null;
using (FileStream fileStream = File.OpenRead(path))
{
buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
}
File.Delete(path);
return new FileStreamResult(new MemoryStream(buffer), "application/zip") { FileDownloadName = JointZip(zipName) };
}
/// <summary>
/// 如果文件名中没有后缀名,增加文件后缀名
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private static string JointZip(string fileName)
{
if (!fileName.EndsWith(".zip"))
{
fileName += ".zip";
}
return HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
}
}