Solved

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

Posted on 1998-09-19
5
160 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
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.

728 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