.net core 下文件压缩打包下载
.net core 下文件压缩打包下载
文章目录
- .net core 下文件压缩打包下载
- 前言
- 一、添加NuGet包(SharpCompress)
- 二、ZipHelp帮助类
前言
在一些场景中我们需要将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"));}}
.net core 下文件压缩打包下载相关推荐
- 压缩命令_Linux环境下文件压缩打包命令详解
你好,我是goldsunC 让我们一起进步吧! 前言 我们知道,在面向对象的程序设计中,一切皆对象.而在Linux操作系统中,一切皆文件,因此我们总会跟文件打交道. Linux文件系统很庞大复杂,不过 ...
- zip格式压缩文件并打包下载
压缩文件并下载 zip格式压缩文件并打包下载 zip格式压缩文件并打包下载 Map<String, String> param = new HashMap<String, Strin ...
- JavaWeb通过http下载多个文件,打包下载,压缩下载
JavaWeb通过http下载多个文件,打包下载,压缩下载 DownloadController.java 代码如下 package gov.zhbs.web;import gov.zhbs.util ...
- C# 用zip,批量压缩文件,并打包下载
C# 用zip,批量压缩文件,并打包下载,无需产生临时文件,二种都可以 using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.Sharp ...
- Java将一个文件夹下多个文件压缩并下载(工作案例)
Java将一个文件下多个文件压缩并下载,文件夹目录如下: 每个文件下都有文件,要求实现将文件夹"A2023001_检查"压缩成"A2023001.zip",如下 ...
- javaweb通过接口来实现多个文件压缩和下载(包括单文件下载,多文件批量下载)
程序员在做web等项目的时候,往往都需要添加文件上传.下载.删除的功能,有时是单文件,有时多文件批量 操作,而这些功能的代码程序员可以自己收藏起来当成工具使用,这样,程序员在进行程序设计的时候就会 ...
- dd命令打包多个文件_linux的tar命令详情;linux多个文件压缩打包到一个压缩文件...
tar命令 可以用来压缩打包单文件.多个文件.单个目录.多个目录. Linux打包命令_tar tar命令可以用来压缩打包单文件.多个文件.单个目录.多个目录. 常用格式: 单个文件压缩打包 tar ...
- linux多个文件打包命令行,linux命令五十七之tar命令;linux多个文件压缩打包到一个压缩文件...
tar命令linux 能够用来压缩打包单文件.多个文件.单个目录.多个目录.shell Linux打包命令_tarspa tar命令能够用来压缩打包单文件.多个文件.单个目录.多个目录..net 经常 ...
- ASP.NET多文件批量打包下载
在项目实施中,遇到了将多个文件一起打包后,提供给用户下载.如:在一个ASP.NET的开发项目中,通过一个GridView选中对应行数据的CheckBox,就可以实现对选中文件的打包下载了. 在对多文件 ...
- 关于linux系统下文件压缩归档操作命令略提
Linux下文件压缩归档操作命令:gzip/ungzip 后缀 .gz xz/unxz 后缀 .xz bzip2/bunzip2 后缀 .bz2 tar 用于归档并压缩 -# :# ...
最新文章
- spring boot hello world 搭建
- python培训班哪些比较好-python培训机构哪家比较好?
- MD5加密字符串并转化为base64(C#和PHP代码相同实现)
- HBase常用的Shell命令
- flask echarts词云可视化_基于flask框架的高校舆情分析系统
- ndk学习6: 使用gdb调试ndk程序一
- winserver2016 401您无权使用所提供的凭据查看此目录或页面_不用找了,30分钟帮你搞定使用 Spring Cloud 和 Docker 轻松构建微服务架构!...
- 可变悬挂调节软硬_【5040地推合作品牌】荷兰高性能减震器及悬挂系统品牌—KONI...
- 计算机系统基础 数据的表示和存储
- C语言数理逻辑题目,数学逻辑推理题整理,看看你能答对多少
- 循环队列CircleQueue的使用
- 1.6 编程基础之一维数组 05 年龄与疾病 python
- ECMAScript 发展简史
- 值得苦练的100道Python经典练手题,(附详细答案)
- 计算机考试怎么切换到桌面,考试系统很多考试系统全屏无法切换桌面,只能 – 手机爱问...
- 给出问题的答案 你就可以成为百万富翁
- Java具体可以做什么?
- 浅谈智能DNS云解析(二)
- linux系统优点和缺点,Linux系统和Windows系统的各自的优缺点有哪些
- h5做app和原生app有什么区别?