DECLARE FUNCTION Dec2Bin$ (nn!)
DECLARE FUNCTION Bin2Dec! (nn$)
'Syntax: AR -{l|a|r} A:archive [F:file]
dd$ = "-a A:NEW0.AR f:*.exe"
dd$ = UCASE$(dd$) + " "
IF INSTR(UCASE$(dd$), "-L") <> 0 THEN Mde = 1
IF INSTR(dd$, "-") = 0 OR INSTR(UCASE$(dd$), "-A") <> 0 THEN Mde = 2
IF INSTR(UCASE$(dd$), "-R") <> 0 THEN Mde = 3
IF INSTR(UCASE$(dd$), "A:") = 0 THEN Arc$ = "NEW.AR" ELSE Arc$ = MID$(dd$, INSTR(UCASE$(dd$), "A:") + 2, (INSTR(INSTR(UCASE$(dd$), "A:") + 2, dd$, " ") - INSTR(UCASE$(dd$), "A:")) - 2)
IF INSTR(UCASE$(dd$), "F:") = 0 THEN Fil$ = "*.*" ELSE Fil$ = MID$(dd$, INSTR(UCASE$(dd$), "F:") + 2, (INSTR(INSTR(UCASE$(dd$), "F:") + 2, dd$, " ") - INSTR(UCASE$(dd$), "F:")) - 2)
'PRINT Mde, Arc$, Fil$
ON Mde GOTO ListFile, AddFile, RemoveFile

ListFile:
DIM Hdr AS STRING * 3
PRINT "Listing " + Fil$ + " from " + Arc$ + "."
xx1 = FREEFILE
OPEN Arc$ FOR BINARY AS #xx1
IF LOF(xx1) = 0 THEN CLOSE xx1: PRINT "Invaild Archive.": GOTO LastLn
GET #xx1, , Hdr
IF Hdr <> "TaR" THEN CLOSE xx1: PRINT "Invaild Archive.": GOTO LastLn
DIM Bytes AS STRING * 1
DIM Byte AS STRING * 1000
DO UNTIL INSTR(Byte, "\*") <> 0
GET #xx1, , Bytes
IF INSTR(Bytes, "\*") <> 0 THEN lstChr = INSTR(Bytes, "\*")
LOOP
CLOSE xx1
GOTO LastLn
AddFile:
PRINT "Creating " + Arc$ + " from " + Fil$ + "."
ss$ = DIR$(Fil$)
DO
IF ss$ = "" THEN EXIT DO
xx2 = FREEFILE
OPEN ss$ FOR BINARY ACCESS READ AS #xx2
ts$ = ts$ + CHR$(LEN(ss$)) + ss$ + CHR$(LEN(Dec2Bin$(LOF(xx2)))) + Dec2Bin$(LOF(xx2))
CLOSE xx2
pstss$ = ss$: ss$ = DIR$
LOOP UNTIL ss$ = "" OR pstss$ = ss$
Hdr$ = "TaR"
DIM Byte AS STRING * 1000
xx1 = FREEFILE
OPEN Arc$ FOR BINARY AS #xx1
PUT #xx1, , Hdr$
ts$ = ts$ + "\*"
PUT #xx1, , ts$
ss$ = DIR$(Fil$)
DO
IF ss$ = "" THEN EXIT DO
xx2 = FREEFILE
OPEN ss$ FOR BINARY ACCESS READ AS #xx2
FOR q = 1 TO LOF(xx2)
GET #xx2, , Byte
IF q + LEN(Byte) > LOF(xx2) THEN g$ = MID$(Byte, 1, (LOF(xx2) - q) + 1): PUT #xx1, , g$ ELSE PUT #xx1, , Byte
q = (q + LEN(Byte)) - 1
NEXT q
PRINT ss$
CLOSE xx2
pstss$ = ss$: ss$ = DIR$
LOOP UNTIL ss$ = "" OR pstss$ = ss$
CLOSE xx1


GOTO LastLn
RemoveFile:
PRINT "Removing " + Fil$ + " from " + Arc$ + "."
LastLn:

FUNCTION Bin2Dec (nn$)
FOR q = 1 TO LEN(nn$)
f1$ = STRING$(2 - LEN(HEX$(ASC(MID$(nn$, q, 1)))), "0") + HEX$(ASC(MID$(nn$, q, 1))) + f1$
NEXT q
Bin2Dec = VAL("&H" + f1$)
END FUNCTION

FUNCTION Dec2Bin$ (nn)
f$ = HEX$(nn)
f1$ = STRING$(LEN(f$) MOD 2, "0") + f$
FOR q = 1 TO LEN(f1$) STEP 2
f2$ = CHR$(VAL("&H" + MID$(f1$, q, 2))) + f2$
NEXT q
Dec2Bin$ = f2$
END FUNCTION

