Solved

Wrapping several .png files into one file

Posted on 2002-04-14
2
167 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
[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
2 Comments
 
LVL 1

Accepted Solution

by:
MBo earned 50 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

Technology Partners: 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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

728 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