Advertisement
Guest User

inflate for TPK

a guest
Nov 18th, 2012
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Security.Cryptography;  // HashAlgorithm
  8. using Ionic.Zlib;
  9.  
  10. namespace DeflateTest
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             Byte[] compressed;
  17.  
  18.             BinaryReader reader = new BinaryReader(File.Open("D:\\TITLE_MENU.TPK", FileMode.Open));
  19.             BinaryWriter write = new BinaryWriter(File.Open("D:\\testout.bin", FileMode.Create));
  20.  
  21.             UInt32 magicNumber = reader.ReadUInt32();
  22.             UInt32 uncompressedSize = reader.ReadUInt32();
  23.             UInt32 unknown = reader.ReadUInt32();
  24.  
  25.             compressed = reader.ReadBytes((int)(reader.BaseStream.Length - 0x0C));
  26.  
  27.             Byte[] output = new Byte[uncompressedSize];
  28.  
  29.             ZlibCodec compressor = new ZlibCodec();
  30.             compressor.InitializeInflate(true);
  31.  
  32.             compressor.InputBuffer = compressed;
  33.             compressor.AvailableBytesIn = compressed.Length;
  34.             compressor.NextIn = 0;
  35.             compressor.OutputBuffer = output;
  36.  
  37.             foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish })
  38.             {
  39.                 int bytesToWrite = 0;
  40.                 do
  41.                 {
  42.                     compressor.AvailableBytesOut = (int)uncompressedSize;
  43.                     compressor.NextOut = 0;
  44.                     compressor.Inflate(f);
  45.  
  46.                     bytesToWrite = (int)(uncompressedSize - compressor.AvailableBytesOut);
  47.  
  48.                     if (bytesToWrite > 0)
  49.                         write.Write(output, 0, bytesToWrite);
  50.                        
  51.                 }
  52.                 while ((f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) ||
  53.                        (f == FlushType.Finish && bytesToWrite != 0));
  54.             }
  55.  
  56.             compressor.EndInflate();
  57.  
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement