r/askscience • • Jun 17 '12

Computing How does file compression work?

(like with WinRAR)

I don't really understand how a 4GB file can be compressed down into less than a gigabyte. If it could be compressed that small, why do we bother with large file sizes in the first place? Why isn't compression pushed more often?

414 Upvotes

146 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Jun 17 '12

[deleted]

2

u/Nate1492 Jun 18 '12

That fails to meet the criteria of not already being compressed... Which I briefly covered.

1

u/arienh4 Jun 18 '12

Yeah, I know. What I was trying to say is that in real world situations, most files are already compressed. There are hardly any documents that are not.

1

u/Stereo_Panic Jun 18 '12

Image and video files fail this test pretty quickly. That is, unless you only count uncompressed bitmaps, I suppose.

The person you're replying to said "As long as there has bee no compression ran already." JPGs, MPGs, etc are using compression. So... yeah, "unless you only count uncompressed bitmaps' is exactly right.

6

u/_NW_ Jun 18 '12

Randomness is almost always impossible to compress. Try compressing a file of random bits. Most of the time you will find that it is an uncompressed file that will not compress.

1

u/Stereo_Panic Jun 18 '12

Randomness is difficult to compress but a "random file" is not full of "randomness".

4

u/_NW_ Jun 18 '12

It depends on the file structure. You didn't really read all of my post, it would seem. A file of random bits is full of randomness. I didn't say a text file of random numbers or a file full of INT32 random numbers.

1

u/Stereo_Panic Jun 18 '12

Okay but... from a purely practical standpoint, how often will you come across a file of random bits? Even if you did, as the file grew in size there would be more and more "phrases" that would be compressible.

1

u/_NW_ Jun 18 '12

Every email that comes in to my server, i send through md5sum and append to a file. I have a program to convert that to pure random bits. It is never compressable. I'm just saying that randomness cannot be compressed.

2

u/arienh4 Jun 18 '12

Which is why there is a second line in my comment as well.

1

u/Stereo_Panic Jun 18 '12

That would be a bet you'd lose.

Few exes are compressed, the main exception being for exes that are installers. The overhead on decompressing them at run-time is too high. DLLs are not compressed. When they are compressed they're usually stored like "EXAMPLE.DL_". Same reason as exes.

2

u/arienh4 Jun 18 '12

Two notes.

  1. I explicitly mentioned documents in my comment. An executable isn't a document.

  2. Actually, lots of executables are compressed. UPX does in-place decompression at ~200MB/s on average machines, which is hardly any overhead at all.

1

u/Stereo_Panic Jun 18 '12
  1. Okay you got me there. Touche!

  2. I didn't know about UPX, but it appears to be for package installers and not applications. I explicitly mentioned in my comment about that being the main exception to the rule.

2

u/arienh4 Jun 18 '12

No, UPX is the Ultimate Packer for eXecutables. It is applied to a lot of (especially open-source) software, not just installers.

Most installers use a more efficient algorithm first, like BZip2.