The sign outside the National Security Agency campus in Fort Meade, Md. (Patrick Semansky/AP)

A federal judge found Friday that a former National Security Agency contractor accused of carrying out what is thought to be the largest theft of classified secrets in U.S. history posed a flight risk and ordered that he continue to be held in jail.

U.S. Magistrate Judge A. David Copperthite ruled that Harold T. Martin III should not be released pending trial, despite the impassioned arguments of his defense attorney that the computer technology expert is a patriot who intended no harm to his country and suffered from a “compulsive disorder” that led him to steal classified material over a 20-year period.

Defense attorney James Wyda conceded that Martin took the material, but he stressed that “there’s no evidence that Hal shared this information with anyone” or that he intended to pass it to a foreign government.

“There is nothing to indicate that Hal Martin is a traitor,” Wyda told the judge, as Martin, a heavy-set man wearing gray-striped jail garb, sat quietly at the defense table. “What we see is an individual who is a collector.”

He told the court that his client is not like Edward Snowden, the former NSA contractor who gave classified material on U.S. surveillance programs to journalists, or Aldrich Ames, a onetime CIA officer who was recruited by the Soviet Union and betrayed agents working for the United States.

Wyda portrayed Martin, 51, as a “voracious learner committed to being excellent at his work” who gathered information to improve his knowledge. “What began as an effort to be better at his job,” Wyda said, became over the years a compulsion.

“Frankly,” he said, “the mental health component is the only explanation.”

Martin was charged in a sealed complaint in August with felony theft of government property and the unauthorized removal of classified materials, a misdemeanor. The complaint was unsealed this month when word of his arrest leaked out.

In a memo filed this week, the government said Martin had taken at least 50 terabytes of digital data, roughly the equivalent of 500 million pages of documents. He also took six banker’s boxes worth of paper documents, prosecutors said, many of which were lying open in his home or in his car. And investigators found dozens of computers, thumb drives and other digital storage devices that belonged to the government, prosecutors said.

Prosecutors also have said that Martin had an “arsenal” of weapons in his home and car, including an assault-rifle-style tactical weapon, and that though he lacks a valid U.S. passport, he could still flee to a foreign government that might want to help him. Prosecutors said he has communicated with unnamed people in Russian and in June downloaded information on Russian and other languages.

The detention hearing in U.S. District Court here marked Martin’s first public appearance since his Aug. 27 arrest at his home in Glen Burnie, Md. Sitting in the audience were his wife, Deborah Shaw, and his brother, who traveled from Florida. Wyda pointed to their presence as evidence of Martin’s support network and another reason he would not try to flee if released.

The judge said he agreed with Wyda that there were “some serious mental health” problems at play. At the same time, he said, Martin is a “highly educated man, certainly highly capable.” He has a master’s degree and was working on a PhD at the University of Maryland Baltimore County.

“What I see here is a person who may be two persons,” Copperthite said, “a person who may be a smart guy . . . [wanting] to help his colleagues, but at the same time, a person who is walking out the back door’’ with data “that he knows he has no business” taking.

He noted that Martin kept the material in an unlocked storage shed — “information that many enemies of the United States, I’m sure, would love to explore.”

Copperthite said “the preponderance of evidence” indicates that Martin poses a flight risk and so he should remain in jail pending trial.

On Thursday, prosecutors said they expected additional charges to be brought, including “violations of the Espionage Act.”

That 1917 law makes “willful” retention of “national defense information” a felony punishable by up to 10 years in prison per count.

Assistant U.S. Attorney Zachary A. Myers told the court that Martin “now knows that the evidence supports” multiple violations of the Espionage Act, which could result in a sentence, if he is convicted, as high as 30 years to life. That, Myers said, gives him “significant incentive to flee.”

After the hearing, which lasted slightly more than an hour, Martin’s attorneys Wyda and Deborah Boardman said they would appeal the ruling.

“Hal Martin and his family are disappointed with today’s ruling,” they said in a statement. “We do not believe Hal Martin is a danger to the community or to his country. Hal is no risk of flight. Hal Martin loves America. And he trusts our justice system. This is an early step in a long process.”