我有一个php脚本,它以.sql格式备份我的表格,我想用PHP保存gzip格式的文件。我怎样才能做到这一点。PHP中的GZIP压缩
1
A
回答
5
您还可以使用a filter。从手册页:
<?php
$params = array('level' => 6, 'window' => 15, 'memory' => 9);
$original_text = "You SQL queries here...";
$fp = fopen('yourfile.sql.gz', 'w');
stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, $params);
fwrite($fp, $original_text);
fclose($fp);
?>
2
使用Zlib函数。
阅读documentation的细节
从链接页面直取一个例子:
<?php
$filename = tempnam('/tmp', 'zlibtest') . '.gz';
$s = "Only a test, test, test, test, test, test, test, test!\n";
/***** WRITING THE FILE *****/
// open file for writing with maximum compression
$zp = gzopen($filename, "w9");
// write string to file
gzwrite($zp, $s);
// close file
gzclose($zp);
/***** READING IT *****/
// open file for reading
$zp = gzopen($filename, "r");
// read 3 char
echo gzread($zp, 3);
// output until end of the file and close it.
gzpassthru($zp);
gzclose($zp);
/***** ANOTHER WAY TO READ (and print) IT *****/
// open file and print content (the 2nd time).
if (readgzfile($filename) != strlen($s)) {
echo "Error with zlib functions!";
}
?>
希望有所帮助。
2
这里是另一个解决方案,它基本上拿起卡洛斯利马的想法:
<?php
ob_start(); /* start buffering */
echo "your cvs or sql output!";
$content = ob_get_contents(); /* get the buffer */
ob_end_clean();
$content = gzencode($content, 9);
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: Download SQL Export");
header('Content-Disposition: attachment; filename=test.txt.gz');
echo $content;
die();
?>
相关问题
- 1. PHP启用压缩中GZIP
- 2. iOS gzip压缩到PHP
- 3. AppEngine gzip压缩
- 4. 解压缩GZIP
- 5. TYPO3 gzip压缩
- 6. Gzip解压缩
- 7. javascript gzip压缩
- 8. gzip压缩
- 9. WP8 Gzip压缩
- 10. GZIP压缩+ htaccess的紧缩
- 11. Objective-C的gzip压缩的NSData到Python gzip压缩串?
- 12. gzip压缩的文件通过PHP
- 13. gzip压缩的网站(JS,CSS,PHP)
- 14. gzip和webpack压缩
- 15. gzip压缩vs ob_gzhandler
- 16. 解压缩GZIP流
- 17. 启用gzip压缩
- 18. Gzip压缩与MockRestServiceServer
- 19. Apache gzip解压缩
- 20. 透明GZIP压缩
- 21. Protobuf与GZIP压缩
- 22. SignalR wth gzip压缩
- 23. 启用GZIP压缩
- 24. Web Api gzip压缩
- 25. Spring MVC gzip压缩
- 26. Gzip压缩asp.net c#
- 27. 启用gzip压缩
- 28. 在Haskell中解压缩GZip
- 29. ASP.NET MVC WEB API Gzip压缩 - 解压缩
- 30. 与django_compressor压缩文件以gzip压缩