* Add additional parameters to Unzip calls. Fix issue 39530

This commit is contained in:
Michaël Van Canneyt 2022-01-13 22:14:46 +01:00
parent bb91dadb00
commit 42361be239
4 changed files with 160 additions and 0 deletions

View File

@ -585,9 +585,12 @@ Type
Class Procedure Unzip(const AZipFileName : RawByteString);
// Unzip a single file.
Class Procedure Unzip(const AZipFileName : RawByteString;aExtractFileName : RawByteString);
Class Procedure UnZip(const AZipFileName, aExtractFileName: RawByteString; aOutputFileName : string);
// Unzip several files
Class Procedure Unzip(const AZipFileName : RawByteString; aFileList : Array of RawByteString);
Class Procedure Unzip(const AZipFileName : RawByteString; aFileList : TStrings);
Class Procedure Unzip(const AZipFileName : RawByteString; aFileList : Array of RawByteString; aOutputDir : RawByteString; aFlat : Boolean = false);
Class Procedure Unzip(const AZipFileName : RawByteString; aFileList : TStrings; aOutputDir : RawByteString; aFlat : Boolean = false);
Procedure Clear;
Procedure Examine;
Procedure Terminate;
@ -3020,6 +3023,62 @@ begin
end;
end;
Type
{ TCustomExtractor }
TCustomExtractor = Class(TObject)
Private
FStream : TStream;
FunZipper : TUnzipper;
procedure DoCreateStream(Sender: TObject; var AStream: TStream; AItem: TFullZipFileEntry);
Public
Constructor Create(aUnZipper : TUnzipper);
Destructor Destroy; override;
Procedure UnZip(const AZipFileName, aExtractFileName: RawByteString; aOutputFileName: string);
end;
{ TCustomExtractor }
procedure TCustomExtractor.DoCreateStream(Sender: TObject; var AStream: TStream; AItem: TFullZipFileEntry);
begin
aStream:=FStream;
FStream:=Nil;
end;
constructor TCustomExtractor.Create(aUnZipper: TUnzipper);
begin
FStream:=Nil;
FUnzipper:=aUnzipper;
end;
destructor TCustomExtractor.Destroy;
begin
FreeAndNil(FUnZipper);
FreeAndNil(FStream);
Inherited;
end;
procedure TCustomExtractor.UnZip(const AZipFileName, aExtractFileName: RawByteString; aOutputFileName: string);
begin
FStream:=TFileStream.Create(aOutputFileName,fmCreate);
FUnZipper.OnCreateStream:=@DoCreateStream;
FUnzipper.UnzipFile(aZipFileName,aExtractFileName);
end;
class procedure TUnZipper.UnZip(const AZipFileName, aExtractFileName: RawByteString; aOutputFileName: string);
begin
With TCustomExtractor.Create(Self.Create) do
try
Unzip(aZipFileName,aExtractFileName,aOutputFileName);
Finally
Free;
end;
end;
class procedure TUnZipper.Unzip(const AZipFileName: RawByteString; aFileList: array of RawByteString);
begin
With Self.Create do
@ -3040,6 +3099,31 @@ begin
end;
end;
class procedure TUnZipper.Unzip(const AZipFileName: RawByteString; aFileList: array of RawByteString; aOutputDir: RawByteString;
aFlat: Boolean);
begin
With Self.Create do
try
Flat:=aFlat;
OutputPath:=aOutputDir;
UnZipFiles(aZipFileName,aFileList);
finally
Free;
end;
end;
class procedure TUnZipper.Unzip(const AZipFileName: RawByteString; aFileList: TStrings; aOutputDir: RawByteString; aFlat: Boolean);
begin
With Self.Create do
try
Flat:=aFlat;
OutputPath:=aOutputDir;
UnZipFiles(aZipFileName,aFileList);
finally
Free;
end;
end;
procedure TUnZipper.DoEndOfFile;
Var

Binary file not shown.

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="12"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<Title Value="testsingle"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<BuildModes>
<Item Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
</RunParams>
<Units>
<Unit>
<Filename Value="testsingle.pas"/>
<IsPartOfProject Value="True"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="testsingle"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="../src"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
</CompilerOptions>
<Debugging>
<Exceptions>
<Item>
<Name Value="EAbort"/>
</Item>
<Item>
<Name Value="ECodetoolError"/>
</Item>
<Item>
<Name Value="EFOpenError"/>
</Item>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,19 @@
program testsingle;
uses sysutils, zipper;
Var
FN : String;
begin
FN:=GetTempFileName;
TUnzipper.Unzip('test.zip','files/file1.txt',FN);
if not FileExists(FN) then
Writeln('Error: no file named ',FN)
else
begin
// DeleteFile(FN);
Writeln('OK for ',fn);
end;
end.