View Issue Details

IDProjectCategoryView StatusLast Update
0036399FPCFCLpublic2019-12-05 12:06
ReporterwpAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityhave not tried
Status closedResolutionfixed 
Product VersionProduct Build 
Target VersionFixed in Version3.3.1 
Summary0036399: Add UnZip method for a single file to TUnZipper
DescriptionExtracting a single file from a zip archive is an often-used operation when working with TUnzipper. Doing this, however, is a bit cumbersome: a stringlist must be created, the filename must be added to the stringlist, UnZipFiles must be invoked with the stringlist as parameter (and the name of the zip file -- or the name of the zip file must already have been assigned to the FileName property), and the stringlist must be destroyed.

It would be much more convenient to have an UnZipFile method which does all this in a single call:

procedure TForm1.Button2Click(Sender: TObject);
var
  unzipper: TUnZipper;
begin
  unzipper := TUnZipper.Create;
  try
    unzipper.UnZipFile('test.zip', 'project2.lpi'); // the second parameter is the file to be extracted
  finally
    unzipper.Free;
  end;
end;

A patch is added to provide this functionality.
TagsNo tags attached.
Fixed in Revision43640
FPCOldBugId
FPCTarget3.2.0
Attached Files
  • zipper.pp.patch (1,131 bytes)
    Index: packages/paszlib/src/zipper.pp
    ===================================================================
    --- packages/paszlib/src/zipper.pp	(revision 43639)
    +++ packages/paszlib/src/zipper.pp	(working copy)
    @@ -561,6 +561,8 @@
         Constructor Create;
         Destructor Destroy;override;
         Procedure UnZipAllFiles; virtual;
    +    Procedure UnZipFile(ToBeExtracted: RawByteString);
    +    Procedure UnZipFile(AFileName, ToBeExtracted: RawByteString);
         Procedure UnZipFiles(AFileName : RawByteString; FileList : TStrings);
         Procedure UnZipFiles(FileList : TStrings);
         Procedure UnZipAllFiles(AFileName : RawByteString);
    @@ -2832,6 +2834,25 @@
         end;
     end;
     
    +procedure TUnZipper.UnZipFile(ToBeExtracted: RawByteString);
    +begin
    +  UnzipFile(FFileName, ToBeExtracted);
    +end;
    +
    +procedure TUnZipper.UnZipFile(AFileName, ToBeExtracted: RawByteString);
    +var
    +  L: TStrings;
    +begin
    +  FFileName := AFileName;
    +  L := TStringList.Create;
    +  try
    +    L.Add(ToBeExtracted);
    +    UnzipFiles(L);
    +  finally
    +    L.Free;
    +  end;
    +end;
    +
     procedure TUnZipper.UnZipFiles(AFileName: RawByteString; FileList: TStrings);
     
     begin
    
    zipper.pp.patch (1,131 bytes)

Activities

wp

2019-12-05 00:13

reporter  

zipper.pp.patch (1,131 bytes)
Index: packages/paszlib/src/zipper.pp
===================================================================
--- packages/paszlib/src/zipper.pp	(revision 43639)
+++ packages/paszlib/src/zipper.pp	(working copy)
@@ -561,6 +561,8 @@
     Constructor Create;
     Destructor Destroy;override;
     Procedure UnZipAllFiles; virtual;
+    Procedure UnZipFile(ToBeExtracted: RawByteString);
+    Procedure UnZipFile(AFileName, ToBeExtracted: RawByteString);
     Procedure UnZipFiles(AFileName : RawByteString; FileList : TStrings);
     Procedure UnZipFiles(FileList : TStrings);
     Procedure UnZipAllFiles(AFileName : RawByteString);
@@ -2832,6 +2834,25 @@
     end;
 end;
 
+procedure TUnZipper.UnZipFile(ToBeExtracted: RawByteString);
+begin
+  UnzipFile(FFileName, ToBeExtracted);
+end;
+
+procedure TUnZipper.UnZipFile(AFileName, ToBeExtracted: RawByteString);
+var
+  L: TStrings;
+begin
+  FFileName := AFileName;
+  L := TStringList.Create;
+  try
+    L.Add(ToBeExtracted);
+    UnzipFiles(L);
+  finally
+    L.Free;
+  end;
+end;
+
 procedure TUnZipper.UnZipFiles(AFileName: RawByteString; FileList: TStrings);
 
 begin
zipper.pp.patch (1,131 bytes)

Marco van de Voort

2019-12-05 07:53

manager   ~0119619

(hmm, make the shorter function a class function, saves instancing ?). I'm not sure how overloading class and non class methods works.

I'll look at it when I have internet again, (probably in the weekend currently tethered only))

Michael Van Canneyt

2019-12-05 10:16

administrator   ~0119620

Hi, I added some more methods. Added some class methods as well.

Method list for TZipper:

    // Zips specified files into a zip with name AFileName
    Procedure ZipFile(const aFileToBeZipped : RawByteString);
    Procedure ZipFile(const AZipFileName,aFileToBeZipped : RawByteString);
    Procedure ZipFiles(const AZipFileName : RawByteString; FileList : TStrings);
    Procedure ZipFiles(const AZipFileName : RawByteString; const FileList : Array of RawbyteString);
    Procedure ZipFiles(const aFileList : Array of RawbyteString);
    Procedure ZipFiles(FileList : TStrings);
    // Zips specified entries into a zip with name AFileName
    Procedure ZipFiles(const AZipFileName : RawByteString; Entries : TZipFileEntries);
    Procedure ZipFiles(Entries : TZipFileEntries);
    // Easy access method
    // Zip single file
    Class Procedure Zip(const AZipFileName : RawByteString; const aFileToBeZipped: RawByteString);
    // Zip multiple file
    Class Procedure Zip(const AZipFileName : RawByteString; aFileList : Array of RawByteString);
    Class Procedure Zip(const AZipFileName : RawByteString; aFileList : TStrings);

TUnzipper:

    Procedure UnZipAllFiles; virtual;
    Procedure UnZipFile(const aExtractFileName: RawByteString);
    Procedure UnZipFile(const AZipFileName, aExtractFileName: RawByteString);
    Procedure UnZipFiles(const AZipFileName : RawByteString; FileList : TStrings);
    Procedure UnZipFiles(const AZipFileName : RawByteString; aFileList : Array of RawBytestring);
    Procedure UnZipFiles(aFileList : TStrings);
    Procedure UnZipAllFiles(const AZipFileName : RawByteString);
    // Easy access methods. No instance needed, uses default options.
    // Unzip all files
    Class Procedure Unzip(const AZipFileName : RawByteString);
    // Unzip a single file.
    Class Procedure Unzip(const AZipFileName : RawByteString;aExtractFileName : RawByteString);
    // Unzip several files
    Class Procedure Unzip(const AZipFileName : RawByteString; aFileList : Array of RawByteString);
    Class Procedure Unzip(const AZipFileName : RawByteString; aFileList : TStrings);

wp

2019-12-05 12:06

reporter   ~0119622

Thanks. Great idea with the class method!

Issue History

Date Modified Username Field Change
2019-12-05 00:13 wp New Issue
2019-12-05 00:13 wp File Added: zipper.pp.patch
2019-12-05 07:53 Marco van de Voort Note Added: 0119619
2019-12-05 08:33 Michael Van Canneyt Assigned To => Michael Van Canneyt
2019-12-05 08:33 Michael Van Canneyt Status new => assigned
2019-12-05 10:16 Michael Van Canneyt Status assigned => resolved
2019-12-05 10:16 Michael Van Canneyt Resolution open => fixed
2019-12-05 10:16 Michael Van Canneyt Fixed in Version => 3.3.1
2019-12-05 10:16 Michael Van Canneyt Fixed in Revision => 43640
2019-12-05 10:16 Michael Van Canneyt FPCTarget => 3.2.0
2019-12-05 10:16 Michael Van Canneyt Note Added: 0119620
2019-12-05 12:06 wp Status resolved => closed
2019-12-05 12:06 wp Note Added: 0119622