SharpZipLibを使用して.netでhttp経由でzipファイルをストリーミングする
質問
ユーザーがサイトからすべての画像をダウンロードできるように、単純なダウンロードサービスを作成しています。 そのためには、すべてをhttpストリームに圧縮します。
ただし、すべてがメモリに保存されているようで、zipファイルが完全で出力が閉じられるまでデータは送信されません。 サービスがすぐに送信を開始し、メモリを使いすぎないようにします。
public void ProcessRequest(HttpContext context)
{
List<string> fileNames = GetFileNames();
context.Response.ContentType = "application/x-zip-compressed";
context.Response.AppendHeader("content-disposition", "attachment; filename=files.zip");
context.Response.ContentEncoding = Encoding.Default;
context.Response.Charset = "";
byte[] buffer = new byte[1024 * 8];
using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipOutput = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(context.Response.OutputStream))
{
foreach (string fileName in fileNames)
{
ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);
zipOutput.PutNextEntry(zipEntry);
using (var fread = System.IO.File.OpenRead(fileName))
{
ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(fread, zipOutput, buffer);
}
}
zipOutput.Finish();
}
context.Response.Flush();
context.Response.End();
}
ファイルを作成している間にワーカープロセスのメモリが増大し、送信が完了するとメモリを解放することがわかります。大量のメモリを使用せずにこれを行うにはどうすればよいですか?
解決
で応答バッファリングを無効にします
> context.Response.BufferOutput = false; そして、コードの最後から Flush
呼び出しを削除します。
他のヒント
Response.BufferOutput = false;を使用します。 ProcessRequestの開始時に、各ファイルの後に応答をフラッシュします。
FYI。これは、ブラウザへのストリーミングを使用して、ファイルのツリー全体を再帰的に追加する作業コードです:
string path = @"c:\files";
Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", "hive.zip"));
Response.BufferOutput = false;
byte[] buffer = new byte[1024 * 1024];
using (ZipOutputStream zo = new ZipOutputStream(Response.OutputStream, 1024 * 1024)) {
zo.SetLevel(0);
DirectoryInfo di = new DirectoryInfo(path);
foreach (string file in Directory.GetFiles(di.FullName, "*.*", SearchOption.AllDirectories)) {
string folder = Path.GetDirectoryName(file);
if (folder.Length > di.FullName.Length) {
folder = folder.Substring(di.FullName.Length).Trim('\\') + @"\";
} else {
folder = string.Empty;
}
zo.PutNextEntry(new ZipEntry(folder + Path.GetFileName(file)));
using (FileStream fs = File.OpenRead(file)) {
ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(fs, zo, buffer);
}
zo.Flush();
Response.Flush();
}
zo.Finish();
}
Response.Flush();
所属していません StackOverflow