The idea is simple, the original file unified as a binary file to read sequentially, and then write to a new binary file, so that the merge operation is done, the reverse operation can be split file ...
First, you define a generic file representation, such as the following format: file name Length (4 bytes), filename, file Length (4 bytes), file contents. Then create a new file and write the file you want to add in sequence (as a binary file).
As for the restore, read the merged file first, then restore the file in sequence by following the procedure: Read the filename length, get the file name by this length, read the file length, get the contents of the file by this length, and write to the disk. The specific programming is shown in the following example:
The following is a file merge and decomposition function, the files in the file after the merged files in the order stored in the format:
File name Length (int), filename, file length (int), file contents
//---------------------------------------------------------------------------
Merge file sorucefile into file DestFile
BOOL Mergefile (ansistring sourcefile,ansistring destfile)
{
int isourcefilehandle,idestfilehandle,isize,ibytesread,ibyteswrite;
Char Stemp[max_path];
Isourcefilehandle=fileopen (Sourcefile,fmopenread|fmsharedenynone);
if (isourcefilehandle<0)
{
sprintf (stemp, "Cannot open file%s.", Sourcefile.c_str ());
Merrormsg (stemp);
return false;
}
if (FileExists (destfile))
Idestfilehandle=fileopen (Destfile,fmopenwrite);
Else
Idestfilehandle=filecreate (DestFile);
if (idestfilehandle<0)
{
sprintf (stemp, "Cannot open file%s.", Destfile.c_str ());
Merrormsg (stemp);
FileClose (Isourcefilehandle);
return false;
}
FileSeek (idestfilehandle,0,2); The pointer moves to the end of the file
Char *buffer=new char[1024];
__try
{
Ansistring Filename=extractfilename (sourcefile); FileName with no path
Isize=filename.length ();
if (FileWrite (Idestfilehandle, (char *) &isize,sizeof (isize))!=sizeof (isize))
{
Merrormsg ("Error writing file name length.");
return false;
}
if (FileWrite (Idestfilehandle,filename.c_str (), isize)!=isize)
{
Merrormsg ("Error writing file name.");
return false;
}
Isize=fileseek (isourcefilehandle,0,2); File length
if (FileWrite (Idestfilehandle, (char *) &isize,sizeof (int))!=sizeof (isize))
{
Merrormsg ("Error writing file length.");
return false;
}
FileSeek (isourcefilehandle,0,0); The pointer moves to the file header
Copy files
Todo
{
Ibytesread=fileread (isourcefilehandle,buffer,1024);
Ibyteswrite=filewrite (Idestfilehandle,buffer,ibytesread);
if (Ibyteswrite!=ibytesread)
{
Merrormsg ("Error writing data.");
return false;
}
}while (ibytesread==1024);
}
__finally
{
FileClose (Isourcefilehandle);
FileClose (Idestfilehandle);
delete [] Buffer;
}
return true;
}
//---------------------------------------------------------------------------
Separate files from merged files, Bprogress decide whether to show progress bars
BOOL Seperatefile (ansistring sourcefile,ansistring destpath,bool bprogress)
{
int isourcefilehandle=-1,idestfilehandle=-1,isize,ibytesread,ibyteswrite;
Ansistring DestFile;
Char *buffer=null;
Char Stemp[max_path];
Isourcefilehandle=fileopen (Sourcefile,fmopenread);
if (isourcefilehandle<0)
{
sprintf (stemp, "Cannot open file%s.", Sourcefile.c_str ());
Merrormsg (stemp);
return false;
}
int Ifilesize=fileseek (isourcefilehandle,0,2); Get file length
if (bprogress)//Show progress bar
{
Dlgprogress->initialize (ifilesize, "Restore Database from" "+sourcefile+" "to" "+destpath+" ");
Dlgprogress->show ();
}
FileSeek (isourcefilehandle,0,0); Back to file header
Buffer=new char[1024];
__try
{
while (FileSeek (isourcefilehandle,0,1) <ifilesize)//Is the end of the file
{
if (bprogress&&g_bprocessendbyuser)//user terminated via progress bar
Break
int istepby=0; Step length of progress bar propulsion
Read file name length
Ibytesread=fileread (Isourcefilehandle, (char *) &isize,sizeof (isize));
if (ibytesread!=sizeof (isize))
{
Merrormsg ("Error reading the filename length.");
return false;
}
Istepby+=ibytesread;
Read file name
buffer[isize]=0; Write file name string terminator
Ibytesread=fileread (isourcefilehandle,buffer,isize);
if (ibytesread!=isize)
{
Merrormsg ("Error reading file name.");
return false;
}
Istepby+=ibytesread;
Read into file length
Ibytesread=fileread (Isourcefilehandle, (char *) &isize,sizeof (isize));
if (ibytesread!=sizeof (isize))
{
Merrormsg ("Error reading the filename length.");
return false;
}
istepby+= (ibytesread+isize);
if (bprogress)
Dlgprogress->step (ISTEPBY);
Destfile=destpath+ansistring (Buffer);
Idestfilehandle=filecreate (DestFile);
if (idestfilehandle<0)
{
sprintf (Stemp, "Cannot create file%s.", Destfile.c_str ());
Merrormsg (stemp);
return false;
}
Copy data
for (int i=0;i<isize/1024;i++)
{
Ibytesread=fileread (isourcefilehandle,buffer,1024);
Ibyteswrite=filewrite (idestfilehandle,buffer,1024);
if (ibytesread!=1024| | ibyteswrite!=1024)
{
Merrormsg ("Error copying data.");
return false;
}
}
isize=isize%1024; The remaining unread data
if (isize==0)
{
FileClose (Idestfilehandle);
Idestfilehandle=-1;
Continue
}
Ibytesread=fileread (isourcefilehandle,buffer,isize);
Ibyteswrite=filewrite (idestfilehandle,buffer,isize);
if (ibytesread!=isize| | Ibyteswrite!=isize)
{
Merrormsg ("Error copying data.");
return false;
}
FileClose (Idestfilehandle);
Idestfilehandle=-1;
}
}
__finally
{
FileClose (Isourcefilehandle);
if (idestfilehandle>0)
FileClose (Idestfilehandle);
delete [] Buffer;
if (bprogress&&dlgprogress->visible)
Dlgprogress->close ();
}
return true;
}