C# 利用 SharpZipLib 对多个文本字符串进行多文件打包为RAR或ZIP并进行下载


功能需求

需要对数据库中表结构,每个表生成创建表的脚本(sql),然后一键打包批量下载所有的表结构脚本

实现效果如图:

点击下载,生成rar文件

压缩包内容,每个表为单独的一个.sql 脚本

实现

Nuget 添加引用 SharpZipLib

打包代码,( 在内存中打包,不会生成本地缓存文件 )

C# 全选
private MemoryStream DoZip(Dictionary<string, string> contents)
{
	MemoryStream memoryStream = new MemoryStream();
	ZipOutputStream zipOutStream = new ZipOutputStream(memoryStream);
	foreach (string key in contents.Keys)
	{
		// 压缩包中的文件名
		string fileName = key;
		ZipEntry entry = new ZipEntry(fileName);
		entry.DateTime = DateTime.Now;
		entry.IsUnicodeText = true;
		zipOutStream.PutNextEntry(entry);

		// 文件中内容
		string content = contents[key];
		byte[] array = Encoding.ASCII.GetBytes(content);
		zipOutStream.Write(array, 0, array.Length);
		zipOutStream.CloseEntry();
	}
	//使用流操作时一定要设置IsStreamOwner为false。否则很容易发生在文件流关闭后的异常。
	zipOutStream.IsStreamOwner = false;
	zipOutStream.Finish();
	zipOutStream.Close();

	//byte[] buffer = memoryStream.GetBuffer();
	// 避免后面使用的时候,忘了复位,造成读取挂起,等同于position==0
	memoryStream.Seek(0, SeekOrigin.Begin);
	return memoryStream;

}
 
GarsonZhang www.yesdotnet.com

一般处理程序ashx使用

byte[] buffer = memoryStream.GetBuffer();

context.Response.Clear();
context.Response.Charset = "UTF-8";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.AddHeader("Content-Type", "application/octet-stream");

// 添加头信息,为"文件下载/另存为"对话框指定默认文件名,设定编码为UTF8,防止中文文件名出现乱码
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(projObj.SysName + $"-数据库脚本_{DBType}.zip", System.Text.Encoding.UTF8));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
context.Response.AddHeader("Content-Length", buffer.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
context.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
//context.Response.Write(ms);
context.Response.BinaryWrite(buffer);
//context.Response.WriteFile(file.FullName);
// 停止页面的执行
context.Response.End();
GarsonZhang www.yesdotnet.com

 

.net core mvc使用

C# 全选
public IActionResult Export(P_Export data)
{
	Dictionary<string, string> o = new Dictionary<string, string>();
	o.Add("test01.txt", "test01");
	o.Add("test02.txt", "test02");
	o.Add("test03.txt", "test03");
	o.Add("test04.txt", "test04");
	o.Add("test05.txt", "test05");

	MemoryStream ms = DoZip(o);
	
	return File(ms, "application/octet-stream", "test.rar");
}
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
YES开发框架
上一篇:C# Winform 控件Menustrip中ToolStripMenuItem显示提示框ToolTipText
下一篇:制作一个标准 ICO 图标
评论列表

发表评论

评论内容
昵称:
关联文章

C# 利用 SharpZipLib 文本字符串进行文件打包RARZIP进行下载
C#使用SharpZipLib进行压缩文件(*.rar,*.zip)操作,压缩和解压
C# 利用SharpZipLib*.rar压缩文件解压报错
C# 设置验证 PDF中的文本域格式
文件目录损坏且无法读取
FastReport 合并报表,一次打印rpt文件
Python pyinstaller 打包参数说明
C# 根据DataTable 转换成JSON 文本字符串数据
npm运行server npm-run-all通知执行任务
Windows下Git账号配置,同一电脑ssh-key的管理
使用 WPF 做 PowerPoint 系列 基于 OpenXML 解析实现 PPT 文本描边效果
1.服务端端口进行侦听
使用 WPF 做 PowerPoint 系列 文本 BodyProperties 的 FontScale 与文本字号缩放
JSON.stringify 输出JSON字符串日期Date类型的处理
在ABP VNext框架中HttpApi模块的控制器进行基类封装
同时请求_axios并发请求
C#未来的一点感悟
C# 线程入门系列(三)
C# 线程入门系列(二)
C# 线程入门系列(一)

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码