Data compression fundamentally relies on finding and eliminating redundancy within a file. It is broadly categorized into two types: lossless and lossy. Lossless compression allows the exact original data to be perfectly reconstructed. It is mandatory for text, executables, and spreadsheets, where changing a single byte corrupts the file.
Lossy compression, used primarily for audio, video, and images (like MP3 or JPEG), permanently discards data that human senses are unlikely to notice. A lossy algorithm might average out slight variations in the color of a blue sky. You can never get the discarded data back, which is why lossy files are dramatically smaller.
How lossless algorithms (ZIP/DEFLATE) work
Most standard lossless compression tools (ZIP, GZIP, PNG) use the DEFLATE algorithm, which combines two techniques: LZ77 dictionary compression and Huffman coding. LZ77 scans the file for repeating sequences of bytes. If it finds the word 'compression' multiple times, it stores it once in a dictionary and replaces subsequent instances with a tiny pointer to the first occurrence.
After LZ77 reduces the file via pointers, Huffman coding takes over. It analyzes the frequency of all remaining symbols. It assigns the shortest binary codes (like 1 bit) to the most common symbols, and longer codes to rare symbols. Together, these two steps can shrink text files by up to 80%.
The limit of entropy: Why you can't ZIP a JPEG
Users are often confused when they try to put a batch of JPEG photos or MP4 videos into a ZIP file and discover the resulting archive is almost the exact same size. This happens because JPEGs and MP4s are already heavily compressed data.
Compression algorithms rely on finding patterns. Once a file is highly compressed, its data appears completely random; it has reached maximum 'entropy'. There are no repeating byte sequences left for the LZ77 algorithm to replace, and no predictable frequencies for Huffman coding to optimize. Attempting to compress already compressed data is mathematically futile.