Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Splash screen too shortlived 11 113
Wininet read php file from internet issue 6 70
Delphi selector screen 2 58
Base1 Encode/Decode 3 34
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

17 Experts available now in Live!

Get 1:1 Help Now