Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GZipStream: Compressed file bigger than original

Tags:

c#

gzipstream

I'm trying to use gzip streams in C# but it appears that the files after compression are bigger than before. It's occurs when I working with .avi and .mkv files. But if I working with .txt and .html compressed file smaller than original.

using (MemoryStream output = new MemoryStream(blockToCompress.Length))
{
     using (GZipStream cs = new GZipStream(output, CompressionMode.Compress))
     {
          cs.Write(blockToCompress, 0, blockToCompress.Length);
     }
}

I solved this problem by checking the Framework (from 3.5 to 4.0) without editing the code.

like image 670
Lev Alefirenko Avatar asked Dec 06 '25 11:12

Lev Alefirenko


1 Answers

Your code is correct. Unfortunately, your expectations are not.

Most video and audio files are already heavily compressed. They won't be able to be compressed any further with lossless compression like GZip. In fact, you might slightly increase file size - every file format, GZip included, carried with it some overhead/bookkeeping.

If you truly need to reduce their size, you'll need to use a lossy compression scheme that comprehends the video format. Basically, you'll be removing data, probably reducing apparent quality, in exchange for a smaller size.

Lossless compression schemes like GZip can reduce file size by 25-50%, if the source content is uncompressed. Files with lots of repeated text (HTML) or English text (general text files) usually compress much better.

like image 105
Michael Petrotta Avatar answered Dec 09 '25 00:12

Michael Petrotta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!