Megaupload boss Kim Dotcom has won the right to sue New Zealand's foreign intelligence agency for illegally spying on him prior to his arrest as part of a US investigation into alleged online piracy.

The founder of the file-sharing website was also cleared by the High Court in Wellington to sue the Government Communications Security Bureau (GCSB) for its part in the unlawful January raid on his rented Auckland mansion.

The 38-year-old German national is accused of the biggest copyright infringement in US history, for which the US Department of Justice is trying to extradite him to face charges of internet piracy.

The US authorities allege Megaupload and related file-sharing sites netted more than US$175m (£109m) by offering pirated copies of films, TV shows and other content.

Mr Dotcom denies the charges, saying Megaupload.com simply provides online storage services and is not responsible for the files stored on the site.

His US-based lawyer Ira Rothken hailed the decision as a major victory in the internet tycoon's fight against extradition.

"Today's @KimDotcom judgment shows the NZ democracy works as the judiciary orders discovery & acts as a check & balance on illegal gov(ernment) spying," he tweeted.

In September, it was revealed that the GCSB spied on Mr Dotcom before police raided his Auckland mansion, in contravention of the rules.

The bureau can only bug foreign organisations or people, not New Zealand citizens or permanent residents. Mr Dotcom is a New Zealand resident.