Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Created June 24, 2020 00:26
Show Gist options
  • Select an option

  • Save luisdeol/2d3634bcdfba97f3bbc9310c8cf86526 to your computer and use it in GitHub Desktop.

Select an option

Save luisdeol/2d3634bcdfba97f3bbc9310c8cf86526 to your computer and use it in GitHub Desktop.
4.2: Streams
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