Jump to content

Is GZipStream compression useless?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
I made a working prototype for Gzip compression and decompression. The output is valid, as my decompressed wallpaper still looks awesome, but the compression ratio is staggering ...150%. Is this class a joke? If anyone made this class to actually *compress* data then please share with me. :)

Below is the folder with my input image (1MB), compressed gzip (1.5MB) and decompressed image.

Arek,

http://forum.codecal...=1&d=1251621635

Here is the code for compression method, and decompression method. I used the standard GZipStream class. MSDN documentationis here. Full solution is also posted.

FileStream source = new FileStream("tapeta.jpg", FileMode.Open);

FileStream destinationG = new FileStream("compressed.gzip", FileMode.Create);

FileStream destinationD = new FileStream("compressed.deflate", FileMode.Create);

GZipStream gzip = new GZipStream(destinationG, CompressionMode.Compress);

DeflateStream deflate = new DeflateStream(destinationD, CompressionMode.Compress);


byte[] buffer = new byte[source.Length];

source.Read(buffer, 0, buffer.Length);

gzip.Write(buffer, 0, buffer.Length);

deflate.Write(buffer, 0, buffer.Length);


source.Close();

destinationG.Close();

destinationD.Close();

//gzip.Close();

//deflate.Close();


MessageBox.Show("File has been compressed. Will open the folder.");

Process.Start(Application.StartupPath);


FileStream original = new FileStream("tapeta.jpg", FileMode.Open);

FileStream sourceG = new FileStream("compressed.gzip", FileMode.Open);

FileStream sourceD = new FileStream("compressed.deflate", FileMode.Open);

FileStream decompG = new FileStream("decompressed gzip.jpg", FileMode.Create);

FileStream decompD = new FileStream("decompressed deflate.jpg", FileMode.Create);


GZipStream gzip = new GZipStream(sourceG, CompressionMode.Decompress);

DeflateStream deflate = new DeflateStream(sourceD, CompressionMode.Decompress);


byte[] buffer = new byte[original.Length];

gzip.Read(buffer, 0, buffer.Length);

decompG.Write(buffer, 0, buffer.Length);


buffer = new byte[original.Length];

deflate.Read(buffer, 0, buffer.Length);

decompD.Write(buffer, 0, buffer.Length);


sourceG.Close();

sourceD.Close();

decompG.Close();

decompD.Close();

original.Close();


MessageBox.Show("Bother were decompressed. Will open the folder.");

Process.Start(Application.StartupPath);

Attached Files



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
zipping a .jpg (which is already highly compressed) is asking for that to happen. Most compression programs keep an eye out for things like that and will store a file rather than try to compress it if "compressing" will bloat.

What happens is you get 1-3% compression, but then have to store a massive data dictionary that's larger than the space savings.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
Well, your point is valid, of course. But I would expect it to be certainly around the original size. A ratio of 150% is just a joke. I just took that JPEG and put it into a ZIP and a RAR. They both have around the original size. Nowehere near 150%. :mellow:

And that dictionary space explanation... it sounds very convincing. Let us dig deeper into this subject and look for some solution.

Edited by ArekBulski, 30 August 2009 - 06:08 AM.


#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Have you tried other types of files? What about a text file?

#5
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
A nice suggestion. I just took that JPEG and added a BITMAP and a TEXT file. Amazingly those two new formats got compressed pretty well, into 80% and 50% sizes respectiely. I still wonder, why the jpeg was not "put as raw" but compressed into a bloated archive. I think Winged is right about the explanation.

I will look forward to any more possibilities to get more out of it. I was thinking about making Gaduzila 2.0, an instant messanger, and some compression technology would be very useful.

Jordan- your tutorialis very nice. I think I just expected GZipStream to work better than bloating what it cannot compress. Maybe I overdid it with asking you for some proof that it is working. I am sorry. If you feel offended by my comments then I offer my appologies. :blushing:

http://forum.codecal...=1&d=1251648117

I refactored the code to support multiple files. If anyone is interested in playing with Gzip compression, then go ahead. Use it as you see it fit. Here is the refactored code.

private void button1_Click(object sender, EventArgs e)

{

    BothCompressDecompress("tapeta.jpg", "compressed tapeta.gzip", "unpacked tapeta jpeg.jpg");

    BothCompressDecompress("my desktop bmp.bmp", "compressed desktop bitmap.gzip", "unpacked desktop bitmap.bmp");

    BothCompressDecompress("kopia tekstu postu.txt", "compressed text.gzip", "unpacked text.txt");


    MessageBox.Show("Files has been packed and unpacked. Will open the folder.");

    Process.Start(Application.StartupPath);

}


private void BothCompressDecompress(string original, string destinationGzip, string unpackedFile)

{

    CompressFile(original, destinationGzip);

    DecompressFile(original, destinationGzip, unpackedFile);

}

Attached Files



#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You didn't offend me and I am glad you shared your knowledge. I learned along with you, I didn't know that about jpegs either.
Posted via CodeCall Mobile

#7
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
I came to an idea to create a gzip copy, compare with original file, and then pick the file that is smaller. A small workaround for the problem.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If you look in a zip or rar with something other than Windows' "Compressed Folder" tool, it will list whether it is compressed or not. No compression = no dictionary = almost no bloat.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
GZip isn't a joke, but that built-in Microsoft compression algorithm is.

Here's an example of a GZip program that actually knows what it's doing:

Certainly not impressive compression, but nonetheless a 446 byte savings.

Attached Files


Wow I changed my sig!

#10
relapse

relapse

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 476 posts
Figures. Leave it to microsoft to **** up such a simple task.

#11
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
Ha ha, yeah. I agree with you all, what can I say. :)