Zlib压缩放大文件 [英] Zlib compression enlarging file

查看:47
本文介绍了Zlib压缩放大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 iPhone 应用程序中使用 zlib 将文本文件压缩为 gzip 文件作为测试.我正在使用以下代码

I'm trying to use zlib in an iPhone app to compress a text file into a gzip file as a test. I am using the following code

const char *s = [[Path stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@".%@", [Path pathExtension]] withString:@".gz"] UTF8String];
gzFile *fi = (gzFile *)gzopen(s, "wb");
const char *c = readFile(Path.UTF8String);
gzwrite(fi, c, strlen(c));
gzclose(fi);

其中 readFile() 返回使用 fgets() 函数从文件中获取的 const char*.问题是,当我使用它来压缩文件时,它并没有压缩它,而是 gzip 文件比原始文件大.例如,我有一个 90 字节的文本文件,使用此方法后 gzip 的大小为 98 字节.为什么 gzip 不小于原始文件?

where readFile() returns a const char* that was obtained from the file using the fgets() function. The problem is, when I use this to compress a file, it doesn't compress it, but instead the gzip file is larger than original file. For example, I have a text file that is 90 bytes, and after using this method the size of the gzip is 98 bytes. Why isn't the gzip smaller than the original file?

推荐答案

GZip 格式包括固定大小的标头信息.因为你压缩的数据太少,头信息比你节省的空间大.

The GZip format includes fixed-size header information. Because you are compressing so little data, the header information is larger than the space you are saving.

90 字节通常不值得压缩.

90 bytes is generally not worth compressing.

http://www.gzip.org/zlib/rfc-gzip.html#header-预告片

这篇关于Zlib压缩放大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆