1. File cutting/combination principle
File cutting is to cut a file into several small files. The method is to read a file object in the form of a "file stream" and then save it into files according to a certain block size. File combination means combining several files into one file. For example, this principle is used for packaging tools (Wise). If you are familiar with the concept of "flow", then it will be easy to implement. The following are the implementation steps. 2. Realize
Create a new project in Delphi, then add two BUTTONs, one EDIT, one OPENDIALOG, and one SAVEDIALOG to the form, and then change the CAPTION of BUTTON1 to "cut", and the CAPTION of BUTTON2 to "combination" (Note: Other controls omitted attributes). Double-click BUTTON1 and BUTTON2 and write the following code:
//Cut the file
PRocedure TForm1.Button1Click(Sender: TObject);
var
i, WantedFragSize, RealToWrite : Integer;
//Declare two file stream objects
//InStream is the input source file stream object, and OutStream is the output cut file stream object
InStream, OutStream : TFileStream;
S : String;
Begin
if Opendialog1.Execute then
Begin
SaveDialog1.Title := 'Enter the cutting file name you want to save';
If SaveDialog1.Execute then
Begin
WantedFragSize := StrtoInt(Edit1.Text); //Define the cutting block size
i := 0;
//Create an input file stream object in a read-only manner
InStream:=TFileStream.Create(
OpenDialog1.FileName,fmOpenRead);
Try
//If the current pointer position of the Instream stream is not at the end, read out the data and store it into a file according to the defined block size //
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, use three digits as the extension to facilitate the combination of files
//If the remaining block size in InStream is smaller than the defined block size, save the remaining block number//data into 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
//Release the 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 select Cut File';
if Opendialog1.Execute then
Begin
//Get the path to the currently opened cut file
SourcePath := ExtractFilePath(Opendialog1.FileName);
//Get the file name of the currently opened cut file
FragName := ExtractFileName(Opendialog1.FileName);
SaveDialog1.Title := 'Please enter the file name you want to combine';
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 there is a file with the extension .000 in the directory, create an output file stream//object
If FileExists(SourcePath+FragName) then
Begin
OutStream:=TFileStream.Create(Savedialog1.FileName,fmCreate);
Try
//Combining files in increments with file extension
While FileExists(SourcePath+FragName) do
Begin
InStream:=TFileStream.Create(SourcePath+FragName,fmOpenRead);
Try
//Write the data of 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;
Among the many daily tools we use, there are many "stream" objects that we will use, and their functions are also very important. For example, file encryption, file download, etc. will involve "stream", so the author believes that grasping it is Very useful and more important.