OutOfMemoryException cuando se envía un archivo grande de 500 MB con FileStream ASPNET

Estoy usando Filestream para leer archivos grandes (> 500 MB) y obtengo la OutOfMemoryException.

Yo uso Asp.net, .net 3.5, win2003, iis 6.0

Quiero esto en mi aplicación:

Leer DATOS de Oracle

Descomprima el archivo usando FileStream y BZip2

Lea el archivo sin comprimir y envíelo a la página asp.net para descargarlo.

¡Cuando leo el archivo del disco, falla! y salir de memoria ...

. Mi código es:

using (var fs3 = new FileStream(filePath2, FileMode.Open, FileAccess.Read)) 
        { 
          byte[] b2 = ReadFully(fs3, 1024); 
        } 

 // http://www.yoda.arachsys.com/csharp/readbinary.html
 public static byte[] ReadFully(Stream stream, int initialLength) 
  { 
    // If we've been passed an unhelpful initial length, just 
    // use 32K. 
    if (initialLength < 1) 
    { 
      initialLength = 32768; 
    } 

    byte[] buffer = new byte[initialLength]; 
    int read = 0; 

    int chunk; 
    while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0) 
    { 
      read += chunk; 

      // If we've reached the end of our buffer, check to see if there's 
      // any more information 
      if (read == buffer.Length) 
      { 
        int nextByte = stream.ReadByte(); 

        // End of stream? If so, we're done 
        if (nextByte == -1) 
        { 
          return buffer; 
        } 

        // Nope. Resize the buffer, put in the byte we've just 
        // read, and continue 
        byte[] newBuffer = new byte[buffer.Length * 2]; 
        Array.Copy(buffer, newBuffer, buffer.Length); 
        newBuffer[read] = (byte)nextByte; 
        buffer = newBuffer; 
        read++; 
      } 
    } 
    // Buffer is now too big. Shrink it. 
    byte[] ret = new byte[read]; 
    Array.Copy(buffer, ret, read); 
    return ret; 
  } 

Ahora, especifico mejor mi problema.

Descomprima el archivo usando FileStream y BZip2 está bien, todo está bien.

El problema es el siguiente:

Lea el archivo grande y gordo en el disco (> 500 MB) en el byte [] y envíe bytes a Response (asp.net) para descargarlo.

Cuando uso

http://www.yoda.arachsys.com/csharp/readbinary.html

public static byte[] ReadFully

Me sale el error: OutOfMemoryException ...

Si es mejor BufferedStream que Stream (FileStream, MemoryStream, ...) ??

Usando BufferedStream, ¿puedo leer un archivo grande de 700 MB? (cualquier fuente de código de muestra usando BufferedStream para descargar el archivo grande)

Creo que esta es la pregunta: no "¿cómo leer un archivo de 500 MB en la memoria?" , Pero "¿cómo enviar un archivo grande a la secuencia de respuesta ASPNET?"

Encontré este código por Cheeso:

using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))  
{  
   Response.BufferOutput= false;   // to prevent buffering 
   byte[] buffer = new byte[1024]; 
   int bytesRead = 0; 
   while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)  
   { 
       Response.OutputStream.Write(buffer, 0, bytesRead); 
   } 
}

¿Es un buen código? ¿Alguna mejora para un alto rendimiento?

Un colega dime, usa

Response.TransmitFile(filePath);

Ahora, otra pregunta, ¿mejor TransmitFile o código de Cheeso?

Hace muchos años, en la revista msdn aparece un gran artículo al respecto, pero no puedo accederhttp://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/,

Actualizar: Puedes acceder usandowebarchive en el enlace:https://web.archive.org/web/20070627063111/http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/

¿Alguna sugerencia, comentario, código fuente de muestra?

Respuestas a la pregunta(3)

Su respuesta a la pregunta