Image caption Mr Dotcom faces 20 years if convicted in the US

A court in New Zealand has ruled that Megaupload boss Kim Dotcom can sue the country's foreign intelligence service for illegally spying on him.

The court rejected a challenge to an earlier ruling allowing him to sue the Government Communications Security Bureau (GCSB) over his treatment.

GCSB worked with US officials to investigate Mr Dotcom over allegations of online piracy and money laundering.

Mr Dotcom, who denied the charges, is fighting extradition to the US.

US officials allege that pirated movies and other content shared through his Megaupload site cost copyright holders more than $500m (£322m) in lost earnings, making it one of the biggest cases of its kind.

He faces a jail sentence of up to 20 years if convicted in the US.

Mr Dotcom's home and the offices of Megaupload were raided in January 2012 as part of the FBI investigation. The GCSB was asked to spy on Mr Dotcom by police prior to the raid.

The agency is only allowed to spy on those with no right to reside in New Zealand. The case prompted an apology from Prime Minister John Key to Mr Dotcom - a German national with New Zealand residency - last year.

The New Zealand attorney general had asked the appeals court to exclude the GCSB from Mr Dotcom's lawsuit seeking compensation, after the high court ruled last year that the agency could be held liable for illegally spying, but the court rejected the bid.

The appeals court also said Mr Dotcom could access some GCSB evidence related to his case.

"This will strengthen our case in so far as GCSB remains a party to the proceedings," William Akel, a lawyer for Mr Dotcom, said.