Solved

Wrapping several .png files into one file

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now