compression - dropping the last byte
Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance
- From: "Marc Gravell" <mgravell@xxxxxx>
- Date: Wed, 1 Feb 2006 11:56:33 -0000
It might just be my tired eyes, but I can't see what is wrong in the
following:
I have a byte array filled with random data (with a fixed seed to make
reproducable); I then compress and decompress this using the 2.0 GZip
compression classes - however, as you see, the last byte is getting dropped.
Any ideas? Almost certainly me having a blond moment ;-(
Marc
Overview:
GetByteData : returns an array of random data
Describe : details the first / last 5 values to the console (first: last,
second : penultimate, etc...)
CopyStream : reads / writes between two streams until the source is
exhausted (is there an wasier way of doing this?)
Compress / Decompress : use the GZip classes to manipulate the data
Results:
* 50000
0=170 49999=187
1=200 49998=183
2=81 49997=158
3=81 49996=216
4=66 49995=154
* 49999
0=170 49998=183
1=200 49997=158
2=81 49996=216
3=81 49995=154
4=66 49994=133
Code : watch for wrap
================
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication1 {
static class Program {
public static void Main() {
byte[] data = GetByteData();
Describe(data);
Console.WriteLine();
byte[] compressed = Compress(data);
byte[] decompressed = Decompress(compressed);
Describe(decompressed);
Console.ReadLine();
}
private static void Describe(byte[] data) {
Console.WriteLine("* " + data.Length.ToString());
int length = data.Length;
for (int i = 0; i < 5; i++) {
int otherEnd = length - 1 - i;
Console.WriteLine(string.Format("{0}={1}\t{2}={3}",i,data[i],otherEnd,data[otherEnd]));
}
}
private static byte[] Compress(byte[] data) {
using (Stream input = new MemoryStream(data))
using (MemoryStream output = new MemoryStream())
using (Stream zipper = new GZipStream(output,
CompressionMode.Compress, true)) {
CopyStream(input, zipper);
return output.ToArray();
}
}
private static byte[] Decompress(byte[] data) {
using (Stream input = new MemoryStream(data))
using (Stream unzipper = new GZipStream(input,
CompressionMode.Decompress, false))
using (MemoryStream output = new MemoryStream()) {
CopyStream(unzipper, output);
return output.ToArray();
}
}
private const int SEED = 141566, SIZE = 50000;
public static byte[] GetByteData() {
Random rand = new Random(SEED); // some seed to make
reproducable
byte[] data = new byte[SIZE];
rand.NextBytes(data);
return data;
}
public static long CopyStream(System.IO.Stream source,
System.IO.Stream destination) {
const int BUFFER_SIZE = 512;
long copied = 0;
byte[] buffer = new byte[BUFFER_SIZE];
int bytes;
while ((bytes = source.Read(buffer, 0, BUFFER_SIZE)) > 0) {
destination.Write(buffer, 0, bytes);
copied += bytes;
}
return copied;
}
}
}
.
Relevant Pages
- Re: Honest question (random compression)
... codec or bytecode codec? ... Because you can post garbage about random data compression that only ... they make up not to use 'real' data to test thier systems too. ... I would be one of the laymen who find trying to compress the file ... (comp.compression) - Re: Net Remoting and Performace
... You use the Binary Formatters, serialize the data, and compress the data. ... You're going to populate the object with your data with object accessor properties that represent your data int, string, double, etc. You'll use the attribute at the top of the class. ... You'll notice that it's doing it with a Memory Stream, ... (microsoft.public.dotnet.languages.csharp) - Re: RFD: comp.compression.random
... You act as if everyone that talks about compressing Random Data is talking about reducing 1 bit to 0. ... random source is an idiot." ... Or, a second true statement, if you like it better: "If a random source of the properties above can be compressed, then there is a second integer between 1 and 3". ... However, as the performance of a compressor increases, the output data of it looks considerably more and more like "random data", which means it is considerably harder to compress it. ... (comp.compression) - Re: Compress a subset of bits form
... What is the best algorithm to compress the information, ... Is this information stand alone or what. ... Third option if part of a stream use pure arithmetic ... My Crypto code ... (comp.compression) - Re: my response to some recent posts
... Let me make a prediction for you: As you haven't really implemented this, you will astonishingly discover that representing the prediction error will make things larger than the original stream, even though you thought that this error is actually small. ... That you cannot compress random data is not only a theorem, it is probably even the definition of random. ... You can steal the money by claiming that your idea will work, and probably you find even investors that are stupid enough to put money into that, but that wouldn't make your method working either. ... (comp.compression) |
|