C# 如何生成临时 Zip 文件,然后在下载后自动删除它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/921308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I generate a temporary Zip file, then auto-remove it after it is downloaded?
提问by Ibrahim AKGUN
I have a download page where there are 3 download options: Word, Zip, and PDF. There is a folder containing .doc
files. When a user clicks the Zip option on the page, I want ASP.NET to zip the folder with the .doc
files into a temporary .zip
file. Then the client will download it from the server. When the user's download is finished, the temporary Zip file should delete itself.
我有一个下载页面,其中有 3 个下载选项:Word、Zip 和 PDF。有一个包含.doc
文件的文件夹。当用户单击页面上的 Zip 选项时,我希望 ASP.NET 将包含文件的.doc
文件夹压缩到一个临时.zip
文件中。然后客户端将从服务器下载它。用户下载完成后,临时 Zip 文件应自行删除。
How can I do this with ASP.NET 2.0 C#?
如何使用 ASP.NET 2.0 C# 执行此操作?
Note: I know how I can zip and unzip files and remove files from the system with C# ASP.NET 2.0.
注意:我知道如何使用 C# ASP.NET 2.0 压缩和解压缩文件以及从系统中删除文件。
采纳答案by Ibrahim AKGUN
I fixed my problem by adding this to the end of the stream code:
我通过将其添加到流代码的末尾来解决我的问题:
Response.Flush();
Response.Close();
if(File.Exist(tempFile))
{File.Delete(tempFile)};
回答by Paul Alexander
You'll want to stream the zip file to the user manually, then delete the file when streaming is complete.
您需要手动将 zip 文件流式传输给用户,然后在流式传输完成后删除该文件。
try
{
Response.WriteFile( "path to .zip" );
}
finally
{
File.Delete( "path to .zip" );
}
回答by Cheeso
Using DotNetZipyou can save the zip file directly to the Response.OutputStream. No need for a temporary Zip file.
使用DotNetZip,您可以将 zip 文件直接保存到 Response.OutputStream。不需要临时 Zip 文件。
Response.Clear();
// no buffering - allows large zip files to download as they are zipped
Response.BufferOutput = false;
String ReadmeText= "Dynamic content for a readme file...\n" +
DateTime.Now.ToString("G");
string archiveName= String.Format("archive-{0}.zip",
DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + archiveName);
using (ZipFile zip = new ZipFile())
{
// add a file entry into the zip, using content from a string
zip.AddFileFromString("Readme.txt", "", ReadmeText);
// add the set of files to the zip
zip.AddFiles(filesToInclude, "files");
// compress and write the output to OutputStream
zip.Save(Response.OutputStream);
}
Response.Flush();
回答by Amir
Form Download From DataBase And Zip And Complate Download Remove For Use this Code In Class need using ICSharpCode.SharpZipLib.Zip
从数据库中下载表格和 Zip 和 Complate 下载删除使用此代码在课堂上需要使用 ICSharpCode.SharpZipLib.Zip
if (ds.Tables[0].Rows.Count > 0)
{
// Create the ZIP file that will be downloaded. Need to name the file something unique ...
string strNow = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now);
ZipOutputStream zipOS = new ZipOutputStream(File.Create(Server.MapPath("~/TempFile/") + strNow + ".zip"));
zipOS.SetLevel(5); // ranges 0 to 9 ... 0 = no compression : 9 = max compression
// Loop through the dataset to fill the zip file
foreach (DataRow dr in ds.Tables[0].Rows)
{
byte[] files = (byte[])(dr["Files"]);
//FileStream strim = new FileStream(Server.MapPath("~/TempFile/" + dr["FileName"]), FileMode.Create);
//strim.Write(files, 0, files.Length);
//strim.Close();
//strim.Dispose();
ZipEntry zipEntry = new ZipEntry(dr["FileName"].ToString());
zipOS.PutNextEntry(zipEntry);
zipOS.Write(files, 0, files.Length);
}
zipOS.Finish();
zipOS.Close();
FileInfo file = new FileInfo(Server.MapPath("~/TempFile/") + strNow + ".zip");
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/zip";
Response.WriteFile(file.FullName);
Response.Flush();
file.Delete();
Response.End();
}
}