public void SetUp()
    {
        using (var targetStream = new MemoryStream())
        {
            using (var sourceStream = new MemoryStream(Encoding.ASCII.GetBytes(RawData)))
            {
                using (var gzipStream = new GZipStream(targetStream, CompressionMode.Compress))
                {
                    Streams.Copy(sourceStream, gzipStream);
                }
            }

            compressedData = targetStream.ToArray();
        }
    }
    public void Raw_GZip_Stream_Fails_On_Mocked_Stream()
    {
        TestDelegate act = () => {
            using (var targetStream = new MemoryStream())
            {
                using (var mockStream = new MockStream(compressedData, 5))
                {
                    using (var gzipStream = new GZipStream(mockStream, CompressionMode.Decompress))
                    {
                        Streams.Copy(gzipStream, targetStream);
                    }
                }
            }
        };

        Assert.Throws(typeof(ZlibException), act);
    }
Пример #3
0
        void SaveMessage(MessageHandlerClosure closure, CellarWriter writer)
        {
            try {
                var message    = closure.Message;
                var keySegment = new ArraySegment <byte>(message.Key);

                using (var mem = new MemoryStream(message.Value))
                    using (var zip = new LZ4Stream(mem, CompressionMode.Decompress))
                        using (var pooled = _recyclableStreamManagerWrapper.GetStream("mv")) {
                            Streams.Copy(zip, pooled, _buffer);
                            pooled.Seek(0, SeekOrigin.Begin);

                            writer.Append(keySegment, pooled, (int)pooled.Length);
                        }
            }
            catch (Exception ex) {
                Console.WriteLine("Failed to read message {0}", ex.Message);
            }
        }
Пример #4
0
        private void UnpackRegularFile(Pack1Meta.FileEntry file)
        {
            string destPath = Path.Combine(_destinationDirPath, file.Name);

            DebugLogger.LogFormat("Unpacking regular file {0} to {1}", file, destPath);

            Files.CreateParents(destPath);

            RijndaelManaged rijn = new RijndaelManaged
            {
                Mode    = CipherMode.CBC,
                Padding = PaddingMode.None,
                KeySize = 256
            };

            using (var fs = new FileStream(_packagePath, FileMode.Open))
            {
                fs.Seek(file.Offset.Value, SeekOrigin.Begin);

                using (var limitedStream = new LimitedStream(fs, file.Size.Value))
                {
                    ICryptoTransform decryptor = rijn.CreateDecryptor(_key, _iv);
                    using (var cryptoStream = new CryptoStream(limitedStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (var gzipStream = new GZipStream(cryptoStream, Ionic.Zlib.CompressionMode.Decompress))
                        {
                            using (var fileWritter = new FileStream(destPath, FileMode.Create))
                            {
                                Streams.Copy(gzipStream, fileWritter);
                                if (Platform.IsPosix())
                                {
                                    Chmod.SetMode(file.Mode.Substring(3), destPath);
                                }
                            }
                        }
                    }
                }
            }

            DebugLogger.Log("File " + file.Name + " unpacked successfully!");
        }
    public void Wrapped_Read_Succeeds_On_Mocked_Stream()
    {
        string decompressed;

        using (var targetStream = new MemoryStream())
        {
            using (var mockStream = new MockStream(compressedData, 5))
            {
                using (var wrapperStream = new GZipReadWrapperStream(mockStream))
                {
                    using (var gzipStream = new GZipStream(wrapperStream, CompressionMode.Decompress))
                    {
                        Streams.Copy(gzipStream, targetStream);
                    }
                }
            }

            decompressed = Encoding.ASCII.GetString(targetStream.ToArray());
        }

        Assert.That(decompressed == RawData);
    }
Пример #6
0
        public Lmdb.ChunkDto Compress(byte[] symmetricKey)
        {
            var fileName = _stream.Name + ".lz4";

            _stream.Seek(0, SeekOrigin.Begin);


            using (var chunkFile = File.Create(fileName)) {
                var flags = LZ4StreamFlags.HighCompression | LZ4StreamFlags.IsolateInnerStream;



                using (var aes = new AesCryptoServiceProvider()) {
                    aes.Key     = symmetricKey;
                    aes.Mode    = CipherMode.CBC;
                    aes.Padding = PaddingMode.PKCS7;
                    aes.GenerateIV();
                    var iv = aes.IV;
                    chunkFile.Write(iv, 0, iv.Length);

                    using (var enc = aes.CreateEncryptor())
                        using (var crypto = new CryptoStream(chunkFile, enc, CryptoStreamMode.Write))
                            using (var lz4 = new LZ4Stream(crypto, LZ4StreamMode.Compress, flags, 1048576 * 4)) {
                                Streams.Copy(_stream, lz4, (int)_pos);
                            }
                }
            }

            var size = new FileInfo(fileName).Length;

            return(Lmdb.NewChunkDto()
                   .SetChunkFileName(fileName)
                   .SetUncompressedByteSize(_pos)
                   .SetChunkRecords(_records)
                   .SetCompressedDiskSize((int)size)
                   .SetChunkStartPos(_startPos));
        }