将WriteBinaryStream压缩为INI文件? [英] WriteBinaryStream compressed to INI file?

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

问题描述

在Delphi 10.4中,我尝试将有效的(压缩后的)保存到INI文件中,尝试复制

In Delphi 10.4, I try to save a valid TPicture compressed to an INI file, trying to replicate the ZLibCompressDecompress example from the documentation:

procedure TForm1.SavePictureToIniFile(const APicture: TPicture);
// https://stackoverflow.com/questions/63216011/tinifile-writebinarystream-creates-exception
var
  LInput: TMemoryStream;
  LOutput: TMemoryStream;
  MyIni: System.IniFiles.TMemIniFile;
  ThisFile: string;
  LZip: TZCompressionStream;
begin
  if FileSaveDialog1.Execute then
    ThisFile := FileSaveDialog1.FileName
  else EXIT;

  LInput := TMemoryStream.Create;
  LOutput := TMemoryStream.Create;
  LZip := TZCompressionStream.Create(clDefault, LOutput);
  try
    APicture.SaveToStream(LInput);
    LInput.Position := 0;
    //LOutput.Position := 0;
    LZip.CopyFrom(LInput, LInput.Size);

    MyIni := TMemIniFile.Create(ThisFile);
    try
      MyIni.WriteBinaryStream('Custom', 'IMG', LOutput);
      MyIni.UpdateFile;
    finally
      MyIni.Free;
    end;
  finally
    LInput.Free;
    LOutput.Free;
    LZip.Free;
  end;
end;

但是流未保存在INI文件中.生成的INI文件仅包含以下几行:

But the stream is not saved in the INI file. The resulting INI file contains only these lines:

[自定义]
IMG =

[Custom]
IMG=

那么如何将压缩后的流保存在INI文件中?

So how can I save the compressed stream in the INI file?

推荐答案

您需要在LZip.CopyFrom行的之后设置LOutput.Position := 0 ,即在之前

MyIni.WriteBinaryStream('Custom', 'IMG', LOutput);

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

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