No, its nothing to do with the "Byte order mark".
I wrote the following as a test. Simply take a file, read it, compress it, decompress it, then compare the data. Fails on EVERY file. The lengths are off, usually by one or two, and even if they were on, the data is different toward the end.
Compression.cs
Code:
namespace Test
{
class Compression
{
public Compression()
{
}
public static byte[] Compress(byte[] data)
{
MemoryStream ms = new MemoryStream();
DeflateStream gz = new DeflateStream(ms, CompressionMode.Compress);
gz.Write(data, 0, data.Length);
return ms.ToArray();
}
public static byte[] Decompress(byte[] data)
{
MemoryStream ms = new MemoryStream(data);
DeflateStream gz = new DeflateStream(ms, CompressionMode.Decompress);
int length = StreamLength(gz);
ms = new MemoryStream(data);
gz = new DeflateStream(ms, CompressionMode.Decompress);
byte[] dat = new byte[length];
gz.Read(dat, 0, length);
return dat;
}
private static int StreamLength(DeflateStream stream)
{
int i = 0;
byte[] temp = new byte[1000];
while (true)
{
int read = stream.Read(temp, 0, 1000);
i += read;
if (read == 0)
return i;
}
}
}
}
Program.cs
Code:
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Reading File...");
byte[] file = File.ReadAllBytes(@"file"); //TODO: Replace me with a file you have!
Console.WriteLine("Compressing...");
byte[] compressed = Compression.Compress(file);
Console.WriteLine("Decompressing...");
byte[] decompressed = Compression.Decompress(compressed);
Console.WriteLine("Complete! Calculating...");
if (file.Length != decompressed.Length)
{
Console.WriteLine("This is broke!");
Console.WriteLine("File Length: {0}", file.Length);
Console.WriteLine("Decompressed Length: {0}", decompressed.Length);
Console.ReadLine();
return;
}
int incorrect_counter = 0;
for (int i = 0; i < file.Length; i++)
{
if (file[i] != decompressed[i])
{
incorrect_counter++;
}
}
if (incorrect_counter > 0)
{
Console.WriteLine("This is broke!");
Console.WriteLine("{0} bytes are messed up.", incorrect_counter);
Console.ReadLine();
return;
}
Console.WriteLine("Yeah!");
Console.ReadLine();
}
}
}
I ommited usings, but you need
Code:
using System;
using System.IO;
using System.IO.Compression;
Output on a random 9MB file...
Code:
Reading File...
Compressing...
Decompressing...
Complete! Calculating...
This is broke!
File Length: 10087472
Decompressed Length: 10087471
I'm usually not one to assume there is a bug... but this is about as simple as I can get it before manually compressing my computer into a wall. The sad thing is the sample code in MSDN works perfectly. I'm so very confused.
Edit: Never mind I posted here. A gz.Close() was all I needed. Please Never speak of this again.