| 1 |
(* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* Version: MPL 1.1 |
| 3 |
* |
| 4 |
* The contents of this file are subject to the Mozilla Public License Version |
| 5 |
* 1.1 (the "License"); you may not use this file except in compliance with |
| 6 |
* the License. You may obtain a copy of the License at |
| 7 |
* http://www.mozilla.org/MPL/ |
| 8 |
* |
| 9 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 10 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 11 |
* for the specific language governing rights and limitations under the |
| 12 |
* License. |
| 13 |
* |
| 14 |
* The Original Code is TurboPower Abbrevia |
| 15 |
* |
| 16 |
* The Initial Developer of the Original Code is |
| 17 |
* TurboPower Software |
| 18 |
* |
| 19 |
* Portions created by the Initial Developer are Copyright (C) 1997-2002 |
| 20 |
* the Initial Developer. All Rights Reserved. |
| 21 |
* |
| 22 |
* Contributor(s): |
| 23 |
* |
| 24 |
* ***** END LICENSE BLOCK ***** *) |
| 25 |
|
| 26 |
{*********************************************************} |
| 27 |
{* ABBREVIA: ZipApp.pas *} |
| 28 |
{*********************************************************} |
| 29 |
{* ABBREVIA: Additional classes and routines *} |
| 30 |
{*********************************************************} |
| 31 |
|
| 32 |
unit ZipApp; |
| 33 |
|
| 34 |
{$mode objfpc}{$H+} |
| 35 |
|
| 36 |
interface |
| 37 |
|
| 38 |
uses |
| 39 |
Classes, SysUtils, AbArcTyp, AbZipKit, AbUtils; |
| 40 |
|
| 41 |
const |
| 42 |
{$IF DEFINED(MSWINDOWS)} |
| 43 |
faFolder = faDirectory; |
| 44 |
{$ELSE} |
| 45 |
faFolder = AB_FMODE_DIR or AB_FPERMISSION_GENERIC or AB_FPERMISSION_OWNEREXECUTE; |
| 46 |
{$ENDIF} |
| 47 |
|
| 48 |
type |
| 49 |
{ TAbArchiveItemHelper } |
| 50 |
|
| 51 |
TAbArchiveItemHelper = class helper for TAbArchiveItem |
| 52 |
function MatchesPath(const Path : String; Recursive : Boolean = False) : Boolean; |
| 53 |
function MatchesPathEx(const Paths : String; Recursive : Boolean = False) : Boolean; |
| 54 |
end; |
| 55 |
|
| 56 |
{ TAbArchiveAccess } |
| 57 |
|
| 58 |
TAbArchiveAccess = class(TAbArchive) |
| 59 |
|
| 60 |
end; |
| 61 |
|
| 62 |
{ TAbZipKit } |
| 63 |
|
| 64 |
TAbZipKit = class(TAbCustomZipKit) |
| 65 |
public |
| 66 |
{en |
| 67 |
Delete one file from archive |
| 68 |
} |
| 69 |
procedure DeleteFile(const aFileName : String); |
| 70 |
{en |
| 71 |
Get the normalized file name |
| 72 |
} |
| 73 |
function GetFileName(aFileIndex: Integer): String; |
| 74 |
{en |
| 75 |
Delete directory entry and all file and directory entries matching |
| 76 |
the same path recursively |
| 77 |
} |
| 78 |
procedure DeleteDirectoriesRecursively(const Paths : String); |
| 79 |
{en |
| 80 |
Test specific item in the archive |
| 81 |
} |
| 82 |
procedure TestItemAt(Index : Integer); |
| 83 |
end; |
| 84 |
|
| 85 |
{en |
| 86 |
See if DirPath matches PathToMatch. |
| 87 |
If Recursive=True it is allowed for DirPath to point to a subdirectory of PathToMatch, |
| 88 |
for example: PathToMatch = 'dir/', DirPath = 'dir/subdir' - Result is True. |
| 89 |
} |
| 90 |
function AbDirMatch(DirPath : String; PathToMatch : String; Recursive : Boolean) : Boolean; |
| 91 |
{en |
| 92 |
From a list of paths separated with AbPathSep (';') extracts a path from |
| 93 |
the position StartPos (counted from 1) and modifies StartPos to point to next entry. |
| 94 |
When no more entries are found, returns empty string. |
| 95 |
} |
| 96 |
function AbExtractEntry(const Entries : String; var StartPos : Integer) : String; |
| 97 |
|
| 98 |
implementation |
| 99 |
|
| 100 |
uses |
| 101 |
AbExcept, DCStrUtils; |
| 102 |
|
| 103 |
{ TAbArchiveItemHelper } |
| 104 |
|
| 105 |
function TAbArchiveItemHelper.MatchesPath(const Path: String; Recursive: Boolean): Boolean; |
| 106 |
var |
| 107 |
Value : string; |
| 108 |
Drive, Dir, Name : string; |
| 109 |
begin |
| 110 |
Value := Path; |
| 111 |
if (Value <> '') and (RightStr(Value, 1) <> AbPathDelim) then |
| 112 |
Value := Value + AbPathDelim; |
| 113 |
AbUnfixName(Value); |
| 114 |
AbParseFileName(Path, Drive, Dir, Name); |
| 115 |
Value := Dir + Name; |
| 116 |
Name := FileName; |
| 117 |
AbUnfixName(Name); |
| 118 |
Result := AbDirMatch(Name, Value, Recursive); |
| 119 |
end; |
| 120 |
|
| 121 |
function TAbArchiveItemHelper.MatchesPathEx(const Paths: String; Recursive: Boolean): Boolean; |
| 122 |
var |
| 123 |
Position: Integer; |
| 124 |
Path: String; |
| 125 |
begin |
| 126 |
Result := True; |
| 127 |
|
| 128 |
Position := 1; |
| 129 |
while True do |
| 130 |
begin |
| 131 |
Path := AbExtractEntry(Paths, Position); |
| 132 |
if Path = '' then Break; |
| 133 |
if MatchesPath(Path, Recursive) then Exit; |
| 134 |
end; |
| 135 |
|
| 136 |
Result := False; |
| 137 |
end; |
| 138 |
|
| 139 |
{ TAbZipKit } |
| 140 |
|
| 141 |
procedure TAbZipKit.DeleteFile(const aFileName: String); |
| 142 |
var |
| 143 |
I : Integer; |
| 144 |
begin |
| 145 |
TAbArchiveAccess(Archive).CheckValid; |
| 146 |
if Count > 0 then |
| 147 |
begin |
| 148 |
for I := Pred(Count) downto 0 do |
| 149 |
begin |
| 150 |
with Archive.ItemList[I] do |
| 151 |
begin |
| 152 |
if CompareStr(GetFileName(I), aFileName) = 0 then |
| 153 |
begin |
| 154 |
DeleteAt(I); |
| 155 |
Break; |
| 156 |
end; |
| 157 |
end; |
| 158 |
end; |
| 159 |
end; |
| 160 |
end; |
| 161 |
|
| 162 |
function TAbZipKit.GetFileName(aFileIndex: Integer): String; |
| 163 |
begin |
| 164 |
Result := Items[aFileIndex].FileName; |
| 165 |
if (ArchiveType in [atGzip, atGzippedTar]) and (Result = 'unknown') then |
| 166 |
begin |
| 167 |
Result := ExtractOnlyFileName(FileName); |
| 168 |
if (ArchiveType = atGzippedTar) then |
| 169 |
begin |
| 170 |
if (TarAutoHandle = False) and (ExtractOnlyFileExt(Result) <> 'tar') then |
| 171 |
Result := Result + '.tar'; |
| 172 |
end; |
| 173 |
end; |
| 174 |
DoDirSeparators(Result); |
| 175 |
Result := ExcludeFrontPathDelimiter(Result); |
| 176 |
Result := ExcludeTrailingPathDelimiter(Result); |
| 177 |
while StrBegins(Result, '..' + PathDelim) do |
| 178 |
begin |
| 179 |
Result := Copy(Result, 4, MaxInt); |
| 180 |
Result := ExcludeFrontPathDelimiter(Result); |
| 181 |
end; |
| 182 |
if StrEnds(Result, PathDelim + '..') then |
| 183 |
begin |
| 184 |
Result[Length(Result)] := '_'; |
| 185 |
Result[Length(Result) - 1] := '_'; |
| 186 |
end; |
| 187 |
Result := StringReplace(Result, PathDelim + '..' + PathDelim, PathDelim + '__' + PathDelim, [rfReplaceAll]); |
| 188 |
end; |
| 189 |
|
| 190 |
procedure TAbZipKit.DeleteDirectoriesRecursively(const Paths: String); |
| 191 |
var |
| 192 |
I : Integer; |
| 193 |
begin |
| 194 |
TAbArchiveAccess(Archive).CheckValid; |
| 195 |
if Count > 0 then |
| 196 |
begin |
| 197 |
for I := Pred(Count) downto 0 do |
| 198 |
begin |
| 199 |
with Archive.ItemList[I] do |
| 200 |
if MatchesPathEx(Paths, True) then |
| 201 |
DeleteAt(I); |
| 202 |
end; |
| 203 |
end; |
| 204 |
end; |
| 205 |
|
| 206 |
procedure TAbZipKit.TestItemAt(Index: Integer); |
| 207 |
begin |
| 208 |
if (Archive <> nil) then |
| 209 |
TAbArchiveAccess(Archive).TestAt(Index) |
| 210 |
else |
| 211 |
raise EAbNoArchive.Create; |
| 212 |
end; |
| 213 |
|
| 214 |
function AbDirMatch(DirPath : String; PathToMatch : String; Recursive : Boolean) : Boolean; |
| 215 |
begin |
| 216 |
if Recursive then |
| 217 |
PathToMatch := PathToMatch + '*'; // append wildcard |
| 218 |
|
| 219 |
Result := AbPatternMatch(DirPath, 1, PathToMatch, 1); |
| 220 |
end; |
| 221 |
|
| 222 |
function AbExtractEntry(const Entries : String; var StartPos : Integer) : String; |
| 223 |
var |
| 224 |
I : Integer; |
| 225 |
Len: Integer; |
| 226 |
begin |
| 227 |
Result := ''; |
| 228 |
Len := Length(Entries); |
| 229 |
I := StartPos; |
| 230 |
if (I >= 1) and (I <= Len) then |
| 231 |
begin |
| 232 |
while (I <= Len) and (Entries[I] <> AbPathSep) do Inc(I); |
| 233 |
Result := Copy(Entries, StartPos, I - StartPos); |
| 234 |
if (I <= Len) and (Entries[I] = AbPathSep) then Inc(I); |
| 235 |
|
| 236 |
StartPos := I; |
| 237 |
end; |
| 238 |
end; |
| 239 |
|
| 240 |
end. |
| 241 |
|