Defending champion Federer only landed 42 percent of his first serves during the first set but an early break helped him take the lead against an awkward opponent cheered on by a home crowd.
"He had a clear game plan with a lot of variation so I never really got into a rally," Federer told reporters. "He was also unpredictable for me."
Another early break in the second set combined with an improved service ratio earned top seed Federer a place in the quarter-finals where he faces Frenchman Julien Benneteau, who he beat twice on the grass courts of Wimbledon last year.
Federer won at the London Olympics in straight sets but had to overcome a two-set deficit weeks earlier on his way to his 17th grand slam title.
"It will be tough as he (Benneteau) serves and returns well, while he played convincing matches in the first two rounds," the Swiss added.
Second seed Juan Martin Del Potro, beaten by Federer in last year's final, kept his best tennis for the big points as he struggled before beating wild card Ernests Gulbis 7-6 6-3.
"The first set was tough and quite a battle and I had some luck on the final point in the tiebreak," the Argentine told reporters.
Latvian Gulbis impressed with his serve in the second set when he hit five straight aces, but a break in the eight game was enough for Del Potro to clinch the match.
"Matches like this are decided on one or two points per set, and I was at my best in those moments," Del Potro added.
In the quarter-final, Del Potro will meet Finn Jarkko Nieminen who needed three sets to overcome German qualifier Matthias Bachinger 6-3 5-7 6-3.
Fourth seed Richard Gasquet suffered a 6-4 6-4 defeat against Marcos Baghdatis from Cyprus.
Gasquet had a perfect start to the year with titles in Doha and Montpellier, reaching the fourth round at the Australian Open inbetween, but an early break in each set was too much for the Frenchman. Baghdatis next meets Grigor Dimitrov. (Editing by Mark Meadows; firstname.lastname@example.org; +44 20 7542 7933; Reuters Messaging:; email@example.com)