Beispiel #1
0
        protected void DecompressWithInflaterStream(Func <Stream, Stream> createDeflaterStream, Encoding encoding, byte[] buffer)
        {
            _content = _content ?? new string[_contentIds.Length];
            using (var inStream = new MemoryStream(_compressedContent))
            {
                using (var outStream = createDeflaterStream(inStream))
                {
                    var offsets = new uint[_contentIds.Length];

                    for (int i = 0; i < _contentIds.Length; ++i)
                    {
                        outStream.Read(buffer, 0, 4);
                        offsets[i] = BigEndianBitConverter.ToUInt32(buffer, 0);
                    }

                    long position           = 4 * _contentIds.Length;
                    long positionAfterTable = position;

                    for (int i = 0; i < _contentIds.Length; ++i)
                    {
                        var toSkip = position - (offsets[i] + positionAfterTable);
                        if (0 != toSkip)
                        {
                            Skip(outStream, toSkip, buffer);
                        }
                        else if (toSkip < 0)
                        {
                            throw new InvalidOperationException("Can not go backwards");
                        }
                        try
                        {
                            _content[i] = BigEndianBitConverter.ReadBigText(outStream, encoding, buffer, ref position);
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debugger.Launch();

                            _content[i] = "Content is not available because lzma2 decompression reached end before position were content was supposed to be";
                        }
                    }
                }
            }
        }