项目迭代开发手记--文件分割存储用例的实现过程(2)
今天图老师小编要向大家分享个项目迭代开发手记--文件分割存储用例的实现过程(2)教程,过程简单易学,相信聪明的你一定能轻松get!
类定义了procedure LoadFile(fileFullName: string);来装载图档文件,同时压缩装载的流。
procedure TLoadBinaryDataToDB.LoadFile(fileFullName: string);begin FStream.LoadFromFile(fileFullName); CompressStream; //对装载的流进行压缩end; procedure TLoadBinaryDataToDB.CompressStream;var iSize: Integer; lDestStream : TMemoryStream; lCompressionStream : TCompressionStream;begin lDestStream := TMemoryStream.Create; lCompressionStream := TCompressionStream.Create(clMax,lDestStream); try iSize := FStream.Size; //获得图像流的原始尺寸 FStream.SaveToStream(lCompressionStream); //将原始图像流进行压缩, //lDestStream中保存着压缩后的图像流 lCompressionStream.Free; FStream.Clear; FStream.WriteBuffer(iSize, SizeOf(iSize)); FStream.CopyFrom(lDestStream, 0);//写入经过压缩的图像流 finally lDestStream.Free end;end; 这样装载后的流就被压缩了,向数据库提交的流就是进过压缩过的。…LoadBinaryDataToDB.LoadFile(lFullFileName); ClientDataSet1.Append; ClientDataSet1.FieldByName('F_ID').Value := 2; ClientDataSet1.FieldByName('F_NAME').Value := lFileFullName; (ClientDataSet1.FieldByName('F_BINARY_DATA')
as TBlobField).LoadFromStream(LoadBinaryDataToDB.FileStream); ClientDataSet1.Post; 类还封装了另一个函数 procedure UnCompressStream(var stream :TMemoryStream)该函数实现了对压缩流的逆向解压过程。 procedure TLoadBinaryDataToDB.UnCompressStream(var stream :TMemoryStream);var DecompressionStream: TDecompressionStream; Buffer: PChar; Count: Integer;begin stream.ReadBuffer(Count, SizeOf(Count)); GetMem(Buffer, Count); DecompressionStream := TDecompressionStream.Create(stream); try DecompressionStream.ReadBuffer(Buffer^, Count);//将被压缩的图像流解压缩,然后存入 Buffer内存块中 stream.Clear; stream.WriteBuffer(Buffer^, Count); //将原始图像流保存至 stream流中 stream.Position := 0; finally FreeMem(Buffer); // 释放内存 end;end; 通过下面这样的调用我们就可以还原图档数据。(ClientDataSet1.FieldByName('F_BINARY_DATA')
as TBlobField). SaveToStream(lBlobStream);LoadBinaryDataToDB.UnCompressStream(lBlobStream);//lBlobStream 为解压后的二进制字节流了对了类进行这样的封装很好的实现了压缩和解压代码的重用,开发人员只要创建该类的对象就可以对流进行相应的处理。到中午的时候我们完成了该类的实现,提交了一个可以使用的版本,由于对该项技术的不熟悉过程中我们还是走了不少弯路,查找了不少技术文档。