关于 ZLib 的用法。使用中出现错误!(50分)

  • 主题发起人 主题发起人 question
  • 开始时间 开始时间
Q

question

Unregistered / Unconfirmed
GUEST, unregistred user!
使用压缩过程编码一个文件,没有问题,问题出在我用解压缩过程还原压缩过程产生的文件
时,出现错误,错误应该出在 ZStream := TDecompressionStream.Create(OutStream);
具体代码附后,如何解决?

uses ZLib;

压缩过程:

procedure TForm1.Button4Click(Sender: TObject);
var
InStream, OutStream: TStream;
DestStream: TFileStream;
ZStream: TCompressionStream;
TempFile : String;
S:String;
begin
S := 'D:/ls01.xml';
TempFile := 'd:/school.uc';
InStream := TFileStream.Create(S, fmOpenRead);
OutStream := TMemoryStream.Create;
try
ZStream := TCompressionStream.Create(clDefault, OutStream);
ZStream.CopyFrom(InStream,0);
ZStream.free;
except
InStream.Free;
ShowMessage('编码上报文件失败!');
Exit;
end;
try
DestStream := TFileStream.Create(TempFile, fmCreate);
except
OutStream.Free;
ShowMessage('创建临时文件失败!');
Exit;
end;
DestStream.CopyFrom(OutStream, 0);
DestStream.Free;
InStream.Free;
OutStream.Free;
end;

解压缩

var
SourceDBName : String;
InStream, OutStream : TStream;
ZStream: TDecompressionStream;
DestStream: TFileStream;
TempFile : String;
begin
//开始导入
//检验文件的合法性
SourceDBName := Trim(Edit1.Text);
TempFile := 'c:/input.xml';
OutStream := TMemoryStream.Create;
InStream := TFileStream.Create(SourceDBName, fmOpenRead);
OutStream.CopyFrom(InStream,0);
try
ZStream := TDecompressionStream.Create(OutStream);
//应该是上面这句执行无效,导致下面这句出现错误。
ShowMessage(InttoStr(zStream.Size));
OutStream.CopyFrom(ZStream, 0);
ZStream.Free;
except
SetScreenInfo;
InStream.Free;
OutStream.Free;
ShowMessage('解码上报文件失败!');
Exit;
end;
try
DestStream := TFileStream.Create(TempFile, fmCreate);
except
SetScreenInfo;
InStream.Free;
OutStream.Free;
ShowMessage('创建临时文件失败!');
Exit;
end;
DestStream.CopyFrom(OutStream, 0);
DestStream.Free;
end;
 
当然错了,你没看zlib里面的注释吗?zstream是禁止读的。
你用OutStream.CopyFrom(ZStream, 0);不就错了吗。

改成:

OutStream := TMemoryStream.Create;
InStream := TFileStream.Create(SourceDBName, fmOpenRead);
///////////OutStream.CopyFrom(InStream,0);
try
ZStream := TDecompressionStream.Create(OutStream);
//ShowMessage(InttoStr(zStream.Size));
ZStream.LoasFromStream(InStream);
ZStream.Free;
//然后解压后的数据就放在outstream里了。
except
;
end;
 
接受答案了.
 
onedolph 的方法不对呀!!!!
ZStream := TDecompressionStream.Create(OutStream);
zstream怎么会有LOADFROMSTREAM???
 
后退
顶部