Gewusst wie: Komprimieren von Dateien
.NET Framework 2.0
Mit der GZipStream-Klasse können Sie Daten komprimieren und dekomprimieren. Im folgenden Codebeispiel wird im aktuellen Verzeichnis eine Datei (test.txt) erstellt und mit Text gefüllt. Zudem wird der Inhalt der Datei auf der Konsole angezeigt. Der Code erstellt dann mithilfe der GZipStream-Klasse eine komprimierte Version der Datei (test.txt.gz) und vergleicht die Größe der beiden Dateien. Schließlich liest der Code die komprimierte Datei ein, dekomprimiert sie und schreibt eine neue Datei (test.txt.gz.txt) in das aktuelle Verzeichnis. Anschließend zeigt der Code den Inhalt der dekomprimierten Datei an.
Sie können Daten auch mit der DeflateStream-Klasse komprimieren und dekomprimieren.
Beispiel
using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; public class CompressionSnippet { public static void Main() { string path = "test.txt"; // Create the text file if it doesn't already exist. if (!File.Exists(path)) { Console.WriteLine("Creating a new test.txt file"); string[] text = new string[] {"This is a test text file.", "This file will be compressed and written to the disk.", "Once the file is written, it can be decompressed", "using various compression tools.", "The GZipStream and DeflateStream class use the same", "compression algorithms, the primary difference is that", "the GZipStream class includes a cyclic redundancy check", "that can be useful for detecting data corruption.", "One other side note: both the GZipStream and DeflateStream", "classes operate on streams as opposed to file-based", "compression; data is read on a byte-by-byte basis, so it", "is not possible to perform multiple passes to determine the", "best compression method. Already compressed data can actually", "increase in size if compressed with these classes."}; File.WriteAllLines(path, text); } Console.WriteLine("Contents of {0}", path); Console.WriteLine(File.ReadAllText(path)); CompressFile(path); Console.WriteLine(); UncompressFile(path + ".gz"); Console.WriteLine(); Console.WriteLine("Contents of {0}", path + ".gz.txt"); Console.WriteLine(File.ReadAllText(path + ".gz.txt")); } public static void CompressFile(string path) { FileStream sourceFile = File.OpenRead(path); FileStream destinationFile = File.Create(path + ".gz"); byte[] buffer = new byte[sourceFile.Length]; sourceFile.Read(buffer, 0, buffer.Length); using (GZipStream output = new GZipStream(destinationFile, CompressionMode.Compress)) { Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name, destinationFile.Name, false); output.Write(buffer, 0, buffer.Length); } // Close the files. sourceFile.Close(); destinationFile.Close(); } public static void UncompressFile(string path) { FileStream sourceFile = File.OpenRead(path); FileStream destinationFile = File.Create(path + ".txt"); // Because the uncompressed size of the file is unknown, // we are using an arbitrary buffer size. byte[] buffer = new byte[4096]; int n; using (GZipStream input = new GZipStream(sourceFile, CompressionMode.Decompress, false)) { Console.WriteLine("Decompressing {0} to {1}.", sourceFile.Name, destinationFile.Name); n = input.Read(buffer, 0, buffer.Length); destinationFile.Write(buffer, 0, n); } // Close the files. sourceFile.Close(); destinationFile.Close(); } }