用minizip/zlib从zip存档中加载文本文件,文件末尾有垃圾字符

Loading text file from zip archive with minizip/zlib, garbage characters at end of file

本文关键字:文件 字符 文本 加载 zlib minizip zip      更新时间:2023-10-16

我试图从zip文件内加载着色器源,这是一个用记事本创建的纯文本文件。加载代码如下(从下面的代码片段中删除了错误检查代码):

std::string retrievestringfromarchive(std::string filename)
{
//data is the zip resource attached elsewhere
unz_file_info info;
Uint8* rwh;
unzLocateFile(data, filename.c_str(), NULL);
unzOpenCurrentFile(data);
unzGetCurrentFileInfo(data, &info, NULL, 0, NULL, 0, NULL, 0)
rwh = (Uint8*)malloc(info.uncompressed_size);
unzReadCurrentFile( data, rwh, info.uncompressed_size );
//garbage at end of file
const char* rwh1 = reinterpret_cast<char*>(rwh);
std::stringstream tempstream(rwh1);
std::string tempstring = tempstream.str();
free(rwh);
return tempstring;
}

返回的字符串输出如下:

//FRAGMENT SHADER    
#version 120    
//in from vertex shader    
varying vec2 f_texcoord;    
varying vec4 f_color;
uniform sampler2D mytexture;
void main(void)
{
    gl_FragColor = texture2D(mytexture, f_texcoord) * f_color;
}
//endfile««««««««îþîþ

指出:

  • 我检查了信息结构,压缩和未压缩的大小与7zip

  • 中的信息匹配。
  • 缓冲区"rwh"本身在末尾有垃圾字符,当使用gdb

  • 检查时
  • 我是在Win7 64位,使用代码块和TDM-GCC-32 4.8.1编译

  • "//endfile"注释巧妙地避免了gl shader编译问题,但这是必须的

rwh = (Uint8*)malloc(info.uncompressed_size);
unzReadCurrentFile( data, rwh, info.uncompressed_size );

我非常怀疑unzReadCurrentFile在缓冲区中添加了0终止符-无论如何都没有空间-并且您正在使用指针作为0终止的字符串。

如果将缓冲区解释为字符串确实有意义,您可以这样做:

std::string tempstring(rwh1, info.uncompressed_size);

解压缩器给你一个解压缩的数据块——但它不知道数据是纯文本,也不知道你打算把它用作c语言字符串。因此,它不会在末尾附加一个终止NUL(零)字符。这一切。只需复制给定数量的字符,并且不假设数据块以零结尾。