Пример #1
0
function downloadZip($format = null){

	$app = \Slim\Slim::getInstance();
	$extension = array(
		'xml' => 'xml',
		'rdfxml' => 'xml',
		'jsonld' => 'json',
		'turtle' => 'ttl',
		'ntriples' => 'nt'
	);
	if(!array_key_exists($format, $extension)){
		$app->flash('error', '指定された出力形式が不正です。');
		throw new RuntimeException('指定された出力形式が不正です。');
	}
	$zip = new ZipArchive();
	$zipFileName = 'data.zip';
	$zipFilePath = $app->config( 'uploadPath' ) ;

	$check = $zip->open($zipFilePath.$zipFileName, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
	if ($check !== true) {
		$app->flash('error', 'zipファイルの作成に失敗しました');
		throw new RuntimeException('zipファイルの作成に失敗しました');
	}

	if($format == 'xml'){
		$zip->addFromString('schema.xsd' , getSchema());
		$xml = new XMLConverter();
		$output = $xml->getXML();
	}else{
		$rdf = new RDFConverter();
		$rdf->parseGraph($format);
		$output = $rdf->output();
	}
	$zip->addFromString('convertedData.'  . $extension[$format] , $output);
	$zip->addFromString('header.xml' , dmdHeaderXML());
	$zip->addFromString('header.ttl' , dmdHeaderRDF());
	$zip->addFromString('mapping.json' , getMapping());

	$zip->close();

	$app->response->headers->set( 'Content-Type', 'application/zip; name="' . $zipFileName . '"' );
	$app->response->headers->set( 'Content-Disposition', 'attachment; filename="' . $zipFileName . '"' );
	$app->response->headers->set( 'Content-Length', filesize($zipFilePath.$zipFileName) );
	$app->response->write( file_get_contents($zipFilePath.$zipFileName));

	unlink($zipFilePath.$zipFileName);
}