Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Is GZipStream compression useless?

stream

  • Please log in to reply
10 replies to this topic

#1 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 30 August 2009 - 12:44 AM

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 documentation is 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 Thumbnails

  • screenshot rozmiaru plików.jpg

Attached Files


  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 30 August 2009 - 03:25 AM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 30 August 2009 - 04:22 AM

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.

  • 0

#4 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 30 August 2009 - 06:01 AM

Have you tried other types of files? What about a text file?
  • 0

#5 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 30 August 2009 - 08:22 AM

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 tutorial is 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 Thumbnails

  • bitmap text compressed, jpeg not.jpg

Attached Files


  • 0

#6 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 30 August 2009 - 08:42 AM

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
  • 0

#7 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 30 August 2009 - 09:00 AM

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.
  • 0

#8 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 31 August 2009 - 07:08 AM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#9 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 04 September 2009 - 02:17 AM

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 Thumbnails

  • Screenshot-8.png

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#10 relapse

relapse

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 323 posts

Posted 04 September 2009 - 04:57 AM

Figures. Leave it to microsoft to **** up such a simple task.
  • 0

#11 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 04 September 2009 - 05:19 AM

Ha ha, yeah. I agree with you all, what can I say. :)
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download