EldoS | Feel safer!

Software components for data protection, secure storage and transfer

TElZipWriter and commit update

Also by EldoS: BizCrypto
Components for BizTalk® and SQL Server® Integration Services that let you securely store and transfer information in your business automation solutions.
#24488
Posted: 04/09/2013 02:37:51
by Damien Griessinger (Standard support level)
Joined: 03/14/2013
Posts: 9

Dear all,

I continue to convert my software to use telzipwriter, and I get another problem:

I have zip, I put toto.txt into it, and on the same session, I try to read the toto.txt file previously saved, toto.txt is not found on the zip !!
I close the software, I open the zip manually, and toto.txt is on the zip.
It's like on the same session, until the program is not close, the zip is not commited !

let's see with small example:

Code
program Project1;

{$APPTYPE CONSOLE}

uses
  Windows,
  Classes,
  SysUtils,
  SBUtils,
  SBArcBase,
  SBArcZip,
  SBZipUtils;

var fzip:TElZipWriter;
    inStream:TStringStream;
    entry: TElZipArchiveDirectoryEntry;
    fname,fdir,filename:string;
begin
fzip:=TElZipWriter.create(nil);
fzip.Open('a.zip',false);      //the file already exists
fzip.ReplaceMode:=armAlways;
inStream:=TStringStream.Create;
inStream.WriteString('TEST DATA012343');
inStream.Position:=0;

filename:='toto.txt';

entry := fzip.Directory.EntryWithPath(filename);
if (entry <> nil) and (not entry.IsDirectory) then
begin
  writeln('toto exists');
  fzip.Remove(filename); // we delete it before add again
end
else
  writeln('toto not found');

// first put the file on the zip
fzip.Add(inStream,filename);
fzip.Compress();
entry.free;


// test if toto is in the zip
entry := fzip.Directory.EntryWithPath(filename);
if (entry <> nil) and (not entry.IsDirectory) then
  writeln('toto exists')
else
  writeln('toto not found');
entry.free;

inStream.Free;
fzip.Close;
fzip.Free;
end.




Thanks a lot
#24489
Posted: 04/09/2013 03:29:48
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Hi. You should not call entry.free, EntryWithPath returns just pointer to entry in internal structure.
#24490
Posted: 04/09/2013 03:48:00
by Damien Griessinger (Standard support level)
Joined: 03/14/2013
Posts: 9

sorry for the .free, I understand, please try again the same code if toto.txt is under subdirectory:
filename:='justif/toto.txt';

here I get not found
#24491
Posted: 04/09/2013 03:57:24
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

That's because version of .Add which works with streams doesn't respect subdirectories inside of Filename.
To correctly add it, you should extend your code:

Code
  entry := fzip.Directory.EntryWithPath('justif');
  fzip.Add(entry, inStream, 'toto.txt');
#24497
Posted: 04/09/2013 05:36:29
by Damien Griessinger (Standard support level)
Joined: 03/14/2013
Posts: 9

Thanks, now I can add into subfolder, but...maybe I have again a doing stupid thing, If I try to extract the content after, I get Internal Error:

Code
program Project1;

{$APPTYPE CONSOLE}

uses
  Windows,
  Classes,
  SysUtils,
  SBUtils,
  SBArcBase,
  SBArcZip,
  SBZipUtils;

var fzip:TElZipWriter;
    inStream:TStringStream;
    entry: TElZipArchiveDirectoryEntry;
    fname,fdir,filename:string;
begin
fzip:=TElZipWriter.create(nil);
fzip.Open('a.zip',false);      //the file already exists
fzip.ReplaceMode:=armAlways;
inStream:=TStringStream.Create;
inStream.WriteString('TEST DATA012343');
inStream.Position:=0;

filename:='000\toto.txt';


entry := fzip.Directory.EntryWithPath(filename);
if (entry <> nil) and (not entry.IsDirectory) then
begin
  writeln('toto exists, delete it first');
  fzip.Remove(filename);
end
else
  writeln('toto not found');

fdir:=ExtractDirectory(filename);
if fdir='' then //no subdirectory
begin
   fzip.Add(inStream,filename);
end else
begin // the file is into subdir
   fname:=ExtractFile(filename);
   entry := fzip.Directory.EntryWithPath(fdir);
   if entry=nil then
     entry:=fzip.MakeDir(fdir); // create subdir
   fzip.Add(entry,inStream,fname);
end;
fzip.Compress();
inStream.Free;


// test: toto is in the zip? yes, extract the content
entry := fzip.Directory.EntryWithPath(filename);
if (entry <> nil) and (not entry.IsDirectory) then
begin
  writeln('toto exists, show content:');
   inStream:=TStringStream.Create;
   entry.ExtractionStream:=inStream;
   fzip.Extract(entry,''); //get internal error
   Writeln(inStream.DataString);
   inStream.Free;
end
else
  writeln('toto not found');

fzip.Close;
fzip.Free;
end.
#24499
Posted: 04/09/2013 05:57:49
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

You should .Close() archive and re-open it before extracting just added entries.
This is required to correctly fill internal structures, flags, etc.
#24500
Posted: 04/09/2013 06:10:23
by Damien Griessinger (Standard support level)
Joined: 03/14/2013
Posts: 9

Thanks a lot for your support.

Use Close() is quite constraining, into my program,I use a lot of save to stream / read from stream, they are no way to fix it easily?
#24501
Posted: 04/09/2013 06:15:40
by Eugene Mayevski (EldoS Corp.)

ZIP (as a concept of a packed data) is not a filesystem which you can use to read and write data dynamically. If you need a built-in virtual filesystem, take a look at our SolFS product. It has many features similar to ZIP (including compression) and is much more powerful and suitable for your task (see the comparison here).


Sincerely yours
Eugene Mayevski
#24502
Posted: 04/09/2013 06:17:29
by Eugene Mayevski (EldoS Corp.)

BTW I see that you have a license for Data Security package. Professional package comes with a free license for SolFS Lite (which should be fine for your needs). I.e. by upgrading to Professional you get all of SecureBlackbox AND SolFS Lite. If you are interested, I'll move the discussion to the helpdesk.


Sincerely yours
Eugene Mayevski
#24503
Posted: 04/09/2013 06:19:37
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Why calling .Close() and .Open() is not easy for you?
Also, for what reason do you need to add stream to archive and right after this extract it? You can just store that stream somewhere in variable.
Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.

Reply

Statistics

Topic viewed 2081 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!