C# : .NET
Top 36 C# Example Pages...

[["X(CSTUUUUTTUUUUY64646766677F+CB(CBC1C/CSTTUUUUTTUUUU","sws.rst.","GZipStream"," compresses data. It saves data efficiently\u2014such as in compressed log files. We develop a utility method in the C# language that uses the System.IO.Compression namespace. It creates GZIP files. It writes them to the disk.","Example."," Here we will use the .NET Framework's built-in methods. It provides these new methods because compression and decompression using GZIP is an extremely common problem in any programming language. GZIP is widely used on the Internet. ","Note: ","The GZIP compression algorithm is used on many web pages to boost performance. This method shows the use of GZIP on a string.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that compresses","\n\nusing System.IO;\nusing System.IO.Compression;\nusing System.Text;\n\nclass Program\n{\n static void Main()\n {\n try\n {","\n // 1.\n // Starting file is 26,747 bytes.\n ","string anyString = File.ReadAllText(","\"TextFile1.txt\"",");","\n\n // 2.\n // Output file is 7,388 bytes.\n ","CompressStringToFile(","\"new.gz\"",", anyString);\n }\n catch\n {","\n // Could not compress.\n ","}\n }\n\n public static void ","CompressStringToFile","(string fileName, string value)\n {","\n // A.\n // Write string to temporary file.\n ","string temp = Path.GetTempFileName();\n File.WriteAllText(temp, value);","\n\n // B.\n // Read file into byte array buffer.\n ","byte[] b;\n using (FileStream f = new FileStream(temp, FileMode.Open))\n {\n b = new byte[f.Length];\n f.Read(b, 0, (int)f.Length);\n }","\n\n // C.\n // Use GZipStream to write compressed bytes to target file.\n ","using (FileStream f2 = new FileStream(fileName, FileMode.Create))\n using (GZipStream gz = new ","GZipStream","(f2, CompressionMode.Compress, false))\n {\n gz.","Write","(b, 0, b.Length);\n }\n }\n}\n\n","Results","\n\nStarting file is 26,747 bytes.\nOutput file is 7,388 bytes.","The method compresses strings"," to disk. GZipStream requires bytes and this example uses a byte array. We can use the File static methods to write to temporary files. You see several useful framework methods. ","Note: ","Remember to add System.IO.Compression at the top. You can find more information about byte arrays here.","Byte Array ","byte-array","In part A,"," we combine the Path.GetTempFileName() method and File.WriteAllText. This writes the string to a temporary file. In part B, we use FileStream here to read in bytes from the file. ","Using: ","The using statement provides effective system resource disposal. More examples are available.","Using ","using","Finally: ","In part C, we use a new GZipStream to write the compressed data to the disk. We must combine it with a FileStream.","Reliable."," This is a tested and reliable method that I have used in deployed applications with no problems. It however can be slow so if you are working on a program with extremely strict performance requirements, don't use this code. ","Note: ","This article used to have an example that was incorrect. It had an encoding problem. I thank Jon for his bug report.","Summary."," We saw an example of using GZipStream to compress to a file with an underlying FileStream. More complex methods are available for compression and this example alone does not meet most requirements. ","Tip: ","For the best compression ratio, going outside the .NET Framework and embedding 7-Zip is often helpful.","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"],"url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMcAAABGCAMAAABPN3CJAAAAGFBMVEX////R0dGb+pyzs7PU/dXo6Og/9kOVlZVen+xqAAACQUlEQVR4Xu3Z626sMAwEYI8v4f3f+OgURWpYh9ltSllXnd8bwidrTQxyawIAQooH2x6L6oweVGf01K1IbJ9jZR22DUFVx/Y7HfbnKPD/qNN26zde0ndrPkBC6iaMMMoEHxKD1E+QUvzF3csTVNse/bDoMZ4MdDwifJk8/SPs7cEMkSu0DXHxdowmzZNHhC6zzJFcKDCsQq4Yo+3dHBgU2UjsjeQmBw9GRgEHh3ir4OCQVs2Rn5W1uKMvbsUdG/JyqLuWcmxpOTTX+fkQEVMYvjpHRSQURCDdwo8O2fOqgzCYg2CHbZCVUyf3q686OGPdESIpJHFIz4sOzlh32Ilv2pL0NQdnrDsgkhcEK44xnLHumO5l4lOHUwdnXOUQ6nDiWGAsO+wSB2dc6LADZMFBGbc6lDgWGBc6bKFfEca1jo05GnE8zUAP4if67vz80YiDVMOS2qw6YvocfHQoP19xxkUOJI4O9JZCvBEHYXyrg04H6TjonZE7OOM6h+Vd1yS9ZVVtEwdnbNhz5ggAowNAUEd/8Ya8OzYS4qDBc8uIo8dsNjl7DQffQGs7THq0jIMs1MIOiJxCtIYD5MuBewFHph9eIuqDTO53GOilO2WPi7ylQ2KQ4Eh3/r1Q5Y4kDTYA+x8k7rQbtTd0nMez+9TRIQUc/XPBCUMLOLz1qHtvXYd4AYc2knvKEWYPc4YZTsrB43c4tiy2Vo4CjmcYFRzOGQUcfPxwqeIQJ8Uo4phL1KWSI3/2qYpILUen6I7pZ/c6jn+IiIP+NFgRZQAAAABJRU5ErkJggg==)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEX/2en///+VlZX29vY4hdadAAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIwAAAB4BAMAAADS7QcCAAAAGFBMVEX///95cvSaspm/zr6ppPiuqvilofiMhfa6xS0cAAAAfUlEQVR42u3XsQmAMBgF4V9xAAOCfVxAdAEdQtwg1il0fsHS8IpIsLob4OvPnKy1V7WXWR9ER8oMs8hbZ6ImZUYTTTAwMDAwMDAwMDAwMDAfmX4T7Tl3V+o1iYieKifLYs5NFLOYxUQrDAwMDAwMDAwMDAwMzA/MFUSx0GveDtlxrtujxRkAAAAASUVORK5CYII=)"]

["B","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAACsCAMAAADllnb2AAAAYFBMVEX////6lbiX3axHPiH97gb7q161sqeYPl3/8vyEj4Td4Ui83XZvzfH8x7VmbFX8eob7/rLW5sn+5NiekecRAw/8xzb/3/vw8/T9/87/yPnD6/iV2/D+/um0pwjKyMj/svb+8CRAAAAJSUl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