Array Class Collections File String .NET Algorithm ASP.NET Cast Compression Data Delegate Directive Enum Exception If Interface Keyword LINQ Loop Method Number Regex Sort StringBuilder Struct Switch Time Value Windows WPF
Data increases at a phenomenal pace. All things are digitized, recorded, stored. This requires more and more storage. In compression, we tame this data dragon with advanced algorithms. We optimize for space.
In general, if our messages are such that some symbols appear very frequently and some very rarely, we can encode data more efficiently... if we assign shorter codes to the frequent symbols. Abelson & Sussman, p. 161
A data compression ratio indicates how well an algorithm works. We compute it by dividing the uncompressed size by the compressed size. Often we trade space for time: a slower, more thorough algorithm yields a greater ratio.
We use the 7-Zip program to compress files.
This open-source tool has superb algorithms.
Then: We explore the compression tools in the .NET Framework. We even compress images and minify CSS files.
The 7-Zip compression utility is an open-source project developed by Igor Pavlov. It provides excellent compression ratios, far greater than those in most compression utilities such as the one in Windows.7-Zip Command-Line
Input file: 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]
With 7-Zip, we can specify a range of compression levels. These modes can be specified on the command line or in the 7-Zip interface. I tested these modes on a folder containing 1837 text files and images.
Compression mode results: 1837 files Store: -mx0 8.46 MB Fastest: -mx1 3.98 MB Fast: -mx3 3.89 MB Normal: -mx5 3.58 MB Maximum: -mx7 3.56 MB Ultra: -mx9 3.56 MB
The difference between store and fastest was big. It more than halved the size of the file. But the returns were diminishing. In compression, and all forms of optimization, this is often true.
DEFLATE: Used in GZIP. We test various DEFLATE command-line options and present an optimal command line.DEFLATE
PPMd: Stands for Prediction by Partial Matching. It is often effective on certain kinds of text-based files.PPMd
DeflOpt: The DeflOpt utility is an interesting additional optimization you can add to files you compress. It improves compression ratios.DeflOpt
We can directly compress and decompress data in the C# language. The code is perhaps more complex than ideal. But it is reliable and tested. These examples use the System.IO.Compression namespace.Compress Decompress GZipStream 7-Zip Executable
Compression in ASP.NET: You can build GZIP compression directly into your ASP.NET website. We introduce compression approaches in ASP.NET.Accept-Encoding: GZIP Request HTTP Compression GZIP Output
Test GZIP files: We detect and rewrite GZIP files directly in the C# language. These methods help in programs that handle compressed files.GZIP File Test GZIP Header Flag Byte
New release versions of the .NET Framework provide the System.IO.Compression namespace. In the old days, .NET developers had to turn to third-party compression algorithms, but now they don't need to distribute that code.
Classes DeflateStream GZipStream Enum CompressionMode Compress Decompress
Difference. GZipStream is implemented with the DeflateStream. When you use GZipStream, you are using a simple wrapper type around an actual DeflateStream. Because of this, they will provide the same compression ratios.
If you have a GZIP file, you need to use GZipStream.
It provides support for GZIP headers.
CompressionMode. When you use the DeflateStream or GZipStream constructors, you need to pass in an existing stream, which can be a MemoryStream or FileStream, as well as a CompressionMode or CompressionLevel.
Tip: Compression can enhance performance in many situations, including web site loading and backups.
And: The compression ratio provided by System.IO.Compression is not the best. It is adjusted with a CompressionLevel argument.CompressionLevel
In the .NET Framework version 4.5, a class that makes compression easier is available. The ZipFile class, and its methods CreateFromDirectory and ExtractToDirectory, enables compression of a directory in one method call.ZipFile
Strings in the C# language use two bytes for each character. ASCII strings, however, require only one byte per character. By using byte arrays, you can reduce memory usage of your data.ASCII Strings
Tip: In memory, using the smallest value type that fits all encountered values usually reduces memory.
However: As with the string memory optimization, risks are introduced. An expected value could cause a more serious error.ValueType
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
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 PNG
Tip: A software program (such as Adobe Photoshop) can improve image optimization levels.
Compression of data saves not just space. By representing the data in a more compact way, algorithms acting upon that data touch fewer memory regions. This results in an increase in spatial locality. Less time too is needed.