C# Is there is any way to read .AFP file and extract the TLEs?

1.2k Views Asked by At

I need to read a AFP file and extract it's TLEs such as account number, client full name... The AFP file is well formated and have a very clear structure. I prefer not to add any external library.

2

There are 2 best solutions below

0
owairc On

AFP is an easy format, it's composed of structured fields, your first step is decoding them, download this: "Mixed Object Document Content Architecture Reference" read first 50 pages and write code to split afp into structured fields, in order to create an easy dump of your file.

good job

0
OffColour On

It's in Python, I wrote it a VERY long time ago and haven't touched it since, but it worked. I would add some comments but I can't remember a thing about it. Might be of some use.

import datetime
start = datetime.datetime.now() 

statementlist=[]
ptxlist = []

with open('C:\\temp\\AFP\\bigfile', 'rb') as fs:
    bngcmd = b'\xA8\xAD'
    engcmd = b'\xA9\xAD'
    ptxcmd = b'\xEE\x9B'
    tlecmd = b'\xA0\x90'
    inDoc = False

    while True:
        # 'Read SFI header info
        byte = fs.read(6)
        if not byte:
            break
        # 'get command info
        SFICommand = byte[4:6]
        SFISize = (byte[1]<<8) + byte[2]
        if not inDoc and SFICommand == bngcmd:
            inDoc = True
            ptx = bytearray()
            fs.read(SFISize-5)
        elif inDoc and SFICommand == engcmd:
            inDoc = False
            ptxlist.append(ptx)
            currentsize = 0
            fs.read(SFISize-5)
        elif inDoc and SFICommand == ptxcmd:
            fs.read(3)
            ptx.extend(bytearray(fs.read(SFISize-8)))
        else:
            fs.read(SFISize-5)

inptxcmd = b'\x2B\xD3'
for ptx in ptxlist:
    i = 0
    ptxlength =len(ptx)
    utfstring=''
    while i < ptxlength:
        if ptx[i:i+2] == inptxcmd:
            i+=2

        ptxsize = ptx[i]
        ptxcmd = ptx[i+1]
        
        if ptxcmd == 0xDA or ptxcmd == 0xDB:
            utfstring += ptx[i+2:i+ptxsize].decode("EBCDIC-CP-BE").strip() + '\u0009'

        i+=ptxsize

    statementlist.append(utfstring + '\n')

with open('C:\\temp\\AFP\\pythonout.txt', 'w',encoding='ASCII') as outfile:
    for statement in statementlist:
        outfile.write(statement)