Solved

How do i slice a file size into X peices ?

Posted on 2011-09-09
10
239 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
[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
  • 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
Industry Leaders: 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!

 
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
 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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