🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #79

Problem reading a file generated by a Delphi application

#79
From: Ronaldo
Date: Mon, 04 Apr 2005 15:32
170 lines
7640 bytes
Hi all !! My name is Ronaldo. I�m from Brazil.
I�m having a hard time on reading a file generated by a Software created through Delphi, it�s not a regular text file, though it is a file that was generated by a component, which main goal is to compress any file using a Zlib, and then to save the bytes from that compressed file as binary fields. That was just a brief introduction so as you can have an idea of the general issue�
My real problem is reading the header and the footer generated by that component (I do not have any problem in reading and decompressing the data compressed with Zlib). At that header, there is information, such as backup title, size of the backup title, total bytes from the original file, total files in backup, the size in bytes of the path + name of the file that was compressed, path + name of the file compressed, date & time of last update on that file, and a signature �EC2� or �ECS�, where the first one indicates that the content is compressed, and the second one indicates that is not�
The component writes some data with more frequency; if I manage to read those, I think I�ll be able of reading the others. I created a Delphi code very simple in order to generate that header, and another one, to read it. I did the same in Java. However, what I generate in Delphi, cannot be read in Java, and vice versa.
I�m going to leave here the source codes in Java and Delphi, in case someone can help me out. Probably somebody already passed through this issue, or at least knows both languages.

I appreciate your help in advance, and apologize for the size of this message.

Obs1 : If somebody try to write a Delphi code, add in implementation clausule :

const
fSignature = 'EC2';

Obs2: In the procedure TForm1.Button2Click(Sender: TObject); lb is TListBox component.

// Delphi code to generate header file
procedure TForm1.Button1Click(Sender: TObject);
   var ss : String ;
   L: Integer;
   FA: integer;
   CurrentFile: String;
   SStr: TFilestream;
   TStr: TStream;
   fBackupTitle : String ;
   fSizeTotal, fFilesTotal : integer ;
begin
   // Generate a header
   ss:= 'c:\binario\myfile.zip' ;
   fBackupTitle := '' ;
   fSizeTotal := 0 ;
   fFilesTotal := 1 ;
   try
      SStr := TFilestream.create('c:\binario\teste1.txt', fmOpenRead or fmShareDenyNone);
      fSizeTotal := fSizeTotal + SStr.Size;
   finally
      SStr.free;
   end;
   TStr := TFilestream.create(ss, fmCreate);
   TStr.seek(0, 0);
   L := length(fBackupTitle);
   TStr.writeBuffer(L, sizeof(L)); //Size of title
   TStr.writeBuffer(PChar(fBackupTitle)^,L); //title
   TStr.writeBuffer(fSizeTotal, sizeof(fSizeTotal)); //Total Size of backup
   TStr.writeBuffer(fFilesTotal, sizeof(fFilesTotal)); //Total file count
   CurrentFile := 'c:\binario\teste1.txt';
   L := length(CurrentFile);
   TStr.writeBuffer(L, sizeof(L)); //Size of file name
   TStr.writeBuffer(PChar(CurrentFile)^,L); //file name
   try
      SStr := TFilestream.create(Currentfile, fmOpenRead or fmShareDenyNone);
      FA := FileGetDate(SStr.handle);
      TStr.writeBuffer(FA, sizeof(FA)); //file age
      TStr.WriteBuffer(FSignature, SizeOf(FSignature)); //new signature "EC2" in ver 2.00!
   finally
      SStr.free;
   end;
   TStr.free ;
end;

  // Delphi code to read the file
  procedure TForm1.Button2Click(Sender: TObject);
     var S, Source : String ;
           SStr: TStream;
           L, FA, fSizeTotal, fFilesTotal : integer ;
           Sig: array[0..3] of Char;
begin
   // Read the header
   lb.Items.Clear ;
   source := 'c:\binario\myfile.zip' ;
   if not Fileexists(Source) then exit;
   SStr := TFilestream.create(Source, fmOpenRead or fmShareDenyNone);
   SStr.seek(0,0);
   SStr.readbuffer(L, sizeof(L));
   lb.Items.Add('Length of title: ' + inttostr(L)) ;
   SetString(S, PChar(nil), L);
   SStr.ReadBuffer(PChar(S)^,L); //Title
   lb.Items.Add('Title: ' + S) ;
   SStr.readbuffer(fSizeTotal, sizeof(fSizeTotal));
   lb.Items.Add('SizeTotal: ' + inttostr(fSizeTotal)) ;
   SStr.readbuffer(fFilesTotal, sizeof(fFilesTotal));
   lb.Items.Add('FilesTotal: ' + inttostr(fFilesTotal)) ;
   SStr.readbuffer(L, sizeof(L)); // File size
   lb.Items.Add('File size: ' + inttostr(L)) ;
   SetString(S, PChar(nil), L);
   SStr.ReadBuffer(PChar(S)^,L); //File name
   lb.Items.Add('File name: ' + S) ;
   // Read file.
   SStr.ReadBuffer(FA, sizeof(FA)); //File age
   lb.Items.Add('File age: ' + inttostr(FA)) ;
   SStr.ReadBuffer(Sig, SizeOf(FSignature));
   lb.Items.Add('Signature: ' + Sig) ;
   sstr.Free ;
 end;

// Java code to generate a file
public static void main(String[] args) throws IOException {
	ByteArrayOutputStream bout = new ByteArrayOutputStream() ;
	DataOutputStream dout = new DataOutputStream(bout) ;
	File f = new File("c://binario//teste1.txt") ;
 
	String fBackupTitle = "" ;
	int L = fBackupTitle.length() ;
	dout.writeInt(L) ;
	dout.write(fBackupTitle.getBytes()) ;
 
	Long ltmp = new Long(f.length()) ;
	int fSizeTotal = ltmp.intValue() ;
	dout.writeInt(fSizeTotal) ;
	
	int totalfiles = 1 ;
	dout.writeInt(totalfiles) ;
 
	String CurrentFile = "c:\\binario\\teste1.txt" ;
	L = CurrentFile.length() ;
	dout.writeInt(L) ;
	dout.write(CurrentFile.getBytes()) ;
 
	f = new File("c://binario//teste1.txt") ;
	long modifiedTime = f.lastModified() ;
	dout.writeLong(modifiedTime) ;
 
	String FSignature = "EC2" ;
	dout.write(FSignature.getBytes()) ;
 
	dout.flush() ;
	f = new File("c://binario//teste_int.zip") ;
	FileOutputStream fout = new FileOutputStream(f) ;
	fout.write(bout.toByteArray()) ;
	fout.flush();
	fout.close();
} 



// Java code to read the file
public static void main(String[] args) throws IOException {
	File f = new File("c://binario//teste_int.zip") ;
	RandomAccessFile raf = new RandomAccessFile(f, "r");
	raf.seek(0) ;
 
	int L = raf.readInt();
	System.out.println("L: " + L);
	byte[] tmp = new byte[L] ;
	raf.read(tmp) ;
	String fBackupTitle = new String(tmp);
	System.out.println("fBackupTitle: " + fBackupTitle);
 
	int fSizeTotal = raf.readInt();
	System.out.println("fSizeTotal: " + fSizeTotal);
 
	int totalfiles = raf.readInt() ;
	System.out.println("totalfiles: " + totalfiles);
 
	L = raf.readInt() ;
	System.out.println("CurrentFile.length : " + L);
 
	tmp = new byte[L] ;
	raf.read(tmp) ;
	String CurrentFile = new String(tmp);
	System.out.println("CurrentFile: " + CurrentFile);
	long modifiedTime = raf.readLong();
	System.out.println("modifiedTime: " + modifiedTime);
 
	tmp = new byte[3] ;
	raf.read(tmp) ;
	String FSignature = new String(tmp);
	System.out.println("FSignature: " + FSignature);
	raf.close() ;
}
Technical Details
Message-ID: <42515016@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 1089d0,e0d9b07a5e544a79,start
X-Google-Attributes: gid1089d0,public
X-Google-Language: ENGLISH,ASCII
From: "Ronaldo" <rsv29@hotmail.com>
Sender: "Ronaldo" <rsv29@hotmail.com>
Reply-To: "Ronaldo" <rsv29@hotmail.com>
Subject: Problem reading a file generated by a Delphi application
Newsgroups: borland.public.jbuilder.java.api
X-User-Info: 200.178.82.133     
X-Forwarded-For: 10.43.11.160
NNTP-Posting-Host: 207.105.83.66
Message-ID: <42515016@newsgroups.borland.com>
Date: 4 Apr 2005 07:32:54 -0800
X-Trace: newsgroups.borland.com 1112625174 207.105.83.66 (4 Apr 2005 07:32:54 -0800)
Lines: 171
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail