Problem with ZipFile.Save(Response.OutputStream)

Dec 3, 2009 at 9:14 AM

Hi,

I try to create a zip file with multiple documents in it. below is the code I use:

Using zip As New ZipFile()
      'TODO: handle same filename error
      If Not String.IsNullOrEmpty(dossierIdInvestplan) Then
        For Each itemKoopsom As ListItem In cblDocumentsKoopsom.Items
          If itemKoopsom.Selected Then
            zip.AddEntry(itemKoopsom.Value & ".rtf", "", GetUl3Document(dossierIdInvestplan, itemKoopsom.Value))
          End If
        Next
      End If

      If Not String.IsNullOrEmpty(dossierIdSaveplan) Then
        For Each itemSpaarinspanning As ListItem In cblDocumentsSpaarinspanning.Items
          If itemSpaarinspanning.Selected Then
            zip.AddEntry(itemSpaarinspanning.Value & ".rtf", "", GetUl3Document(dossierIdSaveplan, itemSpaarinspanning.Value))
          End If
        Next
      End If

      Dim archiveName As String = String.Format("archive-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))

      Response.Clear()
      Response.BufferOutput = False
      Response.ContentType = "application/zip"
      Response.AddHeader("content-disposition", "attachment; filename=" + archiveName)

      'zip.Save("C:\Documents and Settings\A00428\Desktop\" & archiveName)
      zip.Save(Response.OutputStream)


    End Using
    HttpContext.Current.ApplicationInstance.CompleteRequest()

GetUl3Document() returns a string which represents an rtf document.
When I use the overloaded Save function to save to a filesystem the zip is correctly saved, I can open it and the files are all intact and correct, so far so good...
However, when I use the Response.OutputStream Save function (Which is really the one I need) I get a save dialog, the zip gets saved but when I try to open it, it says the file is corrupt.
I don't really see anything wrong in my code but obviously something is. I already searched the forums but found nothing that helped so any help is greatly appreciated!

Coordinator
Dec 3, 2009 at 5:51 PM

Your usage is probably correct.

Try replacing 

   HttpContext.Current.ApplicationInstance.CompleteRequest()

with 

   Response.Close()

 

Dec 4, 2009 at 7:07 AM

Sweet! it works! Thank you very much!

Coordinator
Dec 4, 2009 at 7:21 AM

Hey Dimitri - glad it works.   Where did you get that code?    Is there a sample that shows HttpContext.Current.ApplicationInstance.CompleteRequest() ?    I'd like to correct it if I can.

 

Dec 4, 2009 at 7:40 AM

I did not get that piece of code from a help file sample but I came across it a few times here on the forums. While testing I remember using Response.close once and it didn't work, but I probably had some other settings wrong at that time which resulted in a corrupt zip file. Thats why I changed it to something else.