Simple Zip File from Strings and delivering on the Response.OutputStream

Jul 20, 2009 at 2:48 PM
Edited Jul 20, 2009 at 2:50 PM

Here's my code

        private void sendToClient(Dictionary<string, string> reportDic)
        {
            String ReadmeText = "some text";

            Response.Clear();
            Response.BufferOutput = false;
            Response.ContentType = "application/zip";
            Response.AddHeader("Content-Disposition", "attachment; filename=filename.zip");
            Response.ContentEncoding = Encoding.Default;
            Response.Charset = "";

            using (ZipFile zip = new ZipFile())
            {
                zip.AddEntry("Readme.txt", "",ReadmeText,Encoding.Default);
                zip.Save(Response.OutputStream);
            }
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

 

this is almost exactly the same as the example.  What I get is a prompt to download a zip file(expected), get a zip file with "readme.txt" inside (expected), but no text inside of the readme.txt document. 

I've tried this with a stream, a string and a byte[].  all the same results.  I also get a bad zip header warning from IZARC, I'm not sure if it's all realted.

My first attemept with DotNetZip was this

 

       
        private void sendToClient(Dictionary<string, string> reportDic)
        {
            Response.BufferOutput = false;
            Response.ContentType = "application/zip";
            Response.AddHeader("content-dispostion", "filename=text.zip");
            Response.ContentEncoding = Encoding.Default;
            Response.Charset = "";
            using (ZipFile zip = new ZipFile())
            {
                foreach (string key in reportDic.Keys)
                {
                    zip.AddEntry(key, "", reportDic[key]);
                }
                zip.Save(Response.OutputStream);
            }
            Response.End();

 

I have 3 files that I need to deliever in one response, they aren't big, but I only have one response and 3 documents. But what I get with this one is one copy of the aspx page zipped up, very strange.

 

Can someone with a bit more experience with the API please take a look and let me know why I'm having these issues, or at least point me in the right direction.   Thanks

Jim

Coordinator
Jul 20, 2009 at 6:27 PM

I don't know what the problem could be.  What you want to do should just work.

What version of DotNetZip are you using?  There was a problem running in ASP.NET with medium trust on.  This may be your trouble.  The problem is fixed in v1.8.4.8. 

 

Jul 20, 2009 at 6:48 PM

I'll have to check the version of the dll I have when I get back to work (I'm in Europe), then I'll post back.  Thanks for your follow up. 

 

Do both code examples seem like they should work?   Would the incorrect version of the dll give me a header error? 

 

Thanks again.

 

Jim

Coordinator
Jul 20, 2009 at 7:22 PM

Yes, the code examples look reasonable.

If you are using the DLL that has the bug related to medium trust, then yes, you would get a broken zip file when saving a zip file from within an ASP.NET site that has medium trust.  Opening the resulting broken zip file in different tools or with different libraries will yield different results.  I'm not sure what IZArc would say.  But there would be a zip file, and it would be unreadable and broken, and I'm sure IZArc would complain. 

Not sure this is the same as the problem you are experiencing, though, because you described two different problems.  On one hand you seemed to say you get a valid zip file with a single entry that is empty.  On the other hand you said you get a zip file with a broken header.  If the situation is that you open it in IZArc and it complains about a header and then cannot unzip the entry, then it sounds like it could be this bug.  Either way it is worth testing out.  Get v1.8.4.8. 

If you still get the same problems with v1.8.4.8, then I'll open a workitem and I'd like you to upload a sample (broken) zip file to the workitem. 

Jul 21, 2009 at 7:54 AM

Cheeso, I've updated the DLL I was using 1.8.1.8 or something. 

 

I had the same issue in my orignal code.  So I created a new project and added just the first example of my code.  It ran fine, made the zip, no bad zip header warnings.  Now I'm really confused. 

Both projects are pointing at the same dll, so it's not different versions.  I referenced all the same dlls in each project. I commented out all the code in the orignal project class so that all I have is the call to my method and the method. 

Coordinator
Jul 21, 2009 at 1:01 PM

sorry, I don't get it.  I don't understand what you're confused about.  It seems to be working.  You said it ran fine, made the zip, etc etc.  what's the problem now?

Jul 21, 2009 at 1:36 PM

Original project -this code produces a zip file with a "Bad Zip Header" from my unzip utility IZarc, but I'm still able to open it.  I get a document named Readme.txt which has nothing in it.  I would expect it to have "some text"

        private void sendToClient(Dictionary<string, string> reportDic)
        {
            String ReadmeText = "some text";

            Response.Clear();
            Response.BufferOutput = false;
            Response.ContentType = "application/zip";
            Response.AddHeader("Content-Disposition", "attachment; filename=filename.zip");
            Response.ContentEncoding = Encoding.Default;
            Response.Charset = "";

            using (ZipFile zip = new ZipFile())
            {
                zip.AddEntry("Readme.txt", "",ReadmeText,Encoding.Default);
                zip.Save(Response.OutputStream);
            }
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

This same code pasted into a brand new project, with nothing else added produces different results.  I don't get an error and I get a file named "Readme.txt" with the text "some text" in it.  This is exactly what I want to happen. (I was pointing at the exact same dll from the orginal project)

in the new project I started adding references to all the dlls I have in the orginal project 1 by 1, I got no error message from the unzip utility and everything still worked perfectly.  Next, I went the other way.  I made a new page in my orginal project not within the master page, with no ajax calendar controls or tab controls, added the code and received the same error as before, with no text inside my document.

I don't have a problem delivering these files one by one via the Response, only the zip, and only in this one project.  It doesn't seem as though the ajax controls, masterpage or the other dlls could be the issue, but I'm not totally convinced.

Coordinator
Jul 21, 2009 at 4:16 PM
Edited Jul 21, 2009 at 6:16 PM

I think you have a reference to an older Ionic.Zip.dll in the original project.

There was a bug in DotNetZip that produced the behavior you originally reported.  I fixed that bug.  If you are still seeing the behavior, then I think you have a reference to an older evrsion of the library.  There's a way to tell - on the ZipFile class there is a static property, LibraryVersion.  You can print that out at runtime to verify the version of the library you are actually using, as opposed to the one you think you are using.  You should have v1.8.4.8. 

Jul 21, 2009 at 4:49 PM

I really appreciate your paitence. I will look at the LibraryVersion property.  The way I checked the version is by looking at the propeties in the VS properties window.  I'll have to look tomorrow when I get back to work.  At anyrate, both projects were pointing at the same dll, I only have one bin file where the dlls are kept, for my main project and just out of lazyness I pointed my test project at the same dll. 

Cheeso, I'm very close to being able to reproduce this. 

 

On a side note.  my second example of code in my OP...you know what that returns on the Response?  The current aspx page that is running.  You get a prompt and the web page, really strange.