下载多个文件,压缩在.NET [英] download multiple files as zip in .net

查看:160
本文介绍了下载多个文件,压缩在.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件列表,每个文件的复选框,如果用户检查多个文件,然后点击下载我不得不压缩所有这些文件并下载......就像在邮件的附件。

i have a file list with check box for each files , if the user checks many files and click download i have to zip all those files and download it... like in mails attachments..

我已经在使用这个code提<一href="http://stackoverflow.com/questions/3889521/response-addheadercontent-disposition-not-opening-file-in-ie6">post对于单个文件下载

i have used the code mention in this post for single file download

请帮助如何下载多个文件,ZIP ..

pls help how to download multiple files as zip..

推荐答案

您需要打包的文件和写一个结果的回应。 您可以使用 SharpZipLib COM pression库。

You need to pack files and write a result to a response. You can use SharpZipLib compression library.

code例如:

Response.AddHeader("Content-Disposition", "attachment; filename=" + compressedFileName + ".zip");
Response.ContentType = "application/zip";

using (var zipStream = new ZipOutputStream(Response.OutputStream))
{
    foreach (string filePath in filePaths)
    {
        byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);

        var fileEntry = new ZipEntry(Path.GetFileName(filePath))
        {
            Size = fileBytes.Length
        };

        zipStream.PutNextEntry(fileEntry);
        zipStream.Write(fileBytes, 0, fileBytes.Length);
    }

    zipStream.Flush();
    zipStream.Close();
}

这篇关于下载多个文件,压缩在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆