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.
Asked
Active
Viewed 989 times
0
-
Then you have to build it yourself. IBM is offering a specification for it. I have built a Reader/Writer for AFP that is extendible. It’s a lot of work, but doable. – Legacy Code Jul 21 '20 at 08:32
-
If there is no solution for that, do you have a suggestion for a clean external library? – Hichem Fakhfakh Jul 22 '20 at 08:41
-
Back then there was no good functional ones that also works with large AFP files, thats why I created my own. There are several AFP implementations though nowadays, but I haven’t tested them and can’t really tell how good they are. e.g. https://github.com/Shaosil/AFPParser – Legacy Code Jul 22 '20 at 09:49
2 Answers
0
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

owairc
- 1,890
- 1
- 10
- 9
0
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)

OffColour
- 33
- 1
- 4