Solved

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

Posted on 1998-09-19
5
152 Views
Last Modified: 2010-04-06
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.
0
Comment
Question by:joshordan
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
rene100 earned 200 total points
ID: 1340286
hi

here are two functions that should do the job:

procedure 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);
dStream2.CopyFrom(fStream,tLength2);
dStream1.SaveToFile(NewFile1);
dStream2.SaveToFile(Newfile2);
dStream1.free;
dStream2.free;
fStream.free;
end;

procedure 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.CopyFrom(dStream2,dStream2.size);
fStream.SaveToFile(NewFileName);
dStream1.free;
dStream2.free;
fStream.free;
end;

regards
rene
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340287
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
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340288
There is a mistake...

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

Regards,
Viktor Ivanov
0
 

Author Comment

by:joshordan
ID: 1340289
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??
0
 
LVL 2

Expert Comment

by:rene100
ID: 1340290
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;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
indy tidtcpclient threading issue 8 63
Newbie Thread Programming 1 138
Delphi Seattle: Project Options : Version info for all platforms, all releases? 2 76
Printing problem 2 79
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now