Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Wrapping several .png files into one file

Posted on 2002-04-14
2
Medium Priority
?
181 Views
Last Modified: 2010-04-04
Hello,
I am trying to take some .png files and put them in one file, which will also specify the position of the start and end of each file in the big file (so that I can recover them afterwards).
The idea is to do something like a zip file.
I'm not very familiar with operating binary files, so any help would be great!
Thanks,  Libby
0
Comment
Question by:libbysharf
2 Comments
 
LVL 1

Accepted Solution

by:
MBo earned 200 total points
ID: 6941103
This is simple way. Look at Seek and Position for more complex work

If you need to save filenames in result file, write string:
N:=Length(Fname);
FS.Write(N,SizeOf(Integer));
FS.Write(FName[1],SizeOf(Integer));
and read
FS.Read(N,SizeOf(Integer));
SetLength(Fname,N);
FS.Read(Fname[1],N);
and then
FSSave:=tfilestream.create(fname...)



procedure TForm1.Button1Click(Sender: TObject);
var
FS,FSLoad:tfilestream;
N:integer;
begin
FS:=tfilestream.create('e:\files.dat',fmCreate);
N:=2;
FS.Write(N,SizeOf(Integer));
FSLoad:=tfilestream.create('e:\4.jpg',fmOpenRead);
N:=FSload.Size;
FS.Write(N,SizeOf(Integer));
FS.CopyFrom(FSLoad,N);
FSLoad:=tfilestream.create('e:\5.jpg',fmOpenRead);
N:=FSload.Size;
FS.Write(N,SizeOf(Integer));
FS.CopyFrom(FSLoad,N);
FSLoad.Free;
FS.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
FS,FSSave:tfilestream;
Num,N,NSize:integer;
begin
FS:=tfilestream.create('e:\files.dat',fmOpenRead);
FS.Read(Num,SizeOf(Integer));
for N:=1 to Num do begin
  FSSave:=tfilestream.create('e:\file'+inttostr(N)+'.jpg',fmCreate);
  FS.Read(NSize,SizeOf(Integer));
  FSSave.CopyFrom(FS,NSize);
  FSSave.Free;
end;
FS.Free;
end;
0
 

Author Comment

by:libbysharf
ID: 6941271
Thanks,What happened was that I answered my question before I got any response... But I'll give you the points for the effort.
I think I did it a bit easier.
This is the code :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  FileDetails = record
    name : array[0..7] of char;
    size : integer;
  end;


var
  Form1: TForm1;
  FileDetailsArray : array[0..9] of  FileDetails;

implementation

{$R *.DFM}

procedure FileSave;
var
 FromF, ToF: file;
 NumRead, NumWritten: Integer;
 Buffer: array[1..2048] of Byte;
 count : integer;
begin
 AssignFile(ToF,'AllPng.gnp');
 Rewrite(ToF, 1);

 seek(ToF,sizeof(FileDetailsArray));
 AssignFile(FromF,'91827E42.png');
 Reset(FromF, 1);
 count:=0;
 repeat
  BlockRead(FromF, Buffer, SizeOf(Buffer), NumRead);
  BlockWrite(ToF, Buffer, NumRead, NumWritten);
  count:=count+NumRead;
 until (NumRead = 0) or (NumWritten <> NumRead);

 FileDetailsArray[0].name:='91827E42';
 FileDetailsArray[0].size:=count;

 AssignFile(FromF,'91842E42.png');
 Reset(FromF, 1);
 repeat
  BlockRead(FromF, Buffer, SizeOf(Buffer), NumRead);
  BlockWrite(ToF, Buffer, NumRead, NumWritten);
  count:=count+NumRead;
 until (NumRead = 0) or (NumWritten <> NumRead);

 FileDetailsArray[1].name:='91842E42';
 FileDetailsArray[1].size:=count;

 AssignFile(FromF,'91858E42.png');
 Reset(FromF, 1);
 repeat
  BlockRead(FromF, Buffer, SizeOf(Buffer), NumRead);
  BlockWrite(ToF, Buffer, NumRead, NumWritten);
  count:=count+NumRead;
 until (NumRead = 0) or (NumWritten <> NumRead);

 FileDetailsArray[2].name:='91858E42';
 FileDetailsArray[2].size:=count;

 seek(ToF,0);
 BlockWrite(ToF, FileDetailsArray, sizeof(FileDetailsArray), NumWritten);

 CloseFile(FromF);
 CloseFile(ToF);
end;


procedure FileLoad;
var
 FromF, ToF: file;
 NumRead, NumWritten: Integer;
 Buffer: array[1..2048] of Byte;
 count : integer;
begin
  AssignFile(ToF,'re1.png');
  Rewrite(ToF, 1);
  AssignFile(FromF,'AllPng.gnp');
  Reset(FromF, 1);
  BlockRead(FromF,FileDetailsArray , SizeOf(FileDetailsArray), NumRead);

 
  count:=0;
  repeat
    if (count+SizeOf(Buffer))>FileDetailsArray[0].size then
    begin
      BlockRead(FromF, Buffer,FileDetailsArray[0].size-count, NumRead);
      BlockWrite(ToF, Buffer, NumRead, NumWritten);
      count:=count+NumRead;
    end
    else
    begin
      BlockRead(FromF, Buffer, SizeOf(Buffer), NumRead);
      BlockWrite(ToF, Buffer, NumRead, NumWritten);
      count:=count+NumRead;
    end;
   until (NumRead = 0) or (NumWritten <> NumRead);

   CloseFile(ToF);

   AssignFile(ToF,'re2.png');
   Rewrite(ToF, 1);
   count:=0;
   repeat
    if (count+SizeOf(Buffer))>(FileDetailsArray[1].size-FileDetailsArray[0].size) then
    begin
      BlockRead(FromF, Buffer,FileDetailsArray[1].size-FileDetailsArray[0].size-count, NumRead);
      BlockWrite(ToF, Buffer, NumRead, NumWritten);
      count:=count+NumRead;
    end
    else
    begin
      BlockRead(FromF, Buffer, SizeOf(Buffer), NumRead);
      BlockWrite(ToF, Buffer, NumRead, NumWritten);
      count:=count+NumRead;
    end;
   until (NumRead = 0) or (NumWritten <> NumRead);

   CloseFile(ToF);

   AssignFile(ToF,'re3.png');
   Rewrite(ToF, 1);
   count:=0;
   repeat
    if (count+SizeOf(Buffer))>(FileDetailsArray[2].size-FileDetailsArray[1].size) then
    begin
      BlockRead(FromF, Buffer,FileDetailsArray[2].size-FileDetailsArray[1].size-count, NumRead);
      BlockWrite(ToF, Buffer, NumRead, NumWritten);
      count:=count+NumRead;
    end
    else
    begin
      BlockRead(FromF, Buffer, SizeOf(Buffer), NumRead);
      BlockWrite(ToF, Buffer, NumRead, NumWritten);
      count:=count+NumRead;
    end;
   until (NumRead = 0) or (NumWritten <> NumRead);

   CloseFile(ToF);

   CloseFile(FromF);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FileSave;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  FileLoad;
end;

end.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

927 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