Package.AddStream losing stream information

5 posts, 0 answers
  1. Joe Sugden
    Joe Sugden avatar
    52 posts
    Member since:
    Feb 2010

    Posted 02 Jul 2013 Link to this post

    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();
                }
            }
  2. Martin
    Admin
    Martin avatar
    811 posts

    Posted 05 Jul 2013 Link to this post

    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.
  3. Joe Sugden
    Joe Sugden avatar
    52 posts
    Member since:
    Feb 2010

    Posted 08 Jul 2013 Link to this post

    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!
  4. Tom
    Tom avatar
    44 posts
    Member since:
    Oct 2010

    Posted 23 Sep 2013 Link to this post

    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);
    }

  5. Fred
    Fred avatar
    2 posts
    Member since:
    Dec 2010

    Posted 16 Jul 2014 in reply to Martin Link to this post

    Could you please include the code instead of just a dead link to a file?
Back to Top