A Brooklyn janitor was arrested for allegedly raping a former student Saturday afternoon inside the middle school where he was working, police said.

Hernan Zapata, 51, allegedly attacked the 17-year-old girl inside the janitor's office of a Fort Greene public school at about 2:45 p.m., police said. He was arrested roughly an hour later.

Zapata, who lives in Queens, was charged with rape, criminal sexual act, sex abuse, sexual misconduct, unlawful imprisonment and assault.

Police said Zapata has no prior arrests. He could not be reached for comment yesterday and it could not be determined if he had retained an attorney.