Example #1
0
        public void FileAsync_Compress_Decompress()
        {
            var filename = FileTool.GetTempFileName();

            using (var fs = FileAsync.OpenWrite(filename))
                using (var gzip = new GZipStream(fs, CompressionMode.Compress)) {
                    gzip.Write(PlainBytes, 0, PlainBytes.Length);
                }

            var fi = new FileInfo(filename);

            Assert.IsTrue(fi.Exists);
            Assert.IsTrue(PlainBytes.Length > fi.Length);


            var outStream = new MemoryStream((int)fi.Length * 2);

            using (var fs = FileAsync.OpenRead(filename))
                using (var gzip = new GZipStream(fs, CompressionMode.Decompress, true)) {
                    var readCount = 0;
                    var buffer    = new byte[CompressorTool.BUFFER_SIZE];

                    while ((readCount = gzip.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        outStream.Write(buffer, 0, readCount);
                    }

                    var output = outStream.ToArray();
                    Assert.AreEqual(PlainBytes.Length, output.Length);
                    Assert.AreEqual(PlainBytes, output);
                }

            fi = new FileInfo(filename);
            fi.Delete();
        }
Example #2
0
        public void FileAsync_Compress_Decompress_Async()
        {
            var filename = FileTool.GetTempFileName();

            using (var fs = FileAsync.OpenWrite(filename))
                using (var gzip = new GZipStream(fs, CompressionMode.Compress)) {
                    gzip.WriteAsync(PlainBytes, 0, PlainBytes.Length).Wait();
                }

            var fi = new FileInfo(filename);

            Assert.IsTrue(fi.Exists);
            Assert.IsTrue(PlainBytes.Length > fi.Length);

            using (var fs = FileAsync.OpenRead(filename))
                using (var gzip = new GZipStream(fs, CompressionMode.Decompress, true)) {
                    var output = With.TryFunctionAsync(() => gzip.ReadAllBytesAsync().Result);

                    Assert.AreEqual(PlainBytes.Length, output.Length);
                    Assert.AreEqual(PlainBytes, output);
                }

            fi = new FileInfo(filename);
            fi.Delete();
        }