title: Most Space-Efficient Way to Store a Byte Array in a Database Table - ASP.NET
body: <p>Right now we have a database table (SQL Server 2008 R2) that stores an uploaded file (PDF, DOC, TXT, etc.) in an image type column. A user uploads this file from an ASP.NET application. My project is to get a handle on the size at which this table is growing, and I've come up with a couple of questions along the way.</p>
  
  <ol>
  <li><p>On the database side, I've discovered the image column type is supposedly somewhat depreciated? Will I gain any benefits to switching over to varbinary(max), or should I say varbinary(5767168) because that is my file size cap, or might as well I just let it stay as an image type as far as space-efficiency is concerned?</p></li>
  <li><p>On the application side, I want to compress the byte array. Microsoft's built in GZip sometimes made the file bigger instead of smaller. I switched over to SharpZipLib, which is much better, but I still occasionally run into the same problem. Is there a way to find out the average file compression savings before I implement it on a wide scale? I'm having a hard time finding out what the underlying algorithm is that they use.</p></li>
  <li><p>Would it be worth writing a Huffman code algorithm of my own, or will that present the same problem where there is occasionally a larger compressed file than original file?</p></li>
  </ol>
  
  <p>For reference, in case it matters, here's the code in my app:</p>
  
  <pre><code>    using ICSharpCode.SharpZipLib.GZip;
  
      private static byte[] Compress(byte[] data)
      {
          MemoryStream output = new MemoryStream();
  
          using (GZipOutputStream gzip = new GZipOutputStream(output))
          {
              gzip.IsStreamOwner = false;
              gzip.Write(data, 0, data.Length);
              gzip.Close();
          }
          return output.ToArray();
      }
  
      private static byte[] Decompress(byte[] data)
      {
          MemoryStream output = new MemoryStream();
          MemoryStream input = new MemoryStream();
          input.Write(data, 0, data.Length);
          input.Position = 0;
  
          using (GZipInputStream gzip = new GZipInputStream(input))
          {
              byte[] buff = new byte[64];
              int read = gzip.Read(buff, 0, buff.Length);
  
              while (read &gt; 0)
              {
                  output.Write(buff, 0, read);
                  read = gzip.Read(buff, 0, buff.Length);
              }
  
              gzip.Close();
          }
          return output.ToArray();
      }
  </code></pre>
  
  <p>Thanks in advance for any help. :)</p>