Dot Net Perls
Top 37 C# Example Pages

["-swsy.yrse.X+(CST~~}T~~YF6464966G5577F+CE(CEC1CaCP86G557XS}T~~}T~~","GZipStream."," This class compresses data. It saves data efficiently\u2014such as in compressed log files. GZIP has a file header, while DEFLATE does not.","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.","GZipStream example."," We use the .NET Framework's built-in methods. Compression and decompression using GZIP is common 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","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","Based on:"," .NET 4.7 (2017)\n\n","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.","Notes, above method."," 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. ","Note: ","Remember to add System.IO.Compression at the top. You can find more information about byte arrays here.","Byte Array ","byte-array","Notes, continued."," In part A, we use Path.GetTempFileName 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.","Notes, reliable."," This is a tested and reliable method that I have used in deployed applications with no problems. It however can be slow. ","Note: ","This article used to have an example that was incorrect. It had an encoding problem. Jon wrote in with a fix.","DeflateStream."," With DeflateStream we can output a deflate file. A GZIP file contains a DEFLATE body. With GZIP we have an additional header. ","So: ","We can use DeflateStream to generate the DEFLATE body with no header. This may be useful in some situations.","C# program that uses DeflateStream","\n\nusing System.IO;\nusing System.IO.Compression;\n\nclass Program\n{\n static void Main()\n {\n CompressWithDeflate(","\"C:\\\\programs\\\\example.txt\"",",\n ","\"C:\\\\programs\\\\deflate-output\"",");\n }\n\n static void CompressWithDeflate(string inputPath, string outputPath)\n {","\n // Read in input file.\n ","byte[] b = File.ReadAllBytes(inputPath);","\n\n // Write compressed data with DeflateStream.\n ","using"," (FileStream f2 = new FileStream(outputPath, FileMode.Create))\n ","using"," (","DeflateStream"," deflate = new DeflateStream(f2,\n CompressionMode.Compress, false))\n {\n deflate.Write(b, 0, b.Length);\n }\n }\n}","A summary."," We used GZipStream to compress to a file with an underlying FileStream. More complex methods are available for compression. This example alone does not meet most requirements. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"]

%iVBORw0KG;)NSUhEUg?AMc?ABGCAM?ABPN3CJ)GFBMVEX::R0dGb+pyzs7PU/dXo6Og/9kOVlZVen+xq?A.UlEQVR4Xu3Z626sMAwEYI8v4f3f+OgURWpYh9ltSllXnd8bwidrTQxyawIAQ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