Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do i slice a file size into X peices ?

Posted on 2011-09-09
10
Medium Priority
?
251 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

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…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

564 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