Solved

How to combine a splited files?

Posted on 2001-07-28
7
147 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

19 Experts available now in Live!

Get 1:1 Help Now