Why can't open created file with WinZip or WinRar

Apr 24, 2009 at 2:34 PM
Hi
I'm trying to use this excellent component with this code from ASP.NET web page.
I use ASP.NET 2.0, Windows XP, Ionic.Zip.dll 1.7.2.12

 

 

'get data from database

 

Dim

 

name As String = ""
Dim dt As System.Data.DataTable = Nothing
Dim obj As New clsSysFields(1, 1)
obj.funGetData(dt)
Dim str As New StringBuilder()
For i As Integer = 0 To dt.Rows.Count - 1
    For j As Integer = 2 To dt.Columns.Count - 1
        name = dt.Rows(i).Item(j).ToString +
" "
        str.Append(name.Substring(0, 20))
    Next
    str.AppendLine()
Next

Dim
archiveName As String = String.Format("archive-{0}.zip", DateTime.Now.ToString("yyyy-MM-dd"))
Response.Clear()
Response.ContentType =
"application/zip"
Response.AddHeader("content-disposition", "filename=" + archiveName)

Dim
zip As New ZipFile
zip.AddFileFromString(
"Readme.txt", "", str.ToString())
zip.Save(Response.OutputStream)
Response.End()

This code create one zip file, but when i try open this file with WinZip or WinRar, show me this message "Cannot open file: it does not appear to be a valid archive".
I have WinZip 12.0 and WinRar 3.7
The same with compressed files from Windows Explorer.

Can you help me?

Thank you

 

Coordinator
Apr 28, 2009 at 5:34 PM

Why are you not using a Using clause? 
The using clause around the ZipFile is essential.

I delivered an example, very similar to the code you posted.  http://code.msdn.microsoft.com/DotNetZip/Release/ProjectReleases.aspx?ReleaseId=2015  Maybe you used the example as a starting point?    But the example I wrote employed a Using clause, whereas it is missing from your code.

The using clause closes the stream, releases resources.  Try it with the using clause and let me know.

 

May 1, 2009 at 12:47 PM

Thank you for your post.

I change this (from original post code):

 

Dim zip As New ZipFile
zip.AddFileFromString(
"Readme.txt", "", str.ToString())
zip.Save(Response.OutputStream)


for this:

 

Using zip As ZipFile = New ZipFile
    zip.AddFileFromString("Readme.txt", "", str.ToString())
    zip.Save(Response.OutputStream)
End Using

from your suggestion example, but, i get the same  results.





 

 

 

 

Coordinator
May 1, 2009 at 4:17 PM

CD, your code looks correct, and it should work.
I don't know what the problem might be but I can suggest a few things.

  • Try setting Response.Expires = 1 or some other non-zero number.  doc for system.web.httpresponse.expires

    There's a report of a bug that exists in IE7 where a Response.Expires = 0 can lead to inability to save the downloaded file. I have not seen this myself. But it is worth checking.

  • There were a few problems in DotNetZip fixed since v1.7.2.12. They do not, on the surface, seem to be related to the problem you are experiencing. One problem that has been fixed had to do with creating an encrypted zip file in ASP.NET. Another problem now fixed, was that DotNetZip was failing to read some zip archives with comments of a particular form . These do not seem to be related to your issue, but it is worth getting on the latest DotNetZip to be sure.

If those steps don't eliminate the problem, would it be possible for you to share the zip file in question that cannot be opened? You would have to open a workitem (via the tab across the top of this page labeled "Issue Tracker"), create a new workitem, and attach the failing zip file to the newly opened workitem.

May 1, 2009 at 4:39 PM
I'm sorry Cheeso.
You are right. Code is correct.
I finish discovering the error. I am using page compression with the MBCompression library. When i compression turn off, the zipfile generated is Ok. 
I must investigate how to do that both libraries (Ionic and MBCompression) works together.

Thank you again

Coordinator
May 1, 2009 at 5:04 PM
My pleasure. I'm glad you found the source of the problem!