Solved

How to combine a splited files?

Posted on 2001-07-28
7
148 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now