I have still no idea what you are asking for, but your code is obviously wrong.

You read 1024 byte from the end but your writing never ensures it writes 1024 bytes.

Any specific reason you are not using a standard archive format, e.g. ZIP?

Cheers,
_