ZipArchive 返回空文件 c#
ZipArchive returning empty files c#
所以我已经实现了压缩文件,但现在我遇到了另一个问题,即 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
var demoFile = zip.CreateEntry(logoimage);
这会在 ZIP 文件中创建一个名为 logoimage
的条目(即 /A/B/C/images/error.png
或任何完整路径)。
但是你从来没有写过那个条目,所以它是空的。另外,如果你想有不同的路径,你应该在那里指定它:
var demoFile = zip.CreateEntry("content\error.png");
using (StreamWriter writer = new StreamWriter(demoFile.Open()))
using (StreamReader reader = new StreamReader(logoimage))
{
writer.Write(reader.ReadToEnd());
}
或者,您也可以完全跳过 StreamWriter
,直接写入流:
using (Stream stream = demoFile.Open())
using (StreamReader reader = new StreamReader(logoimage))
{
reader.BaseStream.CopyTo(stream);
}
顺便说一句。您可以跳过要先写入 zip 文件的外部 MemoryStream
,然后将该流写入 OutputStream
。相反,您可以直接写入该流。只需将其传递给 ZipFile
构造函数:
Stream output = HttpContext.Current.Response.OutputStream;
using (ZipArchive zip = new ZipArchive(output, ZipArchiveMode.Create, true))
{
…
}
所以我已经实现了压缩文件,但现在我遇到了另一个问题,即 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
var demoFile = zip.CreateEntry(logoimage);
这会在 ZIP 文件中创建一个名为 logoimage
的条目(即 /A/B/C/images/error.png
或任何完整路径)。
但是你从来没有写过那个条目,所以它是空的。另外,如果你想有不同的路径,你应该在那里指定它:
var demoFile = zip.CreateEntry("content\error.png");
using (StreamWriter writer = new StreamWriter(demoFile.Open()))
using (StreamReader reader = new StreamReader(logoimage))
{
writer.Write(reader.ReadToEnd());
}
或者,您也可以完全跳过 StreamWriter
,直接写入流:
using (Stream stream = demoFile.Open())
using (StreamReader reader = new StreamReader(logoimage))
{
reader.BaseStream.CopyTo(stream);
}
顺便说一句。您可以跳过要先写入 zip 文件的外部 MemoryStream
,然后将该流写入 OutputStream
。相反,您可以直接写入该流。只需将其传递给 ZipFile
构造函数:
Stream output = HttpContext.Current.Response.OutputStream;
using (ZipArchive zip = new ZipArchive(output, ZipArchiveMode.Create, true))
{
…
}