Solved

Wrapping several .png files into one file

Posted on 2002-04-14
2
169 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

617 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