This is really a programming question, and should be asked at the Tridion specific site at tridion.stackexchange.com
In order to achieve this I suggest reading the API manual, and look for the AddBinary()
method. This will allow you to publish the file with a specific filename, and bind it to a Structure Group. You will need to make Structure Groups to match your Folder structure in order to fully achieve what you want.