using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            UnicodeEncoding uniEncode = new UnicodeEncoding();
            byte[] bytesToCompress = uniEncode.GetBytes("Жил бы цитрус в чаще леса? Да, но фальшивый экземпляр.");
            Console.WriteLine("starting with: " + uniEncode.GetString(bytesToCompress));
            FileStream fileToCompressTo = File.Create("examplefile.gz");
            Over_write file_to_compress_to = new Over_write(fileToCompressTo);
            try
            {
                DeflateStream compressionStream = new DeflateStream(file_to_compress_to, CompressionMode.Compress);
                try
                {
                    compressionStream.Write(bytesToCompress, 0, bytesToCompress.Length);
                }
                finally
                {
                    compressionStream.Flush();
                    compressionStream.Close();
                    compressionStream = null;
                }
            }
            finally
            {
                file_to_compress_to.Close();
                file_to_compress_to = null;
                fileToCompressTo.Close();
                fileToCompressTo = null;
            }
            byte[] decompressedBytes = new byte[bytesToCompress.Length];
            FileStream fileToDecompress = File.Open("examplefile.gz", FileMode.Open);
            try
            {
                DeflateStream decompressionStream = new DeflateStream(fileToDecompress, CompressionMode.Decompress);
                try
                {
                    decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length);
                }
                finally
                {
                    decompressionStream = null;
                }
            }
            finally
            {
                fileToDecompress = null;
            }
            Console.WriteLine("  ending with: " + uniEncode.GetString(decompressedBytes));
            Console.ReadLine();
        }
    }

    public class Over_write : System.IO.Stream
    {
        Stream File_stream = null;
        public Over_write(Stream file_stream) : base()
        {
            File_stream = file_stream;
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override bool CanSeek { get { return false; } }
        public override bool CanRead { get { return false; } }
        public override long Length { get { return File_stream==null ? 0 : File_stream.Length; } }
        public override long Position {
            get { return File_stream == null ? 0 : File_stream.Position;  }
            set { }
        }
        public override int Read(byte[] what, int offset, int count) { return 0; }
        public override void Write(byte[] what, int offset, int count)  {
            if (File_stream == null) return;
            Console.Write("write bytes: " + count.ToString());
            File_stream.Write(what, offset, count);
        } 
        public override void Flush() { if (File_stream != null) File_stream.Flush(); }
        public override long Seek(long where, SeekOrigin origin) { return 0L; }
        public override void SetLength(long value) { }



    }
}
