Project Iterative Development Instruction - Implementation Process of File Segmentation Storage Case (2)
In the development of the next morning, we extended the use case of the iterative development of the first day. We consider adding the compressed stream to reduce the storage burden of data.
Iteration 2:
Use: The binary stream submitted to the database is compressed; then the decompression operation is performed when extracted from the database to obtain the original image file data.
By querying information We find the Zlib library of Delphi to support the compression and decompression of the word stream so that we can implement compression and decompression functions as long as the TcompressionStream and TDecompressionStream of this class can make compression and decompression functions. For better packaging, this process is convenient for project group reuse We have encapsulated compression and decompression.
We define the TLOADBINARYDATATATODB class to encapsulate compression and decompression functions for use.
Category defines Procedure LoadFile (filefullname: string); to load the file file while compressed the loaded stream.
Procedure tloadbinaryDataodb.loadfile (filefullname: string);
Begin
FStream.loadFromFile (filefullname);
CompressStream; / / / / compress the loaded stream
END;
Procedure tloadbinarydatodb.compressstream;
VAR
ISIZE: INTEGER;
LDestStream: TMemoryStream;
LCompressionStream: TcompressionStream;
Begin
LDestStream: = TMEMORYSTREAM.CREATE;
LCompressionStream: = TcompressionStream.create (CLmax, LDestStream);
Try
ISIZE: = fstream.size; // Get the original size of the image stream
FStream.savetostream (LCompressionStream); // Compress the original image stream,
// LDestStream saves a compressed image stream
LCompRessionsTream.free;
FStream.clear;
FStream.writeBuffer (isize, sizeof (isize);
FStream.copyFrom (LDestStream, 0); // Write a compressed image stream
Finally
LDestStream.free
END;
END;
This stream after loading is compressed, and the flow submitted to the database is compressed.
...
LoadbinaryDataodb.loadfile (lfullfilename);
ClientDataSet1.Append;
ClientDataSet1.fieldByName ('f_id'). Value: = 2;
ClientDataSet1.fieldByname ('f_name'). Value: = lfilefullname;
(ClientDataSet1.fieldByname ('f_binary_data') as tblobfield) .loadFromstream (loadingbinaryDataodb.filestream);
ClientDataSet1.post;
The class also encapsulates another function Procedure UncompressStream (Var Stream: TMEMORYSTREAM) This function implements the reverse decompression process of the compressed stream.
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); // 解 压 压 图像 图像 图像, then store into the buffer memory block
stream.clear;
Stream.WriteBuffer (buffer ^, count); // Save the original image stream to the Stream stream
Stream.position: = 0;
Finally
FreeMem (buffer); // Release memory
END;
END;
We can restore the file data by calling us below.
(ClientDataSet1.fieldByname ('f_binary_data') as tblobfield). SaveTostream (LBLOBSTREAM);
LBLOBSTREAM.POSITON: = 0;
LoadbinaryDataodb.uncompressStream (LBLOBSTREAM);
// lblobstream is the decompression binary byte stream.
For the class, the encapsulation of the class has realized the reuse of compression and decompression code, and developers can perform corresponding processing as long as the objects of this class can be treated.
At noon, we completed the implementation of this class and submitted a version that can be used. Due to the unfamiliar process of the technology, we still got a lot of detours, and found a lot of technical documents.