A file gets compressed when a program uses algorithms to find ways to represent the data using less space.
File compression is essentially the process of rewriting the information contained within a file so that it occupies less storage space. This is not magic; it's a calculated process carried out by specialized software.
The Role of Programs and Algorithms
Compression is performed by a program that uses functions or an algorithm to effectively discover how to reduce the size of the data. These programs analyze the data in a file to identify patterns, redundancies, or statistical properties that can be exploited for size reduction.
Think of an algorithm as a set of instructions or rules. Compression algorithms are designed specifically to find efficient ways to encode information. For example:
- Finding repeating sequences of data.
- Identifying frequently occurring symbols or patterns.
- Analyzing the probability of certain data appearing after others.
Techniques Used in Compression
The specific techniques vary depending on the algorithm, but the core idea is always about representing the original data more compactly. A common approach involves creating a kind of shorthand.
As the reference mentions, an algorithm might represent a string of bits with a smaller string of bits by using a 'reference dictionary' for conversion between them. This is a key concept in many compression methods. The program builds a dictionary (or table) of common data sequences it finds in the file. When it encounters a sequence it has already added to the dictionary, instead of writing the sequence itself, it writes a short code or reference that points to that sequence in the dictionary. During decompression, the program reads the code, looks it up in the dictionary (which must be reconstructed or stored), and replaces it with the original, longer sequence.
Other techniques might include:
- Run-Length Encoding (RLE): Replacing sequences of identical data values with a single value and a count (e.g., "AAAAA" becomes "A5").
- Huffman Coding: Assigning shorter codes to frequently occurring data elements and longer codes to less frequent ones.
By systematically applying these algorithmic techniques to identify and replace repetitive or predictable data with shorter references or codes, the overall size of the file is reduced. The goal is to do this without losing any of the original information (lossless compression) or with minimal, acceptable loss (lossy compression, often used for images and audio).