[RFC] Modification to packages/paszlib/src/zipper.pp

View: New views
6 Messages — Rating Filter:   Alert me  

[RFC] Modification to packages/paszlib/src/zipper.pp

by Brad Campbell :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

With reference to this thread..
http://www.nabble.com/FPC-Zip-library-td15145102.html

and the resulting bug entry..
http://bugs.freepascal.org/view.php?id=10787

I had a similar requirement, so I went and had a crack at patching zipper.pas.

It does precisely what _I_ want it to do, but it's not the cleanest of hacks and I wonder if there
is a better way to do it?

I post it here for comment anyway.

In short rather than call ZipFiles(), I now call ZipSomeFiles(). I can then replace the path each
file will appear to have inside the zip file with SetFilePath() prior to calling ZipAllFiles() to
create the zipfile.

I was thinking perhaps it might be better to attach an ansistring to each stringlist.object entry
with a new path prior to calling ZipFiles() instead, but I thought I'd solicit comment first.

Regards,
Brad
--
Dolphins are so intelligent that within a few weeks they can
train Americans to stand at the edge of the pool and throw them
fish.

--- /tracks/devel/fpc/fpc-src/packages/paszlib/src/zipper.pp 2007-12-02 02:38:01.000000000 +0400
+++ zipper.pas 2008-09-28 11:13:29.000000000 +0400
@@ -121,6 +121,7 @@
 
   TZipItem   = Class(TObject)
     Path : String;
+    ArchivePath : String;
     Name : String;
     Size : LongInt;
     DateTime : TDateTime;
@@ -295,7 +296,9 @@
   Public
     Constructor Create;
     Destructor Destroy;override;
+    Procedure SetFilePath(Entry, NewPath : String);
     Procedure ZipAllFiles; virtual;
+    Procedure ZipSomeFiles(AFileName : String; FileList : TStrings);
     Procedure ZipFiles(AFileName : String; FileList : TStrings);
     Procedure Clear;
   Public
@@ -903,6 +906,24 @@
     TZipper
   ---------------------------------------------------------------------}
 
+Procedure TZipper.SetFilePath(Entry, NewPath : String);
+Var
+   I      : LongInt;
+   Found  : Boolean;
+Begin
+   I := 0;
+   Found := False;
+   While (I < FFiles.Count) and Not Found do
+    begin
+     If Entry = FFiles[I] then
+       begin
+         TZipItem(FFiles.Objects[I]).ArchivePath := NewPath;
+         Found := True;
+       end
+    else
+       inc(I);
+    end;
+End;
 
 Procedure TZipper.GetFileInfo;
 
@@ -919,6 +940,7 @@
        try
          NewNode:=TZipItem.Create;
          NewNode.Path := ExtractFilePath(FFiles[i]);
+         NewNode.ArchivePath := NewNode.Path;
          NewNode.Name := Info.Name;
          NewNode.Size := Info.Size;
          NewNode.DateTime:=FileDateToDateTime(Info.Time);
@@ -984,7 +1006,7 @@
 var
   ZFileName  : ShortString;
 Begin
-  ZFileName:=Item.Path+Item.Name;
+  ZFileName:=Item.ArchivePath+Item.Name;
   With LocalHdr do
     begin
     FileName_Length := Length(ZFileName);
@@ -1125,7 +1147,6 @@
     exit;
   FZipping:=True;
   Try
-    GetFileInfo;
     OpenOutput;
     Try
       filecnt:=0;
@@ -1148,7 +1169,6 @@
   end;
 end;
 
-
 Procedure TZipper.SetBufSize(Value : LongWord);
 
 begin
@@ -1171,9 +1191,18 @@
 begin
   FFiles.Assign(FileList);
   FFileName:=AFileName;
+  GetFileInfo;
   ZipAllFiles;
 end;
 
+Procedure TZipper.ZipSomeFiles(AFileName : String; FileList : TStrings);
+
+begin
+  FFiles.Assign(FileList);
+  FFileName:=AFileName;
+  GetFileInfo;
+end;
+
 Procedure TZipper.DoEndOfFile;
 
 Var

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [RFC] Modification to packages/paszlib/src/zipper.pp

by Michael Van Canneyt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Sun, 28 Sep 2008, Brad Campbell wrote:

> With reference to this thread..
> http://www.nabble.com/FPC-Zip-library-td15145102.html
>
> and the resulting bug entry..
> http://bugs.freepascal.org/view.php?id=10787
>
> I had a similar requirement, so I went and had a crack at patching zipper.pas.
>
> It does precisely what _I_ want it to do, but it's not the cleanest of hacks
> and I wonder if there is a better way to do it?

The proper way is to create a new collection item

  TZipFileEntry = Class(TcollectionItem)
    ArchiveFileName : String;
    DiskFileName : String;
    FStream : TStream;
  end;
 
  TZipFileEntries = Class(TCollection)
  end;

And add an overloaded call

  ZipFiles(AFileName : String; Files : TZipFileEntries);

Which calculates ArchiveFileName from DiskFileName if ArchiveFileName is empty,
and which uses FStream if it is set instead of DiskFileName;

This can then be used to solve both the name inside the zip file as well
as allowing it to work with streams (another request). The current call
should then simply rework the stringlist to a TZipFileEntries collection,
and call the overloaded version.

I'll have a stab at it this afternoon. Shouldn't be long to do.

Michael.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [RFC] Modification to packages/paszlib/src/zipper.pp

by Michael Van Canneyt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Sun, 28 Sep 2008, Michael Van Canneyt wrote:

>
>
> On Sun, 28 Sep 2008, Brad Campbell wrote:
>
> > With reference to this thread..
> > http://www.nabble.com/FPC-Zip-library-td15145102.html
> >
> > and the resulting bug entry..
> > http://bugs.freepascal.org/view.php?id=10787
> >
> > I had a similar requirement, so I went and had a crack at patching zipper.pas.
> >
> > It does precisely what _I_ want it to do, but it's not the cleanest of hacks
> > and I wonder if there is a better way to do it?
>
> The proper way is to create a new collection item
>
>   TZipFileEntry = Class(TcollectionItem)
>     ArchiveFileName : String;
>     DiskFileName : String;
>     FStream : TStream;
>   end;
>  
>   TZipFileEntries = Class(TCollection)
>   end;
>
> And add an overloaded call
>
>   ZipFiles(AFileName : String; Files : TZipFileEntries);

Implemented as proposed (rev. 11833).

I updated TUnZipper so it works with TZipFileEntry as well.

It is now also possible to provide the input through streams. (size is needed, so
not every stream can be used).

Michael.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [RFC] Modification to packages/paszlib/src/zipper.pp

by Brad Campbell :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Michael Van Canneyt wrote:

> Implemented as proposed (rev. 11833).
>
> I updated TUnZipper so it works with TZipFileEntry as well.
>
> It is now also possible to provide the input through streams. (size is needed, so
> not every stream can be used).

Absolutely fantastic. The stream option also saves me writing out a temporary file to disk to
include it in the zip file. Thanks very much Michael!

I don't suppose this is likely to make it into the FIXES branch by any chance? (Not that it's a
problem, I'll just leave zipper.pp in my local source tree)

Regards,
Brad
--
Dolphins are so intelligent that within a few weeks they can
train Americans to stand at the edge of the pool and throw them
fish.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [RFC] Modification to packages/paszlib/src/zipper.pp

by Michael Van Canneyt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Tue, 30 Sep 2008, Brad Campbell wrote:

> Michael Van Canneyt wrote:
>
> > Implemented as proposed (rev. 11833).
> > I updated TUnZipper so it works with TZipFileEntry as well.
> >
> > It is now also possible to provide the input through streams. (size is
> > needed, so
> > not every stream can be used).
>
> Absolutely fantastic. The stream option also saves me writing out a temporary
> file to disk to include it in the zip file. Thanks very much Michael!
>
> I don't suppose this is likely to make it into the FIXES branch by any chance?
> (Not that it's a problem, I'll just leave zipper.pp in my local source tree)

It's currently not quite clear if the fixes branch is going to be used for
the next release. In case it will be used for the next release, I'll make
sure it ends up there.

Michael.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [RFC] Modification to packages/paszlib/src/zipper.pp

by Burkhard Carstens-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Am Dienstag, 30. September 2008 10:05 schrieb Michael Van Canneyt:

> On Tue, 30 Sep 2008, Brad Campbell wrote:
> > Michael Van Canneyt wrote:
> > > Implemented as proposed (rev. 11833).
> > > I updated TUnZipper so it works with TZipFileEntry as well.
> > >
> > > It is now also possible to provide the input through streams.
> > > (size is needed, so
> > > not every stream can be used).
> >
> > Absolutely fantastic. The stream option also saves me writing out a
> > temporary file to disk to include it in the zip file. Thanks very
> > much Michael!
> >
> > I don't suppose this is likely to make it into the FIXES branch by
> > any chance? (Not that it's a problem, I'll just leave zipper.pp in
> > my local source tree)
>
> It's currently not quite clear if the fixes branch is going to be
> used for the next release. In case it will be used for the next
> release, I'll make sure it ends up there.

I think many people use the fixes_2_2 branch because trunk is "too hot"
but they need some of the 2.2.2 bugs fixed. So IMO it's good to merge
non intrusive enhancements like this, regardless if the next release
will be 2.2.4 or 2.4.0.

regards
 Burkhard

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@...
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
LightInTheBox - Buy quality products at wholesale price!