Solved

How do i slice a file size into X peices ?

Posted on 2011-09-09
10
235 Views
Last Modified: 2012-05-12
lets say i retrieve a file size, i want to chop/slice its size into 8 peices and store each peice in an array..

something like this...


type
  TArrayOfInt64 = Array [0..8] Of Int64;

procedure TForm1.Button1Click(Sender: TObject);
var
  i, c: integer;
  iDiv : SmallInt;
  TotalFileSize: Int64;
  StartOffset_Array : TArrayOfInt64;
  EndOffset_Array   : TArrayOfInt64;
  iStart, iEnd : Int64;
  a: boolean;
begin
  a := true;
  TotalFileSize := get_file_size( fileURL );
  iDiv := 9;
  for c := 0 to 8 do
  begin
    if a = true then
    iStart := 0;
    begin
    iEnd   := TotalFileSize div iDiv;
    StartOffset_Array[c] := iStart;
    EndOffset_Array[c]   := iEnd;
    inc(iDiv);
    ShowMessageFmt( 'Start: %d, End: %d', [ StartOffset_Array[c], EndOffset_Array[c] ] );
    iStart := TotalFileSize div iDiv;
    a := false;
    end;
  end;

Open in new window

0
Comment
Question by:John6661
  • 5
  • 2
  • 2
  • +1
10 Comments
 

Author Comment

by:John6661
ID: 36513695
9 peices, sorry.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 36514056
What is the file?

DO you need to be able to put it back together?

mlmcc
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 36514113
Well, I'm not at all sure how this could make sense but it can be done.  Just determine how many bytes there are in the file, divide by 9 (as an integer) calling that AvgPiece, then multiply that number by 8 and subtract it from the original size calling taht FinalPiece.  Now, put the packets of AvgPiece size in the first 8 array cells and there rest of it (i.e. the piece that is FinalPiece size in the 9th cell.

You can reassemble it in the reverse of the above.
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 36514161
I haven't got much time to do it completely, but here is a starter. Look at Delphi help for the functions I'm using, to help you finish/fix whatever I left undone. I might also have complicated the thing by making the filesize of chunks a multiply of 2kb, and all the same size, except for the last file.

Const
 Block_Size=2048;
 File_Div=8;

Var
 F,F2: File of byte;
 block:Array [0..Block_Size-1] of Byte;
 fs:Int64;
 i,nf,bc,bc2:integer; // block count;
 br:integer;
begin
 AssignFile(F, 'SomeFile.bin'); 
 Reset(F) ;
 fs:=FileSize(F);
 bc:=(fs+Block_Size-1) Div Block_Size;
 bc2:=(bc+File_Div-1) Div File_Div;
for nf:=1 to File_Div do
 begin
  AssignFile(F2,'SomeFile'+IntToStr(nf)+'.bin');
  Rewrite(F2);
  for i:=0 to bc2-1 do
   begin
    BlockRead(F,block,Block_Size, rb);
    BlockWrite(F2,block,rb);
   end;
   CloseFile(F2);
 end;
 CloseFile(F);
end;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
ID: 36514196
about the 'storing file in an array' part of your question, I haven't quite understood it.
It either does not makes much sense (you don't put a file content in an array) or trivial : if you meant, having an array of records, each record saying the filename, file index 0..8, file size, file offset compared to origin file. etc... those informations can all be stored within the general loop I gave you that will physically split the files (on disk)

If you meant only split the files IN MEMORY, then the answer is trivial as well : calculate the size, use GetMem to reserve some memory and use BlockRead to read each part in ONE call.
The fact I used chunks of 2Kb in my sample is just because it is generally optimized that way to ensure that files are 2k or 4K aligned if possible.
But beware of the memory requirement if that is what you meant.

If I didn't answered your question in one or the other post, then you must tell us more about the context of your needs
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:John6661
ID: 36514229
I'm making a download accelerator for education purpose...

btw, i got it to work, although i will accept answers because you guys took effort for your answers..

here's my final solution (Note, this is a FireMonkey HD Application in XE2)

 
procedure TFireMonkeyDLClass.bStartDLClick(Sender: TObject);
type
  TArrayOfInt64 = Array [0..8] Of Int64;
var
  iForLoop: Integer;
  siEndDiv : SmallInt;
  i64TotalFileSize: Int64;
  StartOffset_Array : TArrayOfInt64;
  EndOffset_Array   : TArrayOfInt64;
  i64Start, i64End, i64End2 : Int64;
  bStartZero: boolean;
begin
  i64TotalFileSize := FileInfo.FileSize;

  bStartZero := True;

  siEndDiv := 9;

  for iForLoop := 0 to 8 do
  begin
    if ( bStartZero = true ) then
     i64Start := 0;

    i64End    := i64TotalFileSize div siEndDiv;
    i64End2   := i64End; // make a backup of for later use

    StartOffset_Array[iForLoop] := i64Start;
    EndOffset_Array[iForLoop]   := i64End;

    dec( siEndDiv );

  {  ShowMessageFmt( 'Start: %d, End: %d, %d',
   [ StartOffset_Array[iForLoop], EndOffset_Array[iForLoop], iForLoop ] ); }

    i64Start  := i64End2 + 1; // very important

    bStartZero := False;
  end;
end;

Open in new window

0
 

Author Comment

by:John6661
ID: 36514246
@mlmcc: let me explain it better..

i'm using CreateFile() to create a file, lets say .iso is the suffix...

there are 9 threads (9 connections to the server) that each thread uses InternetReadFile in a repeat/until loop with each thread has its own offset limit, each thread access the same file that was created using CreateFile and write to a different offset..
0
 

Author Comment

by:John6661
ID: 36514258
this is a multi-threaded application
0
 

Author Closing Comment

by:John6661
ID: 36514759
thanks...
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 36514964
Saving the best until the last (i.e. this is a multi-threaded application) makes it a bit more difficult to answer as does providing the extra information about it being a download application. ;-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

11 Experts available now in Live!

Get 1:1 Help Now