ASP.NET 将所有文件下载为 Zip
我的网络服务器上有一个文件夹,其中有数百个 mp3 文件。我想为用户提供从网页下载目录中每个 mp3 的压缩存档的选项。
我想仅在需要时以编程方式压缩文件。由于 zip 文件非常大,出于性能原因,我认为需要在压缩时将 zip 文件发送到响应流。
这可能吗?我该怎么做呢?
I have a folder on my web server that has hundreds of mp3 files in it. I would like to provide the option for a user to download a zipped archive of every mp3 in the directory from a web page.
I want to compress the files programmatically only when needed. Because the zip file will be quite large, I am thinking that I will need to send the zip file to the response stream as it is being zipped, for performance reasons.
Is this possible? How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我用 DotNetZip 执行此操作的代码 - 效果非常好。显然,您需要提供outputFileName、folderName 和includeSubFolders 的变量。
Here is code I use to do this with DotNetZip - works very well. Obviously you will need to provide the variables for outputFileName, folderName, and includeSubFolders.
我不敢相信这有多么容易。阅读此内容后,这是我使用的代码:
I can't believe how easy this was. After reading this, here is the code that I used:
您可以添加一个自定义处理程序(.ashx 文件),该处理程序获取文件路径、读取文件并使用压缩库对其进行压缩,然后将具有正确内容类型的字节返回给最终用户。
You could add a custom handler (.ashx file) that takes the file path, reads the file compresses it using a compression library and returns the bytes to the end user with the correct content-type.