2013-10-16 29 views
-1

對於創建xml文件,我使用DOMObject。但是所有創建的xml文件都很大,會帶來很大的帶寬。使用gzip壓縮創建的XML文件

這是我保存XML文件的方式

$xml->save($filename); 

我怎麼可以添加一個gzip壓縮?

編輯: 這段代碼不起作用,因爲它會創建一個空文件

$gz = gzopen($filename,'w'); 
gzwrite($gz, $xml); 
gzclose($gz); 
+0

http://www.php.net/manual/en/function.gzwrite.php – cmorrissey

+0

的gzopen()混淆了我,因爲直到保存過程中文件不存在。 –

+0

可能重複[如何使用PHP創建.gz文件?](http://stackoverflow.com/questions/6073397/how-do-you-create-a-gz-file-using-php) – hakre

回答

0

這裏只是一個.zip實現。

$file = '...'; 
$folder = 'folder'; 

$zip  = new ZipArchive(); 
$fileName = "some_file.zip"; 

if ($zip->open($fileName, ZIPARCHIVE::CREATE)!==TRUE) { 
    throw new Exception('Can not create zip file.'); 
} 

$zip->addEmptyDir($folder); 

if (file_exists($file)) { 
    $zip->addFile($file, $folder . "/" . basename($file)); 
} 

$zip->close(); 
+1

I用$ xml-> save('compress.zlib://'。$ filename。'.gz')解決它。 –

+0

@Ronny Linsener:這是正確的方法,你會發現這個問題,並在這裏回答:[你如何使用PHP創建一個.gz文件?](http://stackoverflow.com/q/6073397/367456) - 我因此投票關閉副本以使關係更緊密。 – hakre