How to download a zipped folder

Nov 6, 2009 at 2:29 AM

Using DotNetZip library in my vb.net application, I was able to zip a folder but unable to download. I know there is something missing here but I cannot figure out what. any feedback would be greatly appreciated.

 

Private Sub ZipFolder(ByVal FolderToZip As String)
        Response.Clear()
        Response.BufferOutput = False
        app.Events.Write("we are here 1")

        Dim archiveName As String = String.Format(FolderToZip & ".zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
        app.Events.Write("archiveName = " & archiveName)
        Response.ContentType = "application/zip"
        Response.AddHeader("content-disposition", "filename=" + archiveName)
        app.Events.Write("we are here 2")

        Using zip As ZipFile = New ZipFile()
            zip.AddDirectory(FolderToZip)
            zip.Save(FolderToZip & ".zip")

            zip.Save(Response.OutputStream)

            zip.Dispose()
            ' Send the output to the client.
            Response.Flush()
            app.Events.Write("we are here 3")
        End Using
        HttpContext.Current.ApplicationInstance.CompleteRequest()
        ' Response.Close()
        app.Events.Write("we are here 4")
    End Sub

Coordinator
Nov 6, 2009 at 6:00 AM

Seems like you are saving it twice?  Any reason why? 

I don't know what app.Events.Write() does, but if it is writing to the outputstream, it will cause your download to fail.

What is the error you are getting, anyway?

Private Sub ZipFolder(ByVal FolderToZip As String)
        Response.Clear()
        Response.BufferOutput = False

        Dim archiveName As String = String.Format(FolderToZip & ".zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
        Response.ContentType = "application/zip"
        Response.AddHeader("content-disposition", "filename=" + archiveName)

        Using zip As ZipFile = New ZipFile()
            zip.AddDirectory(FolderToZip)
            zip.Save(Response.OutputStream)
        End Using
        Response.Close()
End Sub

Nov 6, 2009 at 7:41 AM

thanks for the reply.

the app.Events.Write() is just my debugging code - it doesn't effect anything

I am able to download now but zip.Save(Response.OutputStream) reveals the whole path to the client which is not a very good practice. any suggestion how to remedy this?

thanks again.

Coordinator
Nov 6, 2009 at 8:57 AM
Edited Nov 6, 2009 at 8:59 AM

Yes - set the archiveName this way. (replace the code you have in your method)

'' get a name for the archive
'' example output: Documents-2009-Nov-06-114211.zip
Dim archiveName As String = _
    Path.GetFileName(FolderToZip) & _
    DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")) & _
    ".zip"


ps: What does the debugging code do ?   If it emits messages into the response outputstream, then it will corrupt the zip file.  If, for example, you use it to update a Web control, enabling these diagnostic messages might cause the zip to break.  Like "The Observer effect."

Nov 6, 2009 at 2:30 PM

The code you mentioned above does not hide the full path. So i am still having trouble with this. 

the app.event.write writes to an event log - it's just a print statment, that's all. I can not by any means have the "The Observer effect"

thanks again and looking forward to your reply

Nov 6, 2009 at 2:52 PM

My humble aplogy - it actualy worked and the path is not revealed any more. thank you so much.

On another note, in another part of my application, I have embeded flash that lets the user selecet a whole bunch of files. Then I have to be able to zip these files in a folder and allow the user to download. The problem is that zip.Save(Response.OutputStream) does not get a chance when flash is involved - and I think that's because Flash is trying to respond as well. Have you ever dealth with Flash and zip download? Do you know of any known issue?

thanks again.

Coordinator
Nov 6, 2009 at 4:52 PM

Ahh, I don't know much about Flash.  and I don't know what you mean by "zip.Save(Response.OutputStream) does not get a chance when flash is involved." 

I think you might be able to solve your problem by:

  • saving the zip to a temporary location on the server filesystem
  • providing a clickable link to the user, or maybe setting up an onClick handler in Flash (I don't know how that would work)
  • sending the file to the user after the click event in flash.  LIke I said, I don't know how to do a download in flash, but I would guess there.s a way.
  • removing the zip file from the server filesystem after it is downloaded, or after some time passes

Good luck!