Compression Tips

Array Collections File String Windows VB.NET Algorithm ASP.NET Cast Class Compression Convert Data Delegate Directive Enum Exception If Interface Keyword LINQ Loop Method .NET Number Regex Sort StringBuilder Struct Switch Time Value

Compression concept illustration

Compression optimizes for space. It takes the bit patterns that occur most often and represents them with shorter sequences. Time passes and the quantity of data increases. Compression algorithms such as PNG, GZIP, and 7Z become even more important. Executable programs and framework methods are used for data compression.

In general, if our messages are such that some symbols appear very frequently and some very rarely, we can encode data more efficiently (i.e., using fewer bits per message) if we assign shorter codes to the frequent symbols. Abelson & Sussman, p. 161

Expanded file [input] size
    (Typical text file.)

26,747 bytes

Compressed file [output] size
    (72% smaller.)

 7,388 bytes

7-Zip

7-Zip icon

The 7-Zip compression utility is an open-source project mainly developed by Igor Pavlov. It provides excellent compression ratios, far greater than those in most compression utilities such as the one in Windows. We focus on using the 7-Zip executables in a programmatic way.

7-Zip Command-Line Examples
Input file [folder with 1574 files]

perls      7.89 MB

Output files [compressed]

perls.zip  4.41 MB [Windows compressed folder]
perls.7z   3.20 MB [7-Zip LZMA ultra compression]

Overview: This section covers compression algorithms. It emphasizes 7-Zip and methods from the .NET Framework.

DEFLATE benchmark

How can you improve files you are compressing with DEFLATE with 7-Zip? In this benchmark, I test various command-line options and present an optimal command line.

7-Zip DEFLATE Compression Ratios

PPMd benchmark

The acronym PPMd stands for Prediction by Partial Matching. It is very effective on certain kinds of text-based files usually. In my testing, I found it can compress text files containing English very well.

PPMd Compression Benchmark in 7-Zip

C# examples

The C# programming language

You can directly compress and decompress data in the C# programming language and .NET Framework. We reveal how you can do this. The code is somewhat more complex than would be ideal, but it is reliable and tested.

Compress Data: GZIP Decompress GZIP With CompressionMode.Decompress GZipStream Example 7-Zip Executable Tutorial

Testing GZIP files. Next, there are some articles that help you detect and rewrite GZIP files directly in the C# programming language. They can be used in algorithms that must detect unknown data types, and also for improving compression ratios.

GZIP File Test GZIP Header Flag ByteASP.NET web programming framework

Compression in ASP.NET. You can built GZIP compression directly into your ASP.NET website. You do not need to have IIS compress your data for you. We introduce compression approaches in ASP.NET.

Accept-Encoding GZIP HTTP Compression Overview GZIP Output

System.IO.Compression. What is inside the System.IO.Compression namespace in the .NET Framework? What is the difference between GZipStream and DeflateStream? We answer these questions.

System.IO.Compression Namespace

C# strings

In the C# language, strings are encoded with two bytes representing each character. ASCII strings, however, require only one byte per character. By using byte arrays, you can reduce memory usage of your data.

ASCII String Representation

CSS

Cascading style sheet (CSS)

Every time a visitor loads your website, the CSS content will be downloaded and processed. You can reduce the amount of time this takes by minifying your CSS text. This article provides some tips.

Minify CSS Tutorial

DeflOpt

The DeflOpt utility is an interesting additional optimization you can add to files you compress. This program improves compression ratios on most files in GZIP format—test it and find out if it works for you.

DeflOpt Utility

Images

Image (graphical text)

You can also optimize images such as PNG images and also ICO images. These articles describe methods and provide benchmarks for image compression approaches. They are useful for optimizing website performance.

Favicon.ico Compression Tutorial PNG Optimization Tutorial

Summary

Compression of data does not just save space. By representing the data in a more compact way, algorithms can act upon that data while touching fewer memory regions. This results in an increase in spatial locality and overall performance.

Dot Net Perls