Windows 上的 gzstream 通过 mingw:注入 CR +低频
我从网站下载了源代码并构建了它,但是当我运行测试时,所有压缩文件都有 CR+LF 行结尾,而不仅仅是 LF,这使得解压缩的文件与原始文件不同。
我正在查看源代码,但似乎他们已经以二进制模式打开文件:
gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
if ( is_open())
return (gzstreambuf*)0;
mode = open_mode;
// no append nor read/write mode
if ((mode & std::ios::ate) || (mode & std::ios::app)
|| ((mode & std::ios::in) && (mode & std::ios::out)))
return (gzstreambuf*)0;
char fmode[10];
char* fmodeptr = fmode;
if ( mode & std::ios::in)
*fmodeptr++ = 'r';
else if ( mode & std::ios::out)
*fmodeptr++ = 'w';
*fmodeptr++ = 'b';
*fmodeptr = '\0';
file = gzopen( name, fmode);
if (file == 0)
return (gzstreambuf*)0;
opened = 1;
return this;
}
我真的很想使用这段代码,因为它看起来非常干净,并且可以在 mingw gcc 上轻松编译。唯一的问题是这个棘手的事情,如果我能找到解决方案,我就可以放过它。
I downloaded the source from the site and built it but when I run the test, all of the zipped files have CR+LF line endings rather than just LF which makes the unzipped files different from the originals.
I'm looking at the source but it seems like they are already opening the file in binary mode:
gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
if ( is_open())
return (gzstreambuf*)0;
mode = open_mode;
// no append nor read/write mode
if ((mode & std::ios::ate) || (mode & std::ios::app)
|| ((mode & std::ios::in) && (mode & std::ios::out)))
return (gzstreambuf*)0;
char fmode[10];
char* fmodeptr = fmode;
if ( mode & std::ios::in)
*fmodeptr++ = 'r';
else if ( mode & std::ios::out)
*fmodeptr++ = 'w';
*fmodeptr++ = 'b';
*fmodeptr = '\0';
file = gzopen( name, fmode);
if (file == 0)
return (gzstreambuf*)0;
opened = 1;
return this;
}
I'd really like to use this bit of code because it looks very clean and it compiled effortlessly on mingw gcc. The only problem is this tricky business which I could let slide if I can figure out a solution for it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经成功实施了我的解决方法。虽然 gzstream 看起来不错,但我还是硬着头皮写了一些直接使用 zlib 的代码。事实证明它根本还不错,因为 zlib 隐藏了帮助程序,并且
zlib.h
本身也有很多有用的注释。ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen));
非常简单。当然,不再有虚假
0x0D
回车字符的问题!I've successfully implemented my workaround. Though gzstream looks nice I bit the bullet and just wrote some code that directly uses zlib. Turns out it wasn't bad at all because zlib has helpers hidden away in there, and also plenty of helpful comments in
zlib.h
itself.ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen));
is simple enough.And of course, no more problems with spurious
0x0D
carriage-return chars!std::ios::binary 在哪里?
在 UNIX 平台上,它通常是不必要的,因此有些人在不应该的时候省略了它。
Where is std::ios::binary??
On UNIX platforms it is often unnecessary so some people omit it when they should not.