使用ASP.NET,如何从字符串数组中创建一个zip文件? [英] Using ASP.NET how can I create a zip file from an array of strings?
问题描述
我正在使用ASP.NET,并且我更喜欢VB作为语言,但是我应该能够根据需要翻译C#.
I am using ASP.NET and I prefer VB as the language, but I should be able to translate C# for my needs.
我有一个字符串数组,我希望将这些字符串作为单独的文件发送给浏览器,以供用户保存.在搜索Internet时,将多个文件发送到浏览器的最常见解决方案是将它们压缩并发送一个zip文件.
I have an array of strings which I would like to send to the browser as individual files for the user to save. In searching the internet, the most common solution to send multiple files to a browser is to zip them up, and send a single zip file.
为此,我需要学习一些我不知道的东西;
Towards that end, I need to learn a few things I do not know;
1)我可以使用哪些工具/方法(最好是内置在IIS7上的ASP.NET内置)来创建一个zip文件流以发送到浏览器?
1) What tool/methods (preferably built in to ASP.NET running on IIS7) can I use to create a zip filestream to send to a browser?
2)如何使zip工具误以为它从内存中的字符串中获取多个文件?我假设我需要创建文件流,但是如何告诉方法文件名是什么,等等?
2) How do I fool the zip tool into thinking it is getting multiple files from strings in memory? I assume I need to create filestreams, but how do I tell the methods what the file name is, etc.?
如果有一个做一些与我需要的东西基本相似的事情的例子,那就太好了.只是指向我.
If there is an example of doing something substantially similar to what I need available, that would be great. Just point me at it.
感谢您的帮助.
推荐答案
方法可能是:
- 将字符串转换为流
- 将数据流中的数据添加到zip文件中
- 将zip文件写入响应流
下面的代码示例:
ZipFile zipFile = new ZipFile();
int fileNumber = 1;
foreach(string str in strArray)
{
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(contents);
MemoryStream stream = new MemoryStream(byteArray);
stream.Seek(0, SeekOrigin.Begin);
//add the string into zip file with a name
zipFile.AddEntry("String" + fileNumber.ToString() + ".txt", "", stream);
}
Response.ClearContent();
Response.ClearHeaders();
Response.AppendHeader("content-disposition", "attachment; filename=strings.zip");
zipFile.Save(Response.OutputStream);
zipFile.Dispose();
这篇关于使用ASP.NET,如何从字符串数组中创建一个zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!