Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to combine a splited files?

Posted on 2001-07-28
7
Medium Priority
?
159 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 300 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
Independent Software Vendors: 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!

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 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