Exemplo n.º 1
0
        public void Memory_Compress_Decompress_Async() {
            var input = (byte[])PlainBytes.Clone();
            byte[] compressed;

            using(var outStream = new MemoryStream(input.Length)) {
                using(var gzip = new GZipStream(outStream, CompressionMode.Compress)) {
                    gzip.WriteAsync(input, 0, input.Length).Wait();
                }
                compressed = outStream.ToArray();
                Assert.IsNotNull(compressed);
                Assert.IsTrue(compressed.Length > 0);
            }

            using(var gzip = new GZipStream(new MemoryStream(compressed), CompressionMode.Decompress)) {
                var decompressed = With.TryFunctionAsync(() => gzip.ReadAllBytesAsync().Result);

                Assert.IsTrue(decompressed.Length > 0);
                Assert.AreEqual(PlainBytes, decompressed);
            }
        }
Exemplo n.º 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();
        }