| 1 | unit UnzipperExt;
|
|---|
| 2 |
|
|---|
| 3 | {$mode objfpc}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, zipper;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 |
|
|---|
| 12 | { TUnzipperStreamUtf8 }
|
|---|
| 13 |
|
|---|
| 14 | TUnzipperStreamUtf8 = class(TUnZipper)
|
|---|
| 15 | private
|
|---|
| 16 | FCustomOutputStream: TStream;
|
|---|
| 17 | FCustomInputStream: TStream;
|
|---|
| 18 | procedure SetInputStream(AValue: TStream);
|
|---|
| 19 | protected
|
|---|
| 20 | Procedure CustomOpenInput(Sender: TObject; var AStream: TStream);
|
|---|
| 21 | procedure CustomCloseInput(Sender: TObject; var AStream: TStream);
|
|---|
| 22 | procedure CustomCreateOutput(Sender : TObject; var AStream : TStream; {%H-}AItem : TFullZipFileEntry);
|
|---|
| 23 | procedure CustomCloseOutput(Sender : TObject; var AStream : TStream; {%H-}AItem : TFullZipFileEntry);
|
|---|
| 24 | public
|
|---|
| 25 | function UnzipFileToStream(AFilename: string; AStream: TStream; ACaseSensitive: boolean= true): boolean;
|
|---|
| 26 | function UnzipFileToString(AFilename:string): string;
|
|---|
| 27 | constructor Create;
|
|---|
| 28 | property InputStream: TStream read FCustomInputStream write SetInputStream;
|
|---|
| 29 | end;
|
|---|
| 30 |
|
|---|
| 31 | implementation
|
|---|
| 32 |
|
|---|
| 33 | uses BGRAUTF8;
|
|---|
| 34 |
|
|---|
| 35 | { TUnzipperStreamUtf8 }
|
|---|
| 36 |
|
|---|
| 37 | procedure TUnzipperStreamUtf8.SetInputStream(AValue: TStream);
|
|---|
| 38 | begin
|
|---|
| 39 | if FCustomInputStream=AValue then Exit;
|
|---|
| 40 | FCustomInputStream:=AValue;
|
|---|
| 41 | end;
|
|---|
| 42 |
|
|---|
| 43 | procedure TUnzipperStreamUtf8.CustomOpenInput(Sender: TObject; var AStream: TStream);
|
|---|
| 44 | begin
|
|---|
| 45 | if Assigned(FCustomInputStream) then
|
|---|
| 46 | AStream := FCustomInputStream
|
|---|
| 47 | else
|
|---|
| 48 | AStream := TFileStreamUTF8.Create(FileName, fmOpenRead or fmShareDenyWrite);
|
|---|
| 49 | end;
|
|---|
| 50 |
|
|---|
| 51 | procedure TUnzipperStreamUtf8.CustomCloseInput(Sender: TObject; var AStream: TStream);
|
|---|
| 52 | begin
|
|---|
| 53 | if AStream = FCustomInputStream then
|
|---|
| 54 | AStream := nil
|
|---|
| 55 | else
|
|---|
| 56 | FreeAndNil(AStream);
|
|---|
| 57 | end;
|
|---|
| 58 |
|
|---|
| 59 | procedure TUnzipperStreamUtf8.CustomCreateOutput(Sender: TObject;
|
|---|
| 60 | var AStream: TStream; AItem: TFullZipFileEntry);
|
|---|
| 61 | begin
|
|---|
| 62 | AStream := FCustomOutputStream;
|
|---|
| 63 | end;
|
|---|
| 64 |
|
|---|
| 65 | procedure TUnzipperStreamUtf8.CustomCloseOutput(Sender: TObject;
|
|---|
| 66 | var AStream: TStream; AItem: TFullZipFileEntry);
|
|---|
| 67 | begin
|
|---|
| 68 | AStream := nil;
|
|---|
| 69 | end;
|
|---|
| 70 |
|
|---|
| 71 | function TUnzipperStreamUtf8.UnzipFileToStream(AFilename: string; AStream: TStream;
|
|---|
| 72 | ACaseSensitive: boolean): boolean;
|
|---|
| 73 | var
|
|---|
| 74 | i: integer;
|
|---|
| 75 | entryName: string;
|
|---|
| 76 | begin
|
|---|
| 77 | OpenInput;
|
|---|
| 78 | AFilename := StringReplace(AFilename,'/','\',[rfReplaceAll]);
|
|---|
| 79 | Try
|
|---|
| 80 | ReadZipDirectory;
|
|---|
| 81 | for i := 0 to Entries.count-1 do
|
|---|
| 82 | begin
|
|---|
| 83 | entryName := Entries.FullEntries[i].ArchiveFileName;
|
|---|
| 84 | entryName:= StringReplace(entryName,'/','\',[rfReplaceAll]);
|
|---|
| 85 | if (entryName = AFilename) or
|
|---|
| 86 | (not ACaseSensitive and (CompareText(entryName,AFilename)=0)) then
|
|---|
| 87 | begin
|
|---|
| 88 | OnCreateStream := @CustomCreateOutput;
|
|---|
| 89 | OnDoneStream := @CustomCloseOutput;
|
|---|
| 90 | FCustomOutputStream := AStream;
|
|---|
| 91 | UnZipOneFile(Entries.FullEntries[i]);
|
|---|
| 92 | OnCreateStream := nil;
|
|---|
| 93 | OnDoneStream := nil;
|
|---|
| 94 | FCustomOutputStream := nil;
|
|---|
| 95 | result := true;
|
|---|
| 96 | exit;
|
|---|
| 97 | end;
|
|---|
| 98 | end;
|
|---|
| 99 | Finally
|
|---|
| 100 | CloseInput;
|
|---|
| 101 | end;
|
|---|
| 102 | result := false;
|
|---|
| 103 | end;
|
|---|
| 104 |
|
|---|
| 105 | function TUnzipperStreamUtf8.UnzipFileToString(AFilename: string): string;
|
|---|
| 106 | var mem: TMemoryStream;
|
|---|
| 107 | begin
|
|---|
| 108 | mem := TMemoryStream.Create;
|
|---|
| 109 | try
|
|---|
| 110 | UnzipFileToStream(AFilename,mem);
|
|---|
| 111 | setlength(result,mem.Size);
|
|---|
| 112 | mem.Position:= 0;
|
|---|
| 113 | mem.Read(result[1], length(result));
|
|---|
| 114 | finally
|
|---|
| 115 | mem.Free;
|
|---|
| 116 | end;
|
|---|
| 117 | end;
|
|---|
| 118 |
|
|---|
| 119 | constructor TUnzipperStreamUtf8.Create;
|
|---|
| 120 | begin
|
|---|
| 121 | inherited Create;
|
|---|
| 122 | OnOpenInputStream := @CustomOpenInput;
|
|---|
| 123 | OnCloseInputStream:= @CustomCloseInput;
|
|---|
| 124 | end;
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | end.
|
|---|
| 128 |
|
|---|