Solved

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

Posted on 1998-09-19
5
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month5 days, 23 hours left to enroll

627 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