Solved

How to combine a splited files?

Posted on 2001-07-28
7
157 Views
Last Modified: 2010-04-06
Whether is it possible to split a big files into small
parts first, then transfer those small parts to a webserver and combine them together?

Thanks

Shekou
0
Comment
Question by:shekou
[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
7 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6329939
a sample from my paq:

a simple sample

unit splitfiles_u;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
  Button1: TButton;
  OpenDialog1: TOpenDialog;
  Edit1: TEdit;
  SaveDialog1: TSaveDialog;
  Button2: TButton;
  Label1: TLabel;
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
private
  { Private declarations }
public
  { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

//Splitting Files
procedure TForm1.Button1Click(Sender: TObject);
var
i, WantedFragSize, RealToWrite : Integer;
InStream, OutStream : TFileStream;
S : String;
begin
if Opendialog1.Execute then
begin
  SaveDialog1.Title := 'Select Dir and input FilePrefix without Suffix';
  If SaveDialog1.Execute then
  begin
    WantedFragSize := StrtoInt(Edit1.Text);  //Wanted SplitSize
    i := 0;
    InStream  := TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
    try
      while (InStream.Position < InStream.Size) do
      Begin
        s := IntToStr(I);                   //Building numeric suffix
        while Length(s) < 3 do s := '0'+s;
        s := '.'+s;
        If InStream.Size - InStream.Position < WantedFragSize then
          RealToWrite := InStream.Size - InStream.Position
        else
          RealToWrite := WantedFragSize;
        OutStream  := TFileStream.Create(SaveDialog1.FileName+s,fmCreate);
        try
          OutStream.CopyFrom(InStream,RealToWrite);
          Inc(i);
        finally
          OutStream.Free;
        end;
      end;
    Finally
      InStream.Free;
    end;
  end;
end;
end;

//concanating files
procedure TForm1.Button2Click(Sender: TObject);
var
i : Integer;
InStream, OutStream : TFileStream;
SourcePath, FragName, S : String;
begin
Opendialog1.Title := 'Select any Fragmented File';
if Opendialog1.Execute then
begin
  SourcePath := ExtractFilePath(Opendialog1.FileName);
  FragName := ExtractFileName(Opendialog1.FileName);
  SaveDialog1.Title := 'Select Dir and input the FileName with Suffix';
  If SaveDialog1.Execute then
  begin
    i := 0;
    s := IntToStr(I);
    while Length(s) < 3 do s := '0'+s;
    s := '.'+s;
    FragName := ChangeFileExt(FragName, s);
    If FileExists(SourcePath+FragName) then
    begin
      OutStream := TFileStream.Create(Savedialog1.FileName,fmCreate);
      try
        While FileExists(SourcePath+FragName) do
        Begin
          InStream  := TFileStream.Create(SourcePath+FragName,fmOpenRead);
          try
            OutStream.CopyFrom(InStream,0);
            Inc(i);
            s := IntToStr(I);
            while Length(s) < 3 do s := '0'+s;
            s := '.'+s;
            FragName := ChangeFileExt(FragName, s);
          finally
            InStream.Free;
          end;
        end;
      finally
        OutStream.Free;
      end;
    end;
  end;
end;
end;

end.


meikl ;-)
0
 
LVL 2

Accepted Solution

by:
bugroger earned 100 total points
ID: 6330027
Hi,

Try these two functions:
 With "SplitFile" you can Split a File "FileName"
 (ex.: 'C:\BigFile.dat') to a specified directory
 "DestDir" (ex.: 'C:\BigFileDir\'). PartSize is the
 max. size of each part file.

 With JoinFiles you can join the File which were splitted
 with SplitFile to a File "FileName". "SourceDir" is the
 directory where are the part files (ex.: bigfile.000, ...)
 "SplitName"  is the name of the splitted files without
 '.XXX' (ex.: 'BigFile'). IF "SplitName" is '' the function
 use the "FileName" as name of the splitted files.

 you can call the functions like this:

 SplitFile('C:\test.bmp','C:\Test1\', 500000);
 {...}
 JoinFiles('C:\test.bmp', 'C:\Test1\', '' );

 SplitFile('C:\test.bmp','C:\Test1\', 500000);
 {...}
 JoinFiles('C:\Bitmap.bmp', 'C:\Test1\', 'test' );

Function JoinFiles(FileName, SourceDir, SplitName : String) : Boolean;
VAR
 DestFile : TFileStream;
 PartFile : TMemoryStream;

 Fn                    : String;
 z                     : Cardinal;

 Function _NoExt(No : Cardinal) : String;
 Begin
  Result := IntToStr(No);
  While Length(Result) < 3 do Insert('0', Result, 1);
 End;


Begin
 Result := TRUE;
 TRY
  If SourceDir[Length(SourceDir)] <> '\' then SourceDir := SourceDir + '\';

  If SplitName = '' then
  Begin
   fn := ExtractFileName(FileName);
   z  := pos('.', fn);
   Delete(fn, z+1 , Length(fn) -z );
  End else
  Begin
   fn := SplitName + '.';
  End;

  DestFile := TFileStream.Create(FileName, fmCreate);

  z := 0;
  While FileExists(SourceDir + fn + _NoExt(z)) do
  Begin

   PartFile := TMemoryStream.Create;
   PartFile.LoadFromFile(SourceDir + fn + _NoExt(z));
   DestFile.WriteBuffer(PartFile.Memory^, PartFile.Size);
   PartFile.Free;
   Inc(z);
  End;

 EXCEPT
  Result := FALSE;
 END;
 DestFile.Free;
End;

Function SplitFile(Filename, DestDir : String; PartSize : Cardinal) : Boolean;
VAR
 SourceFile   : TFileStream;
 PartFile     : TMemoryStream;
 Fn           : String;

 Size, z : Cardinal;

 Function _NoExt(No : Cardinal) : String;
 Begin
  Result := IntToStr(No);
  While Length(Result) < 3 do Insert('0', Result, 1);
 End;


Begin
 Result := TRUE;
 TRY
  If DestDir[Length(DestDir)] <> '\' then DestDir := DestDir + '\';

  fn := ExtractFileName(FileName);
  z  := pos('.', fn);
  Delete(fn, z+1 , Length(fn) -z );

  SourceFile := TFileStream.Create(FileName, fmOpenRead);
  Size := SourceFile.Size;

  PartFile := TMemoryStream.Create;

  z := 0;
  While Size > 0 do
  Begin
   IF Size > PartSize then PartFile.Size := PartSize
                      else PartFile.Size := Size;

   Size := Size - PartFile.Size;

   SourceFile.ReadBuffer(PartFile.Memory^, PartFile.Size);
   PartFile.SaveToFile(DestDir + fn + _NoExt(z));
   PartFile.Position := 0;
   Inc(z);
  End;

 EXCEPT
  Result := FALSE;
 END;
 PartFile.Free;
 SourceFile.Free;
End;


GL
 Bug




0
 
LVL 21

Expert Comment

by:gemarti
ID: 6330474
Listening.....
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:shekou
ID: 6333288
I know it is easy to combine the file in c:\, what I am
asking is whether is it possible to concatenate files in a
webserver ( or ftp server) such as under ftp://ftp.mydrive.com/

Thanks

Shekou
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6333398
even that my sample works with streams,
you can download the fragments, combine it
and upload the whole file
0
 

Author Comment

by:shekou
ID: 6336680
Still some misunderstanding, I am NOT mean upload a whole file, I mean upload splitted parts of file to ftp://ftp.mydrive.com/ then combine them together from there. How to do that?

Thanks

James
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6336875
this must do the ftp-server, i guess,
with an ftp-server os depending script
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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month5 days, 10 hours left to enroll

627 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