You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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é

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 19:32:51