Compression
- Compression reduces the size of files to save storage space and speed up data transmission.
- There are two main types of compression: lossless and lossy.
- Lossless compression is used when it is important to retain the original data.
- Lossy compression is used when some data loss is acceptable in exchange for greater compression.
Lossless Compression
Lossless compression
Lossless compression runs an algorithm to compress the data, the algorithm can then be reversed to restore the data. This ensures that the original data can be perfectly reconstructed from the compressed version.
How Lossless Compression Works
Lossless compression algorithms find patterns and redundancies in the data and encode them more efficiently.
Example- Run-Length Encoding (RLE): Replaces repeated characters with a single character and a count.
- i.e, "AAAAABBBCC" becomes "5A3B2C."
- Huffman Coding: Assigns shorter codes to more frequent characters and longer codes to less frequent characters.
- Lempel-Ziv-Welch (LZW): Builds a dictionary of patterns in the data and replaces repeated patterns with shorter codes.
Advantages of Lossless Compression
- No Data Loss: The original file can be perfectly reconstructed.
- Suitable for Critical Data: Used for text, software, and other files where data integrity is essential.
Disadvantages of Lossless Compression
Lower Compression Ratios: Lossless compression typically achieves lower compression ratios compared to lossy compression.
Example- Compressing a text file using ZIP reduces its size by 50%.
- When decompressed, the original text is restored without any loss.