Created
June 24, 2020 00:26
-
-
Save luisdeol/2d3634bcdfba97f3bbc9310c8cf86526 to your computer and use it in GitHub Desktop.
4.2: Streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.IO.Compression; | |
| using System.Linq; | |
| using System.Text; | |
| namespace ImplementDataAccess._41_PerformIoOperations | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var filePathUncompressed = @"../../FileToCompress/file.txt"; | |
| var filePathCompressed = @"../../FileToCompress/file.gz"; | |
| Console.WriteLine("Type a text content for the new file."); | |
| var content = Console.ReadLine(); | |
| var contentBytes = Encoding.ASCII.GetBytes(content); | |
| content = string.Concat(Enumerable.Repeat(content, 1000)); | |
| using (var streamWriter = File.CreateText(filePathUncompressed)) | |
| { | |
| streamWriter.Write(content); | |
| using (var compressedFileStream = File.Create(filePathCompressed)) | |
| { | |
| using (var gzipStream = new GZipStream(compressedFileStream, CompressionMode.Compress)) | |
| { | |
| gzipStream.Write(contentBytes, 0, contentBytes.Length); | |
| } | |
| } | |
| } | |
| var uncompressedFileInfo = new FileInfo(filePathUncompressed); | |
| var compressedFileInfo = new FileInfo(filePathCompressed); | |
| Console.WriteLine($"Uncompressed file: {uncompressedFileInfo.Length}"); | |
| Console.WriteLine($"Compressed file: {compressedFileInfo.Length}"); | |
| Console.Read(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment