| 1 |
{ |
| 2 |
Copyright 2019-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Unzip routines that both with both FPC and Delphi. } |
| 17 |
unit GameUnzip; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses SysUtils, Classes, |
| 22 |
{$ifdef FPC} Zipper {$else} System.Zip {$endif}; |
| 23 |
|
| 24 |
{ Unpack single file from zip, to a TStream. |
| 25 |
FileInZip should be relative path within the zip archive. } |
| 26 |
function UnzipFile(const ZipFileName, FileInZip: String): TStream; |
| 27 |
|
| 28 |
implementation |
| 29 |
|
| 30 |
uses CastleUriUtils, CastleDownload, CastleFilesUtils; |
| 31 |
|
| 32 |
{$ifdef FPC} |
| 33 |
type |
| 34 |
{ Helper for FPC that implements TUnZipper OnCreateStream event. } |
| 35 |
TUnzipFileHelper = class |
| 36 |
Stream: TStream; |
| 37 |
procedure UnzipCreateStream(Sender: TObject; var AStream: TStream; AItem: TFullZipFileEntry); |
| 38 |
procedure UnzipDoneStream(Sender: TObject; var AStream: TStream; AItem: TFullZipFileEntry); |
| 39 |
end; |
| 40 |
|
| 41 |
procedure TUnzipFileHelper.UnzipCreateStream(Sender: TObject; var AStream: TStream; AItem: TFullZipFileEntry); |
| 42 |
begin |
| 43 |
AStream := Stream; |
| 44 |
end; |
| 45 |
|
| 46 |
procedure TUnzipFileHelper.UnzipDoneStream(Sender: TObject; var AStream: TStream; AItem: TFullZipFileEntry); |
| 47 |
begin |
| 48 |
{ Do nothing. |
| 49 |
We need this event, because TUnZipper.CloseOutput by default |
| 50 |
(when on OnDoneStream assigned) frees the stream at the end of work. } |
| 51 |
end; |
| 52 |
{$endif} |
| 53 |
|
| 54 |
function UnzipFile(const ZipFileName, FileInZip: String): TStream; |
| 55 |
{$ifdef FPC} |
| 56 |
|
| 57 |
{ FPC version uses TUnZipper, |
| 58 |
https://www.freepascal.org/docs-html/fcl/zipper/tunzipper.html } |
| 59 |
|
| 60 |
var |
| 61 |
Unzip: TUnZipper; |
| 62 |
FilesInZipList: TStringlist; |
| 63 |
UnzipFileHelper: TUnzipFileHelper; |
| 64 |
begin |
| 65 |
Unzip := TUnZipper.Create; |
| 66 |
try |
| 67 |
Unzip.FileName := ZipFileName; |
| 68 |
UnzipFileHelper := TUnzipFileHelper.Create; |
| 69 |
try |
| 70 |
UnzipFileHelper.Stream := TMemoryStream.Create; |
| 71 |
Unzip.OnCreateStream := @UnzipFileHelper.UnzipCreateStream; |
| 72 |
Unzip.OnDoneStream := @UnzipFileHelper.UnzipDoneStream; |
| 73 |
FilesInZipList := TStringlist.Create; |
| 74 |
try |
| 75 |
FilesInZipList.Add(FileInZip); |
| 76 |
Unzip.UnZipFiles(FilesInZipList); |
| 77 |
finally FreeAndNil(FilesInZipList) end; |
| 78 |
Result := UnzipFileHelper.Stream; |
| 79 |
Result.Position := 0; // rewind, CGE reading routines expect this |
| 80 |
finally FreeAndNil(UnzipFileHelper) end; |
| 81 |
finally FreeAndNil(Unzip) end; |
| 82 |
{$else} |
| 83 |
|
| 84 |
{ Delphi version uses TZipFile, |
| 85 |
https://docwiki.embarcadero.com/Libraries/Sydney/en/System.Zip.TZipFile } |
| 86 |
|
| 87 |
var |
| 88 |
ZipFile: TZipFile; |
| 89 |
LocalHeader: TZipHeader; |
| 90 |
TempStream: TStream; |
| 91 |
begin |
| 92 |
ZipFile := TZipFile.Create; |
| 93 |
try |
| 94 |
ZipFile.Open(ZipFileName, zmRead); |
| 95 |
ZipFile.Read(FileInZip, TempStream, LocalHeader); |
| 96 |
|
| 97 |
{ We cannot just use Result := TempStream, |
| 98 |
because the stream returned by ZipFile.Read will be destroyed |
| 99 |
when ZipFile is destroyed. } |
| 100 |
|
| 101 |
Result := TMemoryStream.Create; |
| 102 |
Result.CopyFrom(TempStream, 0); |
| 103 |
Result.Position := 0; // rewind, CGE reading routines expect this |
| 104 |
finally FreeAndNil(ZipFile) end; |
| 105 |
{$endif} |
| 106 |
end; |
| 107 |
|
| 108 |
end. |