Compression
What is Data Compression?
Data compression is a technique used to reduce the size of files while preserving essential information. It allows for faster data transfer, efficient storage, and optimised performance.Compression is used in various formats, including text, images, audio, and video.
Types of Compression
| Type | Description | Example |
|---|---|---|
| Lossy Compression | Permanently removes some redundant data to reduce file size. Commonly used in images, audio, and video formats. | JPEG or MP3 compression reduces file size by discarding less noticeable data. |
| Lossless Compression | Reduces file size, but all original data can be restored. Often used for text files, databases, and high-quality images. | ZIP files and PNG images preserve all original content. |
Common Compression Techniques
- Run-Length Encoding (RLE): is a Lossless compression technique. It encodes sequences of repeated characters as a single value and count.
Example:AAAABBBCCCC(raw) →4A3B4C(compressed) - Transform Coding: Used in JPEG and MP3 formats. It transforms data into a more compact representation using mathematical techniques such as Discrete Cosine Transform (DCT).
When Compression May Not Be Suitable
Compression techniques can be ineffective or even counterproductive in certain situations. For example, Run-Length Encoding (RLE) is not efficient for data that lacks repetition.
Example: The string ABBCDEEFG contains few repeated characters. Using RLE would increase the size: 1A2B1C1D2E1F1G, making the compression ineffective.
Comparison: Lossy vs. Lossless Compression
| Feature | Lossy Compression | Lossless Compression |
|---|---|---|
| Data Loss | Yes, some data is permanently removed | No, original data is preserved |
| File Size Reduction | Significant | Moderate |
| Best For | Images, videos, audio (JPEG, MP3, MP4) | Text files, databases, high-quality images (PNG, ZIP) |
| Reversibility | Not reversible | Fully reversible |
Scenario Comparison
| Scenario | Description | Why This Type? |
|---|---|---|
| Streaming Music | A music streaming app compresses audio files using lossy MP3 compression. The reduced file size ensures fast downloads and playback without noticeable loss in quality. | Lossy: Users prioritise performance and storage over perfect sound fidelity. |
| Legal Document Archiving | An archival system stores legal contracts using lossless ZIP compression. Preserving every character is essential for legal integrity. | Lossless: Data accuracy and recoverability are non-negotiable. |
Applications of Data Compression
- Multimedia Storage: Reduces the size of videos, images, and audio files without noticeable quality loss.
- File Archiving: ZIP, RAR, and GZIP allow for efficient file storage and transfer.
- Data Transmission: Compressed files load faster on websites and improve network efficiency.
Key Takeaways
- Data compression reduces file sizes for efficient storage and transmission.
- Lossy compression permanently removes redundant data to reduce size significantly.
- Lossless compression retains all original data, making it reversible.
- Common compression techniques include Run-Length Encoding (RLE) and Transform Coding.
- Compression is widely used in multimedia, data storage, and file transfers.
- Not all data benefits from compression - especially highly random or already compact files.