Solved

Wrapping several .png files into one file

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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