C++ Builder中TZipFile.Read报错,求使用示例与问题排查
TZipFile::Read调用报错E2285的解决方法
问题代码
String fName = "myInfo.txt"; std::auto_ptr<TMemoryStream> input(new TMemoryStream); std::auto_ptr<TStringList> sFile(new TStringList); std::auto_ptr<TZipFile> zip(new TZipFile()); TZipHeader fHeader; zip->Open(file, zmRead); zip->Read(fName, input.get(), fHeader); ...
错误信息
E2285 Could not find a match for 'TZipFile::Read(UnicodeString,TMemoryStream *,TZipHeader)'
问题原因
你调用的TZipFile::Read重载不存在,TZipFile::Read没有接收UnicodeString, TMemoryStream*, TZipHeader这三个参数的版本。要读取压缩包中的文件并获取文件头,需要先通过文件名找到该文件在压缩包中的索引,再用索引调用对应的重载方法。
正确使用示例
方式1:通过索引读取(带文件头)
String fName = "myInfo.txt"; std::auto_ptr<TMemoryStream> input(new TMemoryStream); std::auto_ptr<TZipFile> zip(new TZipFile()); TZipHeader fHeader; zip->Open(file, zmRead); try { // 查找目标文件在压缩包中的索引 int fileIndex = zip->IndexOf(fName); if (fileIndex != -1) { // 读取文件内容到MemoryStream,同时获取文件头 zip->Read(fileIndex, input.get(), fHeader); // 操作流前需将位置重置到开头 input->Position = 0; // 示例:将流内容读取到TStringList std::auto_ptr<TStringList> sFile(new TStringList); sFile->LoadFromStream(input.get()); // 后续业务处理... } else { ShowMessage("压缩包中未找到文件:" + fName); } } __finally { // 确保压缩包被关闭 zip->Close(); }
方式2:直接通过文件名读取(不带文件头)
如果不需要获取文件头信息,可以直接使用接收文件名和流的重载:
String fName = "myInfo.txt"; std::auto_ptr<TMemoryStream> input(new TMemoryStream); std::auto_ptr<TZipFile> zip(new TZipFile()); zip->Open(file, zmRead); try { // 直接通过文件名读取,成功返回true if (zip->Read(fName, input.get())) { input->Position = 0; // 后续处理... } else { ShowMessage("读取文件失败"); } } __finally { zip->Close(); }
TZipFile::Read常用重载说明
void Read(int Index, TStream* Stream):通过文件索引读取内容到指定流,不返回文件头void Read(int Index, TStream* Stream, TZipHeader &Header):通过索引读取内容,同时输出文件头信息bool Read(const String &FileName, TStream* Stream):通过文件名读取内容,成功返回true,失败返回false
内容的提问来源于stack exchange,提问作者André
相关产品推荐
相关产品推荐

