Generally this comes from having an excess of "greens" rather than "browns" in the bin, and potentially too much moisture also.
When this has happened to me I generally put a generous amount of shredded newspaper / junk mail in, and try to let it dry out as much as possible - the fruit flies will then tend to up and leave.
It's probably also worth noting that the flies aren't an inherent problem to the compost per-se, they're just rather unpleasant.
sustainability.stackexchange.com/users/52/berry120
You can make a trap to catch the existing fruit flies with an empty jar, a small plastic sandwich or zip close baggie with square corners, a rubber band and some wine or beer.
Pour a small amount of beer in the bottom of the jar.
Snip off a tiny bit of the corner of the bag, making a small hole.
Push the corner of the bag a few inches down into jar, making a funnel shape, and pull the rest of the bag over edge of the jar.
Put a rubber band around the the rim of the jar sealing the edge of the bag.
The fruit flies crawl in through the funnel, then can't get back out.
I seen similar versions of a fruit fly trap where you punch small holes through the lid of the with a nail.
sustainability.stackexchange.com/users/333/MundaneStudies
I will make this brief:Search for fruit fly lifespan.
An associated article shows an experiment of the effects of sweeteners on fruit flies done by a 6th grader.
He found that Truvia killed fruit flies.
So I tried Truvia in my worm bin and it works!
AND the worms are not harmed.
You should try it and if it works for you, spread the word: No more fruit flies in worm bins.
Steven Goddard
sustainability.stackexchange.com/users/2800/steven goddard
The combination of 4 things worked for me: stop over-feeding and let the compost dry if too wet vacuum the flies
daily cover all the food scraps and put a layer of dry newspaper shred on top of the compost have a trap near the compost to catch some of the flies (yellow container with water + dish soap + sugar works for me, as yellow color and sugar attract flies and dish soap makes them drown)
This method instantly improves your compost situation, but it may take weeks to get rid of all the flies, as your compost probably has some eggs.
sustainability.stackexchange.com/users/6272/Pambi
You could use some food grade DE on your compost pile this will help dry it out and will most certainly kill all the fruit flies.
sustainability.stackexchange.com/users/1443/Get rid of flies
My worm composting bin is infested with fruit flies. What can be done to get rid of the flies?
How to get rid of fruit fly infestation in worm composting bin?
sustainability.stackexchange.com
<composting><vermicomposting>
sustainability.stackexchange.com/questions/537
sustainability.stackexchange.com/users/281/DudeOnRock
Fruit flies can be controlled by adding a generous amount of shredded newspaper, stopping overfeeding the compost and letting it dry out as much as possible - the fruit flies will then tend to up and leave.  Food grade DE will help to dry it out and remove fruit flies. Truvia also works. Fruit flies can also be trapped using a jar, a zip-close bag and some wine or beer. Using a jar trap, shredded paper, Truvia and letting your compost dry out can all address the problem.
Shredded paper, Truvia, letting the compost dry out are viable solutions. You can fashion a trap for fruit flies using an empty jar.