# AION pak to zip converted 0.4 Copyright (c) 2008-2009 by roxfan
# Version history
#  0.4 2009-05-13 fixed incorrect version detection in archives with folders or zero-sized files

from array import array
import sys, struct, zlib, binascii

#stolen from zipfile.py

structFileHeader = "<4s2B4HlLL2H"      # 12 items, file header record, 30 bytes
stringFileHeader = "PK\003\004"        # magic number for file header
stringFileHeader2 = "\xAF\xB4\xFC\xFB" 

structEndArchive = "<4s4H2lH"        # 9 items, end of archive, 22 bytes
stringEndArchive = "PK\005\006"      # magic number for end of archive record
stringEndArchive2 = "\xAF\xB4\xFA\xF9"
structCentralDir = "<4s4B4HlLL5HLl"  # 19 items, central directory, 46 bytes
stringCentralDir = "PK\001\002"      # magic number for central directory
stringCentralDir2 = "\xAF\xB4\xFE\xFD"

# indexes of entries in the local file header structure
_FH_SIGNATURE = 0
_FH_EXTRACT_VERSION = 1
_FH_EXTRACT_SYSTEM = 2                  # is this meaningful?
_FH_GENERAL_PURPOSE_FLAG_BITS = 3
_FH_COMPRESSION_METHOD = 4
_FH_LAST_MOD_TIME = 5
_FH_LAST_MOD_DATE = 6
_FH_CRC = 7
_FH_COMPRESSED_SIZE = 8
_FH_UNCOMPRESSED_SIZE = 9
_FH_FILENAME_LENGTH = 10
_FH_EXTRA_FIELD_LENGTH = 11

# indexes of entries in the central directory structure
_CD_SIGNATURE = 0
_CD_CREATE_VERSION = 1
_CD_CREATE_SYSTEM = 2
_CD_EXTRACT_VERSION = 3
_CD_EXTRACT_SYSTEM = 4                  # is this meaningful?
_CD_FLAG_BITS = 5
_CD_COMPRESS_TYPE = 6
_CD_TIME = 7
_CD_DATE = 8
_CD_CRC = 9
_CD_COMPRESSED_SIZE = 10
_CD_UNCOMPRESSED_SIZE = 11
_CD_FILENAME_LENGTH = 12
_CD_EXTRA_FIELD_LENGTH = 13
_CD_COMMENT_LENGTH = 14
_CD_DISK_NUMBER_START = 15
_CD_INTERNAL_FILE_ATTRIBUTES = 16
_CD_EXTERNAL_FILE_ATTRIBUTES = 17
_CD_LOCAL_HEADER_OFFSET = 18


#open beta
table1 = array('B',
      "\x2f\x5d\x51\xf7\x01\xe9\xb4\x93\x4e\x51\x81\x3e\xaf\x3f\xdf\x99"
      "\x80\x5e\x13\x83\x9b\x46\x57\xb5\x1b\x5c\xec\xb1\x29\x7c\xa9\x31"
      "\x68\xe5\xda\xa7\xf6\x4f\xae\x16\x9a\x7f\x03\xcf\x1d\x5e\xd0\x51"
      "\x5a\xe5\x02\xd9\x11\xd0\xfb\xf4\xf8\x7c\xa2\x88\x26\xd8\x1f\xa2"
      "\x43\xda\x33\xa9\xac\x4e\x5a\x0d\xed\x78\x86\x2d\xb2\x6a\xc4\x9b"
      "\xaa\x77\x85\x57\x6a\xa6\xd8\x35\xd8\x97\x6b\x17\x24\xb7\x7a\x1d"
      "\xd3\x3b\x9e\x79\xf2\xae\x9f\x01\xe6\x9d\x29\x40\xed\x2f\x9c\x16"
      "\xda\x18\xd1\x99\x0e\xd4\x0a\x63\x2d\x92\xd7\xeb\xb4\xa7\x50\x21"
      "\xd8\x0f\x45\xd6\xc6\xbf\xcc\x47\xcc\x59\xed\x3e\x71\xfe\xa0\x26"
      "\xfc\xd1\x07\x85\x8a\xee\x12\x36\x11\x5a\x60\xe1\x8f\xbd\x9e\xf7"
      "\xb6\x64\x39\xcd\x49\x5a\x9a\xf7\x90\x1c\xc1\xa2\x0b\xb3\x81\xf7"
      "\xca\xb8\x2a\x4b\x95\x13\xdc\x2e\x4a\xe5\x64\x16\x94\x99\xc9\xb1"
      "\x7b\x53\x76\xae\xc4\xdf\x26\xf7\xc8\x5f\x78\x31\xae\xaf\x5a\x7f"
      "\xa4\xe7\x29\x5e\x0e\xe2\xbb\x91\x41\x32\x2c\xf0\xce\x60\x9e\x27"
      "\xdc\xfa\xdc\x13\xac\x37\xf7\xf1\xb4\xa4\xcd\xf4\x7a\xdc\xa9\x7b"
      "\x95\x82\xda\x7d\xfb\x8d\x6b\x6e\x0c\x43\xe7\x23\x6c\xc0\x53\xf9"
      "\x39\x82\x38\xde\x9b\xd0\xfe\x57\x3d\x75\x65\x43\xb0\xae\x5a\x6e"
      "\x4e\xb3\xfb\xae\x8c\xc4\x0f\x9b\x65\x27\xaf\xa2\xc6\xf1\x84\x91"
      "\x94\x1a\x39\x39\x53\xa5\x90\x64\xf0\x62\xcc\xb5\xbf\x1e\xbc\xa7"
      "\x28\xae\x33\x3f\x16\xc6\x30\xb7\xb1\xf2\x83\xb1\x5e\xb0\x37\x20"
      "\x9d\xf7\x7b\x95\xbe\x35\x6e\x1b\x07\x05\x77\x32\x3a\xae\x8a\x39"
      "\x25\xaf\x10\xc5\x18\x56\xc2\x2b\xf9\xc4\x4b\xd6\xdc\x44\xd7\x9d"
      "\xa8\x5c\x7f\xad\xef\x88\xbc\x46\x5f\xfe\xc0\xe3\xde\x69\xe3\x03"
      "\xed\xf8\x06\x1f\x38\xc1\x22\x23\xf4\xc1\xd7\xe1\x11\x7b\x3c\xcb"
      "\xb4\x8d\xaf\x82\x23\x30\x0d\x78\x82\xf9\xed\x3e\x91\xe1\x52\xa7"
      "\xd5\xd5\x75\x71\x46\xda\x11\x97\xfb\x16\xdf\xea\xf3\xab\xa0\x32"
      "\x66\xdb\x5e\x5e\xb9\x43\x55\x0e\x9e\xa5\x2a\xfd\x5e\x31\xc6\x93"
      "\xd4\x9a\xa2\x2b\x37\x00\xb9\x46\x13\xf7\x05\x51\xa7\xb2\xaa\x22"
      "\x0c\x9d\xc5\xd2\x3d\x62\xf4\x28\x8c\xbc\x89\x25\x79\xfa\x9a\xfd"
      "\x8d\xa1\xbc\x02\x2b\x15\xb0\xb6\xe6\xa4\xcd\xbc\x72\xf8\x68\xb4"
      "\x9a\x33\x08\xba\x62\xb7\xb1\xb1\xca\x00\x08\x01\x40\x68\x8e\xe1"
      "\x49\x4f\xd8\xf2\x67\x85\xf0\x37\xc9\x61\xab\x1e\xc6\x6a\x4d\xca"
      "\xaf\x03\x2f\x36\x02\xf0\xbc\x5e\x81\x39\x8a\x25\x38\x2c\xca\x04"
      "\xf9\x0d\xf6\x44\x5b\x46\xdb\xde\xb7\x7b\xf4\xac\x3b\x7f\x36\x0d"
      "\x90\x7c\x2c\xb0\x20\x48\xab\xa9\x7f\x39\xdb\x6d\x0b\x80\xe2\xf1"
      "\x37\x50\xfa\x83\x9d\xd3\x3e\x8c\x54\x48\xeb\xe7\x92\x34\x6a\xeb"
      "\x2b\x18\xda\xda\xe5\x7c\x7e\xd3\x3d\xd9\xb1\xfd\x90\x28\xcd\x00"
      "\x45\x93\xb3\x86\xeb\x32\x4b\xe6\xea\x24\xb6\x97\xb4\x11\x94\xa0"
      "\x16\x53\xfb\xae\xa6\xd7\x9a\xe9\xd9\xfb\xa6\x41\xc2\x6d\xec\x4b"
      "\x0b\x59\xd7\x6c\x2e\xec\x9b\x5d\x6f\x76\x66\xcb\xb0\x23\xca\x2c"
      "\x8d\xb6\x3a\x6e\xdc\x29\xd1\xbd\x1d\x89\x3f\xeb\xc7\x22\x09\xb8"
      "\x1d\x2e\x04\x98\x71\x1a\x35\x26\x7d\xaa\xf2\xdb\xc0\x01\x8a\x56"
      "\x76\xd1\x27\xa3\x2b\xc8\x58\xea\x76\x72\xe6\xf9\xea\xa0\x54\xf4"
      "\xb2\xa4\xc0\xbb\xec\x54\x81\x3f\x58\x37\x3c\x69\x45\xc8\xb7\xb1"
      "\x60\x3b\x3d\x20\x5b\x97\xce\xd2\xfc\xb1\xf2\xaf\xa2\xcb\x67\x74"
      "\xad\x58\x79\xc8\xfe\xc1\x54\x71\xea\x98\x0b\x59\xc6\x21\xa0\x94"
      "\x7f\x91\xde\xfd\x61\xfc\x3c\xa1\x71\x47\x9f\x97\x89\x0d\x43\x74"
      "\x97\xec\x85\xfe\x2e\x0d\xe7\x49\xca\x55\x0e\xdd\xf4\x38\xf8\x22"
      "\xb1\x7e\x55\x9e\x56\xea\x0f\x4a\x3a\x3d\x0f\x86\x64\x57\x51\xf9"
      "\xa3\x0c\x23\xe4\x2a\x6a\xdf\x20\x31\xf8\xdd\x6d\xa8\xc4\xdf\x42"
      "\x7d\xae\xd2\xac\x7d\xd7\x1f\x85\x67\xa4\x4f\x97\x21\x25\x61\xd0"
      "\xa9\x6b\x77\x47\xc7\x97\x47\x13\x03\x1a\xfa\xc8\xe2\x05\xd7\xa6"
      "\x0e\xda\x71\x18\x42\xc5\xaa\xd8\xb0\x96\x53\x2f\xd3\x78\xad\x8f"
      "\x2b\xc4\x91\x3b\x07\xd7\x90\x09\xcb\x55\xcc\xf7\xcc\xbd\xcf\xc5"
      "\x3b\xc1\x34\x1d\x35\x3c\x59\x8d\x75\x35\xf7\xf7\xb7\xdb\xd6\x90"
      "\x53\xdb\x66\x20\x0e\xf7\x98\xb0\xbd\x51\xa4\x49\xb4\x3f\x1d\xe2"
      "\x82\x2b\x04\x3c\x13\x4b\x39\xb6\xbd\xa8\x00\xe7\x33\x60\xe5\xfa"
      "\xf1\x7b\xd5\x9b\x2b\x4c\x9f\x81\xb6\xb9\xb8\x55\x16\x5f\x7a\x05"
      "\x07\xe6\xb3\x3e\xbc\x8b\xc3\x2f\x37\x23\x19\x39\xd1\xa2\x4c\xba"
      "\x81\x78\xa3\x99\xd3\xb0\x53\xb9\x38\x44\x2b\xfc\x8f\x7b\x0f\xfe"
      "\x99\xca\xfb\x37\x3e\x1d\xd4\x99\x3c\xdd\xd5\x6f\x48\xc2\xe1\x83"
      "\x23\xab\x7f\x52\xa9\x89\xc4\x61\x6f\xae\x02\x66\xe9\x7a\x67\x67"
      "\xad\xb7\x80\x7f\xc8\xa8\xb5\x61\xc9\x1a\xb3\x57\x73\x6c\xe9\xd3"
      "\xa0\xfa\xfe\x43\x70\xc3\x71\x46\x2e\xbe\x2e\x02\x17\xca\x78\xa0"
)

#closed beta
table2 = array('B',
      "\x86\xFA\x1A\x1C\x07\xBD\xD8\x64\xCE\xEE\x59\x88\xCD\xA9\x1D\x06"
      "\xF7\x3D\x31\x58\x83\xA1\x5C\x7E\xDF\xA6\x50\x9E\x89\xA8\x12\xD2"
      "\x25\x49\x75\xE2\x07\x0F\xEB\x01\x97\x4A\x66\x35\xAB\x32\x9D\xA7"
      "\x4E\xA2\x89\x62\x0F\x55\x41\xC5\x52\x10\x1F\x47\xB0\xA0\x63\xA6"
      "\xF0\x1C\x1C\x4C\x9B\x3C\xAC\xE2\xB3\x4E\x9F\xF1\xA4\x91\x29\x82"
      "\xE4\x76\x0D\x8D\x4F\xA3\x34\x4A\xCC\x1C\xC7\x18\x48\x8E\xFE\x18"
      "\x79\x08\x87\x28\x8E\x24\xB7\x6B\x38\xF2\x58\x01\x2D\xA8\x58\x0E"
      "\x9C\x54\x29\xCF\xA1\xAE\x0A\xD2\x3B\x4A\x10\xF8\xD8\x19\x31\x7D"
      "\xF3\xAE\x1B\x90\xD2\x2F\x16\xC7\xE5\x3B\xCC\xEF\xE1\xE1\x2C\x86"
      "\x00\xDD\x35\x67\x8D\x25\xFC\xED\x32\x1F\xA9\x1A\x12\x6D\xB0\xF7"
      "\x3D\xB6\x1F\xE8\x81\x4D\x36\xE7\x25\x30\x21\x90\x86\x30\x0E\xEE"
      "\x40\xBE\x6E\xDA\xC1\x3A\xAF\xF2\xEC\x28\x2C\xF1\xCD\x44\x98\x72"
      "\xDA\xCD\xC6\xD9\xDF\xF7\xEE\x88\x04\xE1\x62\x00\x08\x0E\xCD\x16"
      "\x37\xAB\xF9\xF5\x14\xAA\x2E\x00\x4E\xF8\x18\x41\x0B\xD9\x6F\x9B"
      "\xFA\xAD\x2B\x54\x56\x2E\x7F\x2C\x3B\x6A\x82\xA1\x7C\x7C\xA6\x8F"
      "\x66\x5E\xE7\xCF\x83\xB9\xEA\xFC\xE2\x31\xD4\x10\xF3\xF4\x22\xEC"
      "\x73\x14\x4F\x94\x78\x79\x8F\x1E\x29\xEA\x5F\x21\x1E\x08\x37\xB8"
      "\xF6\x9A\x2D\xC5\x36\x34\xC1\x97\xDC\x75\xB2\xAD\xD7\xE3\x04\xA7"
      "\xC0\xC9\x1C\x1A\x00\xE9\x2D\x6F\xD6\x8D\xBC\x73\x52\xC0\x8A\xB6"
      "\xBA\x2C\xA6\x7D\x7B\x6F\xF4\x47\x1A\x72\xE9\xB2\x30\x7D\xD4\xD3"
      "\x09\x9C\x65\xB0\xD0\x17\xCF\xFC\xF2\xFF\x46\xD2\xA6\x43\x11\x76"
      "\x2B\xE5\x1D\xE5\xC9\x47\x2F\x4B\x1B\xDD\x9A\xFD\x9D\x20\xB6\x43"
      "\x1A\x64\xE3\x68\xF3\x21\x57\x68\xD4\x04\x8F\xC3\xCE\xAF\xA3\xAB"
      "\x69\xA3\x3C\x34\xBE\x1F\x84\xA8\x0E\x74\xCB\xB7\xE6\xB1\x39\x8D"
      "\x68\x00\x3A\x9B\x9C\xB1\x09\x1C\x7D\x52\x15\x12\xA6\xB0\x83\xD3"
      "\x40\x47\x9B\xE4\x22\xE3\x6E\x30\xC4\xFC\x6F\x4F\xFE\x9F\x51\x14"
      "\x13\x57\xF1\xEB\x25\xF7\x95\x4C\x92\xB6\x3C\xD0\x34\x79\x59\x33"
      "\x20\xBE\xB8\xBF\xE0\x0A\xD2\x77\xBC\x43\x5C\x7D\xFC\xE1\x59\x00"
      "\xDE\x5A\x7D\x44\x11\xAC\x13\xF2\x64\x84\x4F\x5D\xA2\xC4\x36\xD7"
      "\x23\xFA\xF8\xD1\x14\x8D\xF9\xDD\x17\x1D\x52\x41\x22\xF5\x1A\x42"
      "\x39\xFE\x36\xD5\x0A\x10\x01\xD2\xEA\x12\x82\x5A\x48\xD2\x94\x95"
      "\x0A\xF7\xAB\x70\xF7\xF2\x98\x89\xA1\x68\xF9\xE1\xD6\xE1\xBD\x92"
      "\x38\x45\x5F\x19\xE2\xEA\x46\x76\xC5\xC3\xF2\xB4\x9F\x70\x53\x09"
      "\x3F\xB8\x06\x3A\xF3\x46\xC8\x6A\xCD\x0A\xE3\xF0\xAA\x34\xD9\x72"
      "\x98\x34\x23\xD1\x96\x8C\x32\x32\x3B\x00\xA3\x9E\x4F\xED\xBC\x97"
      "\xD4\x4A\x26\x15\x96\x1D\x0E\x36\xB8\xEE\x86\x45\x57\x04\x6D\x2B"
      "\xC0\xDB\x91\x0A\x46\xCE\x7C\x1F\x3C\x3A\x81\x94\x22\x26\x82\x6D"
      "\x83\xBD\x13\x2D\x96\x91\x53\x6C\x26\x0C\x44\xFE\xBD\xEE\xDA\xCC"
      "\xBD\x52\xA6\x11\x3E\x10\x42\x20\x60\xEB\x5F\x5B\x0D\x7C\xBB\x80"
      "\xAC\x2F\xB9\xF9\xD2\x4A\xEB\x54\x80\x60\x62\x85\xE5\x1A\xF0\x30"
      "\x45\xB7\x44\x82\xEF\x3A\x0C\xE0\xE5\x94\xFA\xFD\x2E\xD9\xEB\x8D"
      "\x5A\xC2\xEF\x39\x51\x71\x92\xFA\xDB\xEF\x14\x88\x00\xFF\xE3\xF6"
      "\xB5\x34\x34\x40\xF5\xBB\xC8\xD3\xB5\xBD\xF6\xCF\xC7\xB1\xF9\x18"
      "\x3D\xA2\x74\xEF\x40\xBC\x6B\x39\xF2\xC8\x6E\x00\x64\x78\x52\x88"
      "\x13\xF4\x27\x74\x14\x8F\xCE\x34\x5E\xF9\xE0\x6D\x47\xFC\x38\x6D"
      "\xB0\x03\xED\x6C\xF6\x68\x00\xAC\x2B\xFE\x73\x2C\x94\x9E\x3F\x17"
      "\x0C\x33\xB9\x8F\x33\x34\xDE\x05\x18\xE1\x2B\xB9\x42\x3F\x5F\xA2"
      "\xB4\x1E\xE9\x45\xF3\x38\x43\xBB\x8E\xB0\x0A\x94\x39\xEE\xFF\x9A"
      "\xF4\x2D\x6C\x4B\x66\xB1\x1E\x0F\xC2\x18\x32\xE1\x74\xFF\x90\x94"
      "\xF2\x38\xDD\x56\xDC\x78\x91\x96\xD1\x04\x03\x09\x21\x39\xB2\xD4"
      "\xCC\x2A\xA8\xAB\xE8\x99\x1C\xE7\xE4\x43\x3B\x58\xC1\x59\x54\xE8"
      "\xBD\x9C\x28\xC6\x81\xFC\xAD\x33\x4F\x24\x16\xA0\x47\xD1\x4C\x4D"
      "\x39\x7A\xC1\xF7\x1D\x04\xCF\xE7\xAE\x17\x71\xD9\x37\xDC\x9C\x0A"
      "\x0E\x9D\x0E\x04\xD7\x24\xC1\x50\x0C\x49\xE3\xBC\xCA\x98\x89\x55"
      "\x86\x73\xF1\xC3\x8D\x8F\x99\x34\xF7\x4B\xE7\x69\x0A\xB0\xC1\x2F"
      "\x85\x97\xBF\xC3\xFD\xD0\x62\x75\xB1\xAD\xF3\x04\xF3\xF3\x77\x06"
      "\xAA\x77\x5A\xE7\xEB\x67\x3F\xB5\x40\xA1\x9C\x53\x96\xFD\x85\x53"
      "\x6E\xED\x52\x05\x3B\x6E\x89\xEF\x95\x98\xB6\x66\x34\xD0\x8A\x3F"
      "\x44\xEA\x06\x86\x13\x39\xEF\x20\xAD\xE4\x73\x2C\x61\x77\x10\x3D"
      "\xB9\x0B\xC2\x0C\xFD\xF2\x99\xD8\xB1\x57\x83\x1B\x24\xA6\xA0\xAB"
      "\x97\x3E\xE5\x09\x07\x3F\x43\xED\x12\xE3\x36\xCE\x16\x58\xF2\x78"
      "\x00\x63\xF7\x67\xDC\xD9\x5F\x0D\xAA\x3E\x9A\xA3\x83\x72\xFE\xBA"
      "\x92\xE9\xD4\x22\xF0\x38\x38\x61\xE2\x79\x9B\x5E\x8A\x62\x27\x59"
      "\x84\x71\xC0\xEB\x95\x28\x0D\x34\xCB\xAB\x25\xC6\x3B\xBC\x52\xA5"
      "\xCA\x6B\x93\xCA\x23\x6D\x35\x87\x41\x87\x3E\x48\xB9\xDF\x0E\xFD"
      "\x30\xB8\xD1\xB8\x10\x68\x3D\xBC\x09\x04\x31\x94\x5C\x91\xAF\x6C"
)

def detect_version(cdata, usize, crc, comp_method):

  if comp_method!=0 and comp_method!=8:
    raise Exception("Unknown compression method %d!"%comp_method)

  csize = len(cdata)

  xorsize = 32
  if xorsize>csize: xorsize=csize
  
  #version 1
  tbloff = (csize&0x1F)*32
  for i in range(xorsize):
    cdata[i]^=table1[tbloff+i]

  comp_ok = True
  bytes = cdata.tostring()
  if comp_method == 8:
    try:
      #print "before decompress 1"
      dc = zlib.decompressobj(-15)
      bytes = dc.decompress(bytes)
      ex = dc.decompress('Z') + dc.flush()
      if ex:
          bytes = bytes + ex    
      #print "after decompress 1"
    except:
      #print "exception decompress 1"
      comp_ok = False
  
  for i in range(xorsize):
    cdata[i]^=table1[tbloff+i]
    
  if comp_ok:
    #print "len: %d(%d), crc: %08X(%08X)"%(len(bytes),usize, binascii.crc32(bytes), crc)
    if len(bytes)==usize and crc == binascii.crc32(bytes):
      return 1

  #version 2
  tbloff = (csize&0x3FF)
  for i in range(xorsize):
    cdata[i]^=table2[tbloff+i]

  comp_ok = True
  bytes = cdata.tostring()
  if comp_method == 8:
    try:
      #print "before decompress 2"
      dc = zlib.decompressobj(-15)
      bytes = dc.decompress(bytes)
      ex = dc.decompress('Z') + dc.flush()
      if ex:
          bytes = bytes + ex    
      #print "after decompress 2"
    except:
      #print "exception decompress 2"
      comp_ok = False

  for i in range(xorsize):
    cdata[i]^=table2[tbloff+i]
  
  if comp_ok:
    #print "len: %d(%d), crc: %08X(%08X)"%(len(bytes),usize, binascii.crc32(bytes), crc)
    if len(bytes)==usize and crc == binascii.crc32(bytes):
      return 2

  return None

def main():
  if len(sys.argv)<3:
    print("Usage: pak2zip.py file.pak file.zip")
    sys.exit(1)

  pak = open(sys.argv[1],"rb")
  zip = open(sys.argv[2],"wb")

  version = None

  while True:
    #print("%08X"%pak.tell())
    sig = pak.read(4)
    if len(sig)==0:
      break
    if sig==stringFileHeader2:
      # handle the file header:
      fheaders = sig+pak.read(30-4)
      fheader = struct.unpack(structFileHeader, fheaders)
      fname = pak.read(fheader[_FH_FILENAME_LENGTH])
      if fheader[_FH_EXTRA_FIELD_LENGTH]:
        extra = pak.read(fheader[_FH_EXTRA_FIELD_LENGTH])
      csize = fheader[_FH_COMPRESSED_SIZE]
      cdata = pak.read(csize)
      cdata = array('B', cdata)

      if version==None and fheader[_FH_UNCOMPRESSED_SIZE] > 0:
        version = detect_version(cdata, fheader[_FH_UNCOMPRESSED_SIZE], fheader[_FH_CRC], fheader[_FH_COMPRESSION_METHOD])
        if version==None:
          raise Exception("Unknown AION version")
        print "Version %d found"%version

      if version==1:
        tbloff = (csize&0x1F)*32
        table = table1
      if version==2:
        tbloff = (csize&0x3FF)
        table = table2

      if csize>32: csize=32    
      for i in range(csize):
        cdata[i]^=table[tbloff+i]
      
      print(fname)

      fheaders = stringFileHeader+fheaders[4:]
      zip.write(fheaders)
      zip.write(fname)
      if fheader[_FH_EXTRA_FIELD_LENGTH]:
        zip.write(extra)
      cdata.tofile(zip)
    elif sig==stringCentralDir2:
      # handle the central dir:
      centdirs = sig+pak.read(46-4)
      centdir = struct.unpack(structCentralDir, centdirs)
      fname = pak.read(centdir[_CD_FILENAME_LENGTH])
      #print fname
      extra = pak.read(centdir[_CD_EXTRA_FIELD_LENGTH]+centdir[_CD_COMMENT_LENGTH])
      centdirs = stringCentralDir+centdirs[4:]
      zip.write(centdirs)
      zip.write(fname)
      zip.write(extra)
    elif sig==stringEndArchive2:
      zip.write(stringEndArchive)
      zip.write(pak.read(22-4))
    else:
      raise Exception("Bad signature: %r"%sig)

try:
  main()
except Exception, e:
  print "Error: %s"%e