【发布时间】:2015-11-13 08:03:41
【问题描述】:
所以我已经实现了压缩文件,但现在我遇到了另一个问题,即 zip 文件夹包含空文件。压缩后的文件大小为0字节。
这就是我压缩文件的方式
try
{
var outPutDirectory = AppDomain.CurrentDomain.BaseDirectory;
string logoimage = Path.Combine(outPutDirectory, "images\\error.png");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BufferOutput = false;
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=pauls_chapel_audio.zip");
using (MemoryStream ms = new MemoryStream())
{
// create new ZIP archive within prepared MemoryStream
using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
var demoFile = zip.CreateEntry(logoimage);
// add some files to ZIP archive
}
ms.WriteTo(HttpContext.Current.Response.OutputStream);
}
return true;
}
另一个问题是压缩文件夹与图像的路径相同。所以就像
ZippFolder/A/B/C/image...
我只需要
ZipFolder/content
【问题讨论】:
-
嗯 2 件事 1. 您的代码似乎是 asp.net 而不是 c# 特定的? 2.你正在制作一个很好的记忆流,但它从来没有“准备好”,也就是设置为任何内容?
-
不知道怎么写。我是这方面的新手,你能举个例子吗?
-
HttpContext 听起来像是来自 asp.net 的东西,因此您应该将该标签添加到您的问题中。至于 memoryStream 你做 new MemoryStream() 但只创建一个空的内存流。您没有设置源的来源(因为我不知道有关您的项目的更多详细信息,所以我不能说您应该如何设置/初始化它)。您应该添加数据应该来自哪里。如果是来自文件下载/上传对话框,...
-
暂时来自静态文件。
-
但整个事情应该是一个网络应用程序?
标签: c# asp.net compression