I. File cutting/combination principle
The so-called file cutting, is to cut a file into a number of small files. The idea is to read a file object as a "file stream," and then save it as a file by a certain block size. A combination of files is a combination of several files into one file. This is the principle, for example, used by the packaging tool (Wise). If you are familiar with the concept of "flow", then it is easy to implement, the following is the implementation of the steps.
Second, realize
Create a new project in Delphi, then add two button in the form, one edit, one opendialog, one savedialog, then change the caption of BUTTON1 to "cut", BUTTON2 caption to "Combination" (Note: The properties of other controls are abbreviated). Double-click BUTTON1 and BUTTON2 to write the following code:
//cutting file
procedure Tform1.button1click (sender:tobject);
var
I, wantedfragsize, Realtowrite:integer;
//Declaration of two file stream objects
//instream is an input source file stream object, outstream a file stream object for output
instream, Outstream:tfilestream;
s:string;
begin
if Opendialog1.execute then
begin
savedialog1.title: = ' Enter the cut file name you want to save ';
If Savedialog1.execute then
begin
wantedfragsize: = Strtoint (Edit1.text); Define cutting block Size
I: = 0;
//Create an input file stream object in read-only mode
Instream:=tfilestream.create (
opendialog1.filename,fmopenread);
Try
//If the current pointer position of the instream stream is not at the end, the data is read in the defined block size///To file
while (Instream.position < instream.size) do
Begin
s: = IntToStr (I);
while Length (s) < 3 do s: = ' 0 ' +s;
s: = '. ' +s;
//Generate the extension of the cut file, using three digits as an extension to facilitate the combination of files
//If the remaining block size of the instream is less than the defined block size, the remaining block number is saved as a file
If instream.size-instream.position < wantedfragsize then
realtowrite: = Instream.size-instream.position
Else
realtowrite: = wantedfragsize;
//Create an output file stream object
outstream:=tfilestream.create (savedialog1.filename+s,fmcreate);
Try
Outstream.copyfrom (Instream,realtowrite);
Inc. (i);
Finally
//Free output file stream object
Outstream.free;
end;
end;
Finally
Instream.free;
end;
end;
end;
end;
//combination file
procedure Tform1.button2click (sender:tobject);
var
I:integer;
instream, Outstream:tfilestream;
SourcePath, Fragname, s:string;
begin
opendialog1.title: = ' Please choose Cutting file ';
if Opendialog1.execute then
begin
//Get the path of the currently open cut file
SourcePath: = Extractfilepath (Opendialog1.filename);
//Get the file name of the currently open cut file
Fragname: = Extractfilename (Opendialog1.filename);
Savedialog1.title: = ' Please input the file name you want to assemble ';
If Savedialog1.execute then
begin
I: = 0;
s: = IntToStr (I);
while Length (s) < 3 do s: = ' 0 ' +s;
s: = '. ' +s;
Fragname: = Changefileext (Fragname, s);
//If a file with a. 000 extension exists in the directory, create an output file stream//object
If fileexists (sourcepath+fragname) then
begin
outstream:=tfilestream.create (savedialog1.filename,fmcreate);
Try
//To combine files in increments of file name extensions
while FileExists (sourcepath+fragname) do
Begin
instream:=tfilestream.create (Sourcepath+fragname,fmopenread);
Try
//Writes data from the input file stream to the output file stream
Outstream.copyfrom (instream,0);
Inc. (i);
s: = IntToStr (I);
While Length (s) < 3 do s: = ' 0 ' +s;
s: = '. ' +s;
Fragname: = Changefileext (Fragname, s);
finally
Instream.free;
end;
end;
finally
Outstream.free;
end;
end;
end;
end;
end;
{* Note: Note the use of the CopyFrom method, when the second parameter is 0 o'clock, which means that the input stream instream data is written to the output stream outstream, and when greater than 0 is N, represents the current stream pointer position from the input stream instream. Writes n bytes of data to the output stream OutStream, and the instream stream pointer offsets the n-byte digits backwards. For detailed explanation Please check the Delphi with help *}
Third, concluding remarks
In many of the daily tools we use, there are many use of the "stream" this object, its role is very large, such as file encryption, file download and so will involve "flow", so I think, master it is very useful, but also more important.