Nanook.QueenBee.Parser.PakFormat.xBoxUncompress C# (CSharp) Method

xBoxUncompress() private method

private xBoxUncompress ( string compFilename, string uncompFilename ) : CompressionType
compFilename string
uncompFilename string
return CompressionType
        private CompressionType xBoxUncompress(string compFilename, string uncompFilename)
        {
            if (uncompFilename.ToLower() == compFilename.ToLower()) //nothing to do
                return CompressionType.None;

            if (File.Exists(uncompFilename))
                File.Delete(uncompFilename);

            CompressionType ct = CompressionType.None;

            try
            {
                using (FileStream inFileStream = new FileStream(compFilename, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryEndianReader br = new BinaryEndianReader(inFileStream))
                    {
                        uint chunkLen = 0;

                        while (chunkLen != 0xffffffff)
                        {
                            using (FileStream outFileStream = new FileStream(uncompFilename, chunkLen == 0 ? FileMode.Create : FileMode.Append))
                            {
                                using (ZlibOutputStream outZStream = new ZlibOutputStream(outFileStream, Rebex.IO.Compression.CompressionMode.Decompress))
                                {
                                    //test for newer GHWT+ compression
                                    long pos = inFileStream.Position;

                                    if (Encoding.ASCII.GetString(br.ReadBytes(4)) == "CHNK")
                                    {
                                        ct = CompressionType.ZLibChunk;

                                        EndianType et = this.EndianType;

                                        //Decompress each section
                                        uint headerLen = br.ReadUInt32(et);
                                        uint blockLen = br.ReadUInt32(et);
                                        chunkLen = br.ReadUInt32(et);
                                        uint nextchunkLen = br.ReadUInt32(et);
                                        uint uncompressedLen = br.ReadUInt32(et);
                                        uint uncompressedPos = br.ReadUInt32(et);

                                        _zLibHeaderLen = headerLen;
                                        if (uncompressedLen > this._zLibChunkSize)
                                            this._zLibChunkSize = uncompressedLen;

                                        inFileStream.Seek(headerLen - (inFileStream.Position - pos), SeekOrigin.Current);
                                        outZStream.WriteByte(0x58);  //Search deflate.cs for "Nanook" for the mod to stop it being written out
                                        outZStream.WriteByte(0x85);  //this is the header MS uses, thie zlib deflate uses 78DA ??

                                        copyStream(inFileStream, outZStream, (int)blockLen);

                                        if (chunkLen != 0xffffffff)
                                            inFileStream.Seek((chunkLen - blockLen) - headerLen, SeekOrigin.Current);

                                    }
                                    else
                                    {
                                        try
                                        {
                                            inFileStream.Seek(pos, SeekOrigin.Begin);

                                            outZStream.WriteByte(0x58);
                                            outZStream.WriteByte(0x85);
                                            copyStream(inFileStream, outZStream);
                                            ct = CompressionType.ZLib;
                                            break;
                                        }
                                        catch
                                        {
                                            break;  //no compression???
                                        }
                                    }

                                    outZStream.Flush();
                                    outFileStream.Flush();
                                    //outZStream.Close();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format("Uncompress Failed: {0}", ex));
            }

            //using (DeflateStream ds = new DeflateStream(File.Open(compFilename, FileMode.Open), CompressionMode.Decompress))
            //{
            //    using (BinaryReader br = new BinaryReader(ds))
            //    {
            //        using (FileStream ms = File.Create(uncompFilename))
            //        {
            //            using (BinaryWriter bw = new BinaryWriter(ms))
            //            {
            //                long l;
            //                do
            //                {
            //                    l = ms.Length;
            //                    bw.Write(br.ReadBytes(10000));
            //                }
            //                while (l != ms.Length);
            //            }
            //        }
            //    }
            //}

            return ct;
        }