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

2

u/purenitrogen Jun 18 '12

I like the simplicity of this explanation, so can I ask, what if the "sentence" contains "1".

"I am 1 year old"

If we say 1 = year, "I am 1 1 old"

Now our algorithm reads back "I am year year old"

How is this avoided?

1

u/LoveGentleman Jun 18 '12

The example of 1,2,3 as keys are just that examples, you could put another special marker to detonate the compression algorithms markers from the text, for example using a byte that isnt used in text as prefix to 1,2,3 in a dictionary of keywords in the file header.

1

u/mrsix Jun 18 '12

Generally when you compress a file you do compress the entire file, or if you're only going to compress parts of it you would make some kind of special header-marker that identifies the compressed data range.

In his algorithm he should either create a table that includes all the words so that there's no confusion - or add some kind of special header like say "I am 1 1 old" where the italics is the special marking of compressed data.