How to zip a folder

2019-09-04 04:48发布

I have a link. When user will click this button, I want to run some code that will zip a folder on my desktop and, start downloading it.

Folder path is C:/users/dave/desktop/myFolder

in aspx file:

<asp:HyperLink ID="HyperLink1" runat="server">zip folder and download </asp:HyperLink>

In aspx.vb file:

How can I write code here that will zip a folder on my desktop and start downloading this zip folder? Is there way to do it without downloading extra libraries and plugins?

标签: .net vb.net zip
1条回答
叼着烟拽天下
2楼-- · 2019-09-04 05:12

Add a reference to System.IO.Compression.FileSystem

Then you can zip the folder using the following:

Dim tempFile = System.IO.Path.GetTempFileName() + ".zip"
System.IO.Compression.ZipFile.CreateFromDirectory("C:\temp\awesome", tempFile)

Then to download it, you can send it in the response:

Response.Buffer = false
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=Desktop.zip")
Response.ContentType = "Application/zip"
Response.TransmitFile(tempFile)
Response.End()
查看更多
登录 后发表回答