Пример #1
0
        public static void DecompressFileLZMA(string inFile, string outFile)
        {
            SevenZip.Sdk.Compression.Lzma.Decoder coder = new SevenZip.Sdk.Compression.Lzma.Decoder();
            FileStream input = new FileStream(inFile, FileMode.Open);
            FileStream output = new FileStream(outFile, FileMode.Create);

            // Read the decoder properties
            byte[] properties = new byte[5];
            input.Read(properties, 0, 5);

            // Read in the decompress file size.
            byte[] fileLengthBytes = new byte[8];
            input.Read(fileLengthBytes, 0, 8);
            long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

            coder.SetDecoderProperties(properties);
            coder.Code(input, output, input.Length, fileLength, null);
            output.Flush();
            output.Close();

            try
            {
                output.Flush();
                output.Close();

                input.Flush();
                input.Close();
            }
            catch
            {
            }
        }
Пример #2
0
        private void HandleData(BinaryReader reader)
        {
            int count = this._mFileContent.Length - 0x20;

            if (!this.Compressed)
            {
                this._mData      = reader.ReadBytes(count);
                this._DataReader = new BinaryReader(new MemoryStream(this._mData));
            }
            else
            {
                byte[] destinationArray = new byte[count];
                Array.Copy(this._mFileContent, 0x20, destinationArray, 0, count);
                byte[] buffer = new byte[this.Header.DataSize];
                SevenZip.Sdk.Compression.Lzma.Decoder decoder = new SevenZip.Sdk.Compression.Lzma.Decoder();

                decoder.SetDecoderProperties(this.Header.Props);
                decoder.Code(new MemoryStream(destinationArray), new MemoryStream(buffer), (long)count, (long)this.Header.DataSize, null);
                this._mData      = buffer;
                this._DataReader = new BinaryReader(new MemoryStream(this._mData));
            }
        }
Пример #3
0
            public Stream OpenItem(string id)
            {
                EnsureLoaded();

                var item = _items[id];

                // Find index of current lump
                var keyArray = _items.Keys.ToArray();
                int index    = -1;

                for (int i = 0; i < keyArray.Length; i++)
                {
                    if (keyArray[i] == id)
                    {
                        index = i;
                        break;
                    }
                }

                // https://developer.valvesoftware.com/wiki/Source_BSP_File_Format#Lump_compression
                // Get the actual length of the item using offset of the next item.
                var valueArray   = _items.Values.ToArray();
                var actualLength = item.FileLength;

                if (index != -1)
                {
                    if (index < _items.Count - 1)
                    {
                        // The last game lump is /supposed/ to be a dummy containing nothing but the offset,
                        // but sometimes the offset is 0 which would result in negative actualLength.
                        if (valueArray[index + 1].FileOffset > valueArray[index].FileOffset)
                        {
                            actualLength = valueArray[index + 1].FileOffset - valueArray[index].FileOffset;
                        }
                    }
                }

                var stream = _bspFile.GetSubStream(item.FileOffset, actualLength);

                LzmaHeader lzmaHeader;

                try
                {
                    lzmaHeader = LzmaHeader.Read(stream);
                }
                catch (NotSupportedException e)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    return(stream);
                }

                using (var compressedStream =
                           _bspFile.GetSubStream(item.FileOffset + LzmaHeader.Size, lzmaHeader.LzmaSize))
                {
                    using (var uncompressedStream = new MemoryStream(( int )lzmaHeader.ActualSize))
                    {
                        Decoder decoder = new Decoder();
                        decoder.SetDecoderProperties(lzmaHeader.Properties);
                        decoder.Code(compressedStream, uncompressedStream, lzmaHeader.LzmaSize,
                                     lzmaHeader.ActualSize, null);
                        stream = new MemoryStream(( int )stream.Length);
                        uncompressedStream.Seek(0, SeekOrigin.Begin);
                        uncompressedStream.CopyTo(stream);
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);
                return(stream);
            }
        private void HandleData(BinaryReader reader)
        {
            int count = this._mFileContent.Length - 0x20;
            if (!this.Compressed)
            {
                this._mData = reader.ReadBytes(count);
                this._DataReader = new BinaryReader(new MemoryStream(this._mData));
            }
            else
            {
                byte[] destinationArray = new byte[count];
                Array.Copy(this._mFileContent, 0x20, destinationArray, 0, count);
                byte[] buffer = new byte[this.Header.DataSize];
                SevenZip.Sdk.Compression.Lzma.Decoder decoder = new SevenZip.Sdk.Compression.Lzma.Decoder();

                decoder.SetDecoderProperties(this.Header.Props);
                decoder.Code(new MemoryStream(destinationArray), new MemoryStream(buffer), (long)count, (long)this.Header.DataSize, null);
                this._mData = buffer;
                this._DataReader = new BinaryReader(new MemoryStream(this._mData));
            }
        }