Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2009-07

[PATCH] patch for zipper.pp

10 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrew Haines

Hi,

This patch updates TUnZipper so that files can be unzipped to a custom user stream.

To use a custom stream here is an example:

UnZip := TUnZipper.Create;
// both of the following have to be set or
// the builtin method will be used (TFileStream)
UnZip.UseCustomOutputStream := True;
UnZip.OnCustomOutput := @UnZipCustomOutput;

....

procedure (Sender: TObject; var AStream:
            TStream; AItem: TFullZipFileEntry; AClosing: Boolean);
begin
  if AClosing = False then
  begin
    AStream := TMemoryStream.Create;
  end
  else
  begin
    DoSomethingCreativeWith(AStream);
    AStream.Free;
  end;
end;

Also fixed a bug where if a date was not set on a zipfile entry a crash would occur.

Regards,

Andrew

Reply by email

Micha Nelissen
to Andrew Haines: This patch updates TUnZipper so that files can be unzipped to a custom user stream. To…
Andrew Haines wrote 7 lines
procedure (Sender: TObject; var AStream: TStream; AItem: TFullZipFileEntry; AClosing: Boolean); begin if AClosing = False then begin AStream := TMemoryStream.Create; end

Do you mind if I say I think this is a very weird event handler?

What is the behaviour? How many times is it called? What does AClosing mean? How do I know I don't have a memory leak?

Why an event handler and not a property? A CustomStream property where non-nil means use this stream, if nil then default old behaviour e.g.

Micha

Reply by email

Michael Van Canneyt
to Micha Nelissen
Micha Nelissen wrote 16 lines
Andrew Haines wrote: > procedure (Sender: TObject; var AStream: > TStream; AItem: TFullZipFileEntry; AClosing: Boolean); > begin > if AClosing = False then > begin > AStream := TMemoryStream.Create; > end Do you mind if I say I think this is a very weird event handler? What is the behaviour? How many times is it called? What does AClosing mean? How do I know I don't have a memory leak? Why an event handler and not a property? A CustomStream property where non-nil means use this stream, if nil then default old behaviour e.g.

Well, maybe a bit awkward, but the handler makes sense.

Your Customstream is not possible, because multiple files must be extracted to different streams.

The handler is called 2 times:
- Once to create the stream (aclosing=false)
- Once when the data has been written to the steam. (AClosing=true)

Maybe it had been better to create 2 events:
- OnCreateStream
- OnDestroyStream (optional, if not set the component frees the stream)

What do you think ?

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote 6 lines
Maybe it had been better to create 2 events: - OnCreateStream - OnDestroyStream (optional, if not set the component frees the stream) What do you think ?

This makes a lot more sense that the confusing AClosing usage. Without good documentation the developer would not know what AClosing means. The "self-documenting" events you listed above is much clearer in this regards.

Just my 2c worth... ;-)

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Micha Nelissen
to Michael Van Canneyt: Well, maybe a bit awkward, but the handler makes sense. Your Customstream is not…
Michael Van Canneyt wrote 9 lines
The handler is called 2 times: - Once to create the stream (aclosing=false) - Once when the data has been written to the steam. (AClosing=true) Maybe it had been better to create 2 events: - OnCreateStream - OnDestroyStream (optional, if not set the component frees the stream) What do you think ?

Yes, that's more intuitive to me. (Also removes the need for that boolean parameter?)

Would it be allowed to do processing on OnDestroyStream? Maybe OnDoneStream or so would be even better (since that is what it means I guess, the file in the zip stream has been extracted to the specified stream in OnCreateStream?). In Delphi, in an "OnDestroy" you normally don't need to destroy the object the event is about?

Would it be allowed to re-use the same stream?
1) OnCreateStream <- MyStream
2) OnDoneStream <- write MyStream to .... but do not free it
3) OnCreateStream <- same MyStream
etc...?

Micha

Reply by email

Michael Van Canneyt
to Micha Nelissen
Micha Nelissen wrote 25 lines
Michael Van Canneyt wrote: > The handler is called 2 times: > - Once to create the stream (aclosing=false) > - Once when the data has been written to the steam. (AClosing=true) > > Maybe it had been better to create 2 events: > - OnCreateStream > - OnDestroyStream (optional, if not set the component frees the stream) > > What do you think ? Yes, that's more intuitive to me. (Also removes the need for that boolean parameter?) Would it be allowed to do processing on OnDestroyStream? Maybe OnDoneStream or so would be even better (since that is what it means I guess, the file in the zip stream has been extracted to the specified stream in OnCreateStream?). In Delphi, in an "OnDestroy" you normally don't need to destroy the object the event is about? Would it be allowed to re-use the same stream? 1) OnCreateStream <- MyStream 2) OnDoneStream <- write MyStream to .... but do not free it 3) OnCreateStream <- same MyStream etc...?

I don't see why it would not be allowed, so, yes.

I'll re-implement it as proposed.

Michael.

Reply by email

Andrew Haines
to Micha Nelissen: Do you mind if I say I think this is a very weird event handler? What is the behaviour?…
Micha Nelissen wrote
Do you mind if I say I think this is a very weird event handler?

No, of course not :)

What is the behaviour? How many times is it called? What does AClosing mean? How do I know I don't have a memory leak?

Yes you are right of course. It is confusing.

Why an event handler and not a property? A CustomStream property where non-nil means use this stream, if nil then default old behaviour e.g.

As Michael said a property does not make much sense.

I just looked a the changes Michael made and it is very straight forward now :)

Regards,

Andrew

Reply by email