First, as a preventative, i wash fruits when they come in the hose to remove the fruit fly eggs. When I get them anyway, I leave the lid of my compost container ajar. When a bunch have congregated inside, I quickly snap the lid down, then take it outside. Even if I don't go all the way to the compost pile, the flies will depart when the lid is opened and not return to the house. It takes several tries, but works for me.
Officially Recognized Stretchpert in Self-Sufficient Living