This is a migrated thread and some comments may be shown as answers.

Package.AddStream losing stream information

5 Answers 50 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Joe Sugden
Top achievements
Rank 1
Joe Sugden asked on 02 Jul 2013, 03:24 PM
Hi.

I have recently started using the ZipLibrary and I am facing an issue when adding streams to a ZipPackage.  I am trying to add two streams to the ZipPackage, one for a PowerPoint file and the other for a Excel file.  Once it is packaged, I want to write it to the OutputStream.  Before I add the streams to the package, their size is significantly larger and have data because when I export them as individual streams (outside of a zip) it works just fine.  Once added it looks like they lose all of their data and when it outputs, the PPTX and XLSX files are empty and corrupt respectively.

Here is my code below as well as the SendZipToClient snippet from another forum post, is there anything wrong with it or is it an issue from the ZipLibrary?

Additional Info:
.NET version: 4.5
Current Browser: FireFox 21.0
Telerik version for ASP.NET AJAX: 2013.1.417.45
Language: C#


private void StreamReports(Dictionary<int, MemoryStream> ReportStreams)
{
      string destPPTXFile = string.Format("{0}.pptx", txtPresentationName.Text);
      string destXLSXFile = string.Format("{0}.xlsx", txtPresentationName.Text);
 
      MemoryStream outputStream = new MemoryStream();
      ZipPackage zipPackage = ZipPackage.Create(outputStream);
 
      MemoryStream powerpointStream = ReportStreams[0];
      MemoryStream excelStream = ReportStreams[1];
 
      zipPackage.AddStream(powerpointStream, destPPTXFile);
      zipPackage.AddStream(excelStream, destXLSXFile);
 
      SendZipToClient(outputStream, zipPackage);
 
 }

private void SendZipToClient(MemoryStream memStream, ZipPackage Package)
        {
            string destFile = string.Format("{0}.zip", txtPresentationName.Text);
            Package.Close(false);
            memStream.Position = 0;
            if (memStream != null && memStream.Length > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment; filename=\"" + destFile);
                Response.ContentType = "application/zip";
                Response.BufferOutput = false;   // to prevent buffering
                byte[] buffer = new byte[1024];
                int bytesRead = 0;
                while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    Response.OutputStream.Write(buffer, 0, bytesRead);
                }
 
                Response.End();
            }
        }

5 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 05 Jul 2013, 08:12 AM
Hello Joe Sugden,

Based on your code I have assembled a small runnable sample that performs as expected on my side. Please run it on locally and let me know whether the issue replicates. Note that just for testing purposes I am using pretty simple almost empty files.

Regards,
Martin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Joe Sugden
Top achievements
Rank 1
answered on 08 Jul 2013, 01:48 PM
Hi Martin,

I made a mistake and did not reset my MemoryStream position back to 0 before adding it to the ZipPackage and now it works.

Thanks!
0
Tom
Top achievements
Rank 1
answered on 23 Sep 2013, 04:00 PM
I did the same thing (forgot to reset my memory stream position to 0). In my case I am creating JPEG images on the fly from another image format and adding them to the zip package. Here is a working snippet:

MemoryStream ms = new MemoryStream();
ZipPackage zp = ZipPackage.Create(ms);
foreach (string thisImg in imgList) {
    string fileNameInZip = thisImg + ".JPG";
    Bitmap bm = createImageBitmap(thisImg);  //my conversion routine
    MemoryStream memStream = new MemoryStream();
    bm.Save(memStream, ImageFormat.Jpeg);
    memStream.Position = 0;  //without this I was getting zero-length files in the zip
    zp.AddStream(memStream, fileNameInZip);
}

0
Fred
Top achievements
Rank 1
answered on 16 Jul 2014, 04:38 PM
Could you please include the code instead of just a dead link to a file?
0
Tom
Top achievements
Rank 1
answered on 18 Sep 2018, 12:42 PM

Well, it happened again. I needed help and found a forum thread that I posted to years ago. Now the ZipLibrary code has changed so we are using the ZipArchive class instead of the ZipPackage class. Here is an update to my previous post for the next time I come looking.

In this scenario, I am creating a bitmap on the fly and writing it to the zip archive. I started with this code:

1.using (ZipArchiveEntry fileEntry = za.CreateEntry(outputFilename)) {
2.  using (Stream fileStream = fileEntry.Open()) {
3.    Bitmap bm = createBitmap(); // my bitmap creation code
4.    bm.Save(fileStream, ImageFormat.Png);
5.    bm.Dispose();
6.  }
7.}

 

The above code worked great on my local machine, but not on the live server. Instead, I got the dreaded error:

A generic error occurred in GDI+.

 

Initially I thought the error was due to some permissions problem, but I'm sending the zip file out as an HTML response. (Frankly, permissions might still be [part of] the problem, but I don't know for sure.) I found a solution of saving the bitmap to an intermediate MemoryStream and adding those bytes to the ZipArchiveEntry like so:

1.using (ZipArchiveEntry zaeBitmap = za.CreateEntry(outputBrowseFilename)) {
2.  Stream s = zaeBitmap.Open();
3.  Bitmap bm = createBitmap(); // my create bitmap function
4.  MemoryStream msB = new MemoryStream();
5.  bm.Save(msB,ImageFormat.Png);
6.  byte[] b = msB.ToArray();
7.  s.Write(b,0,b.Length);
8.}

 

Credit to https://stackoverflow.com/questions/15862810/a-generic-error-occurred-in-gdi-in-bitmap-save-method

 

 

 

Tags
ZipLibrary
Asked by
Joe Sugden
Top achievements
Rank 1
Answers by
Martin
Telerik team
Joe Sugden
Top achievements
Rank 1
Tom
Top achievements
Rank 1
Fred
Top achievements
Rank 1
Share this question
or