Link to home
Start Free TrialLog in
Avatar of joshordan
joshordan

asked on

Splitting a 2mb file into 2 * 1mb and back again

Just say I had a 2mb file. Is there a way (in code, D2) to split this file into 2 seperate files then be able to join them together to make the original file again. The reason being that a file of 2mb won't go on a floppy and i'm toying with the idea to make a little app for myself that will do it. I know I could span disks with pkzip etc but would like to do it myself. Any good suggestions will receive the finished util once finished.
ps I know how to get it back into 1 file with the DOS command COPY File1 File2 /B But how to split it in the first place.
ASKER CERTIFIED SOLUTION
Avatar of rene100
rene100

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is how.....
-------------
var
  inFile, outFile : File;
  CopyBuffer : Pointer;
  iRecsOK, iRecsWr, iX: Integer;
  sFileName : String;
CONST
  ChunkSize : LongInt = 1024000;
begin
  GetMem(CopyBuffer, ChunkSize);
  sFileName := 'C:\windows\desktop\test';
  AssignFile(inFile, sFileName + '.ZIP');
  Reset(inFile);
  iX := 1;
  repeat
    AssignFile(outFile, sFileName + IntToStr(iX) + '.ZIP');
    Rewrite(outFile);
    inc(iX);
    BlockRead(inFile, CopyBuffer^, ChunkSize, iRecsOK);
    BlockWrite(outFile, CopyBuffer^, iRecsOK, iRecsWr);
    ClosFile(outFile);
  until (iRecsOK < Chunksize);
  CloseFile(inFile);
  FreeMem(CopyBuffer, Chunksize);
end;
------------
Regards,
Viktor Ivanov
There is a mistake...

Do you see where it says ClosFile(outFile); this is supose to be CloseFile(outFile);

Regards,
Viktor Ivanov
Avatar of joshordan
joshordan

ASKER

Rene / Viktor,

Many thanks for guiding me.

Rene
I can see that your 2 functions have exactly the required result, but.. :) All is OK until the file changed size from 2mb, ie, it changes from 2mb to 5mb or beyond. Is there a way to make the functions a little more generic as to be able to handle any sized file??
try this:

procedure TForm1.SplitFile(FileName,NewFile1,NewFile2: string);
var
fStream: TMemoryStream;
dStream1,dStream2: TMemoryStream;
tLength1,tLength2: longint;
begin
fStream:=TMemoryStream.create;
dStream1:=TMemoryStream.create;
dStream2:=TMemoryStream.create;
fStream.LoadFromFile(FileName);
if fStream.size mod 2 =0 then
   begin
   tLength1:=Trunc(FStream.size/2);
   tLength2:=Trunc(FStream.size/2);
   end
else
   begin;
   tLength1:=Trunc(FStream.size/2-0.5);
   tLength2:=Trunc(FStream.size/2+0.5);
   end;
dStream1.CopyFrom(fStream,tLength1);
dStream1.SaveToFile(NewFile1);
dStream1.free;
fStream.Position:=tLength1;
dStream2.CopyFrom(fStream,tLength2);
dStream2.SaveToFile(Newfile2);
dStream2.free;
fStream.free;
end;

procedure Tform1.ConcatFiles(File1,file2,NewFileName: string);
var
fStream: TMemoryStream;
dStream1,dStream2: TMemoryStream;
begin
fStream:=TMemoryStream.create;
dStream1:=TMemoryStream.create;
dStream2:=TMemoryStream.create;
dStream1.LoadFromFile(File1);
dStream2.LoadFromFile(File2);
fStream.CopyFrom(dStream1,dStream1.size);
fStream.Position:=dStream1.Size;
fStream.CopyFrom(dStream2,dStream2.size);
dStream1.free;
dStream2.free;
fStream.SaveToFile(NewFileName);
fStream.free;
end;