[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do i slice a file size into X peices ?

Posted on 2011-09-09
10
Medium Priority
?
247 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 101

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

Accepted Solution

by:
epasquier earned 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

650 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