0

I can't comprehend. It looks correct.

Windows gets me

21008 80373 0.2613813
80372 80372 1

Deflate on .NET isn't good (it shouldnt be 26%) but more importantly the decompressed size is 1 off. Looking at the file (its html) its missing the last >

mono (2.10.5) on ubuntu gets me

4096 80373 0.05096239
12297 12297 1

-edit- 5% is incorrect. Its 21% when done properly -end edit- 5% of the original seems proper. But the decompressed size is way to small. Looking at the file it looks like the correct html data. It just isn't decompressing it all.

Whats wrong with the code?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace DeflateTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string fn1 = args.First();
            var fn2 = fn1 + ".out";
            var fn3 = fn1 + ".outorig";
            {
                using (var m = new MemoryStream())
                using (var d = new DeflateStream(m, CompressionMode.Compress, true))
                using (var f = File.OpenRead(fn1))
                {
                    var filelen = 0;
                    var b = new Byte[1024 * 4];
                    while (true)
                    {
                        var len = f.Read(b, 0, b.Length);
                        if (len <= 0)
                            break;
                        filelen += len;
                        d.Write(b, 0, len);
                    }
                    d.Flush();
                    m.Flush();
                    var m_arr = m.ToArray();
                    using (var ff = File.Create(fn2))
                    {
                        ff.Write(m_arr, 0, m_arr.Length);
                    }
                    Console.WriteLine(@"{0} {1} {2}", m_arr.Length, filelen, (float)m_arr.Length / filelen);
                }
            }
            {
                using (var f = File.OpenRead(fn2)) 
                using (var d = new DeflateStream(f, CompressionMode.Decompress, true))
                using (var m = new MemoryStream())
                {
                    var filelen = 0;
                    var b = new Byte[1024 * 4];
                    while (true)
                    {
                        var len = d.Read(b, 0, b.Length);
                        if (len <= 0)
                            break;
                        filelen += len;
                        m.Write(b, 0, len);
                    }
                    m.Flush();
                    var m_arr = m.ToArray();
                    using (var ff = File.Create(fn3)) { ff.Write(m_arr, 0, m_arr.Length); }
                    Console.WriteLine(@"{0} {1} {2}", m_arr.Length, filelen, (float)m_arr.Length / filelen);
                }
            }
        }
    }
}

1 Answers1

3

d.Flush(); is incorrect. The deflate stream needs to be closed. d.Close(); solves it.

  • 1
    Indeed. For performance and protocol reasons, the final buffer cannot be flushed until the stream is finally closing. The compression stream must be fully closed before you access the memory-stream's contents. – Marc Gravell Jul 14 '12 at 18:28