Exemplo n.º 1
0
        public static void DecompressFile <TSymmetricAlgorithm>(string sourcePath, string destPath, string password = null, PaddingMode paddingMode = PaddingMode.PKCS7, CipherMode cipherMode = CipherMode.CBC) where TSymmetricAlgorithm : SymmetricAlgorithm, new()
        {
            var hasPassword = !string.IsNullOrEmpty(password);
            Action <Stream, Stream> decryptor    = (source, dest) => Tools.Streams.Decrypt <TSymmetricAlgorithm>(source, dest, password, null, paddingMode, cipherMode);
            Action <Stream, Stream> decompressor = Tools.Streams.GZipDecompress;
            Action <Stream, Stream> noop         = (source, dest) => Tools.Streams.RouteStream(source, dest);

            using (var sourceStream = File.OpenRead(sourcePath))
                using (var destStream = File.OpenWrite(destPath))
                    using (var streamPipeline = new StreamPipeline(hasPassword ? decryptor : noop, decompressor))
                        streamPipeline.Run(sourceStream, destStream);
        }
Exemplo n.º 2
0
        public static string DecompressText <TSymmetricAlgorithm>(byte[] bytes, string password = null, PaddingMode paddingMode = PaddingMode.PKCS7, CipherMode cipherMode = CipherMode.CBC) where TSymmetricAlgorithm : SymmetricAlgorithm, new()
        {
            var hasPassword = !String.IsNullOrEmpty(password);
            Action <Stream, Stream> decryptor    = (source, dest) => Streams.Decrypt <TSymmetricAlgorithm>(source, dest, password, null, paddingMode, cipherMode);
            Action <Stream, Stream> decompressor = Streams.GZipDecompress;
            Action <Stream, Stream> noop         = (source, dest) => Streams.RouteStream(source, dest);

            using (var sourceStream = new MemoryStream(bytes))
                using (var destStream = new MemoryStream())
                    using (var streamPipeline = new StreamPipeline(hasPassword ? decryptor : noop, decompressor)) {
                        streamPipeline.Run(sourceStream, destStream);
                        return(ConvertToString(destStream.ToArray()));
                    }
        }