Solved

How to combine a splited files?

Posted on 2001-07-28
7
150 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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