C#压缩,下载文件方法
压缩文件方法
/// <summary>
/// 创建压缩包
/// </summary>
/// <param name="list">文件路径集合</param>
/// <param name="destinationZipFilePath">zip压缩包名称以及路径</param>
private void CreateZipPackage(List<string> list, string destinationZipFilePath)
{
using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(destinationZipFilePath)))
{
s.SetLevel(9); // 压缩级别 0-9
byte[] buffer = new byte[4096]; //缓冲区大小
foreach (var item in list)
{
var flieName = item;
ZipEntry entry = new ZipEntry(Path.GetFileName(flieName));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = System.IO.File.OpenRead(flieName))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
下载文件方法:newTextPath下载文件路径,dateNow下载文件命名
/// <summary>
/// 下载分析
/// </summary>
private async Task<IActionResult> DwOriginal(string newTextPath, string dateNow)
{
var memoryStream = new MemoryStream();
using (var stream = new FileStream(newTextPath, FileMode.Open))
{
await stream.CopyToAsync(memoryStream);
}
memoryStream.Seek(0, SeekOrigin.Begin);
string encodeFilename = System.Web.HttpUtility.UrlEncode($"{dateNow}.txt", System.Text.Encoding.GetEncoding("UTF-8"));
Response.Headers.Add("Content-Disposition", "attachment; filename=" + encodeFilename);
return new FileStreamResult(memoryStream, "application/octet-stream");
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界