SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

VS2003 ASP.NET 1.1 Optimized File(s) zipping & Streaming

Last post 07-29-2008 2:22 PM by Rolf. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 01-18-2007 10:01 PM

    • vad_77
    • Not Ranked
    • Joined on 01-18-2007
    • Posts 1

    VS2003 ASP.NET 1.1 Optimized File(s) zipping & Streaming

    Hi everybody,

     Here's my challange:

    When user clicks link "download all images" on web page, I would like to

    -----------------------------------------------------------------------------------

    1. list all images in particular folder (easy)

    2. create ZIP file with headers only and send them back immediatly into Response stream.

    3. zip 1 file at-a-time and write it to Response stream (for each file in the list)

    4. write ZIP footer into Response stream. DONE.

    -----------------------------------------------------------------------------------

    NOTE: compression is not a factor - I'm just using standard ZIP compr. at level 0, so this acts as bundling mechanism for all files to be downloaded in 1 download instead of n-number of times.

    The number of images in folder to zip will change so Zipping has to be dynamicly done every time.

    Image files are 70K on average, but there could be a lot of them 1000+, so creating separate zip file is NOT AN OPTION. :/ 

     I have not found how to do it in IC#ZipLib. Any Ideas? Thanks. :)

     
    -Vad
     

  • 07-29-2008 2:22 PM In reply to

    • Rolf
    • Not Ranked
    • Joined on 07-29-2008
    • Posts 1

    Re: VS2003 ASP.NET 1.1 Optimized File(s) zipping & Streaming

    Hi Vad.

     

    Did you ever find a solution for you problem?

     

    I have some source code that will do something like this.

      ----------------------------

    private ZipOutputStream zipOutput;

    private int bufferLength = 100000;

    public void ProcessRequest(HttpContext context)
            {
                try
                {
                    string[ files = new string[ {@"C:\testsan2\0001227966\AudioAssets\0001227966_Full_008.mp3",@"C:\testsan2\0001227966\AudioAssets\0001227966_Full_007.mp3"};
               
                    string mimeType = "application/zip";
                    string deliveryName = "test2.zip";
                    log.Debug("Open Zip stream");

                    zipOutput = new ZipOutputStream(context.Response.OutputStream);
                    zipOutput.SetLevel(0);

                    context.Response.BufferOutput = false;
                    context.Response.ContentType = mimeType;
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + deliveryName + "\"");
                    context.Response.Flush();
                    log.Debug("Respond headers");
                    foreach(string file in files)
                    {
                        FileStream fs = File.OpenRead(file);
                        byte[ buffer = new byte[bufferLength];
                        int bytesRead;
                        //
                        // init entries
                        //
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));   
                        entry.DateTime = DateTime.Now;
                        entry.Size = fs.Length;
                        zipOutput.PutNextEntry(entry);
                        log.Debug("Add Zip entry for "+Path.GetFileName(file));
                        //
                        // Now send data
                        //
                        fs.Seek(0,SeekOrigin.Begin);
                        do
                        {
                            bytesRead = fs.Read(buffer, 0, buffer.Length);   
                            zipOutput.Write(buffer, 0, bytesRead);

                        } while (bytesRead > 0);
                        //
                        // close
                        //
                        fs.Close();
                        log.Debug("Close File");
                    }

                    zipOutput.Finish();
                    zipOutput.Close();
                    log.Debug("Close Zip");
                }
                catch (Exception e)
                {
                    log.Error(e.Message, e);
                    throw ;
                }
            }

     ----------------------------

    This seems to work fine and keep the memory usage nice and low. Unfortunately this solution has some drawbacks.

    1. It is slow (And takes up a lot of processing when trying to it for 100 customers at a time). Seems that you can’t send the content without any data deflation. I tried "ZipEntry entry = new ZipEntry(Path.GetFileName(file), 0, ZipConstants.VersionMadeBy, CompressionMethod.Stored); " but it causes an exception.

    2. The size of the file cannot be passed in the header so the person downloading will not be able to see a nice little progress bar.

     

    I'm still trying to look around if any of there is any other way around this. I’m sure that it has to be possible to generate zip files on the fly without needing any "compression"

     

    Does anyone have any suggestions?

     
Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.