Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Get a lot of files in one stream Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
07-Nov-02
Category
Files Operation
Language
Delphi 3.x
Views
91
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Pieter Valentijn

How to get a lot of files in one stream 
And get it back to :)

Answer:

Some times i want to Have multiple files in a stream couse then i dont have to send 
a lot of files but just one . 

So heres a little code sniped to get it in and out again. 
1   
2   procedure TForm1.ThisISHowIPutFilesIn;
3   var
4     ABigFileStream, SomeSmallFiles: TMemoryStream;
5   
6   begin
7     ABigFileStream := TMemoryStream.Create;
8     try
9       SomeSmallFiles := TMemoryStream.Create;
10      try
11        SomeSmallFiles.LoadFromFile('C:\SomeSmalFile1.txt');
12        AddToStream(SomeSmallFiles, ABigFileStream);
13        SomeSmallFiles.LoadFromFile('C:\SomeSmalFile2.txt');
14        AddToStream(SomeSmallFiles, ABigFileStream);
15        // enz
16      finally
17        SomeSmallFiles.Free;
18      end;
19      ABigFileStream.SaveToFile('C:\MrBig.DDD')
20    finally
21      ABigFileStream.free;
22    end;
23  end;
24  
25  procedure TForm1.ThisISHowIGetStufOut;
26  var
27    ABigFileStream, SomeSmallFiles: TMemoryStream;
28  
29  begin
30    ABigFileStream := TMemoryStream.Create;
31    try
32      ABigFileStream.LoadFromFile('C:\MrBig.DDD');
33      SomeSmallFiles := TMemoryStream.Create;
34      try
35        GetFromStream(ABigFileStream, SomeSmallFiles, 0);
36        SomeSmallFiles.SaveToFile('C:\SomeSmalFile1.txt');
37        GetFromStream(ABigFileStream, SomeSmallFiles, 1);
38        SomeSmallFiles.SaveToFile('C:\SomeSmalFile2.txt');
39        // enz
40      finally
41        SomeSmallFiles.Free;
42      end;
43    finally
44      ABigFileStream.free;
45    end;
46  end;
47  
48  procedure TForm1.AddToStream(Source, Dest: TStream);
49  var
50    Size: Integer;
51  begin
52    Source.position := 0;
53    // Keep the size by puting it in the first byte
54    Size := Source.Size;
55    Dest.write(Size, SizeOf(Integer));
56    Dest.CopyFrom(Source, Source.size);
57  end;
58  
59  procedure TForm1.GetFromStream(Source, Dest: TStream; Index: Integer);
60  var
61    Size, I: Integer;
62  
63  begin
64    Source.Position := 0;
65    for i := 0 to index - 1 do
66    begin
67      Source.read(Size, SizeOf(Integer));
68      Source.Position := Source.Position + Size;
69    end;
70    // if where all the way up the file pointer then someting went wrong :(
71    if Source.position = Source.Size then
72      raise EAccessViolation.Create('Index Out Of Bounds');
73    // Get the desired file size
74    Source.read(Size, SizeOf(Integer));
75    // Clear Dest Buffer
76    Dest.Position := 0;
77    Dest.Size := 0;
78    Dest.CopyFrom(Source, Size);
79  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC