| 1 |
{ |
| 2 |
Double commander |
| 3 |
------------------------------------------------------------------------- |
| 4 |
WCX plugin for working with *.zip, *.gz, *.tar, *.tgz archives |
| 5 |
|
| 6 |
Copyright (C) 2007-2023 Alexander Koblov ([email protected]) |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or |
| 9 |
modify it under the terms of the GNU General Public License as |
| 10 |
published by the Free Software Foundation; either version 2 of the |
| 11 |
License, or (at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
in a file called COPYING along with this program; if not, write to |
| 20 |
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA |
| 21 |
02139, USA. |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
unit ZipFunc; |
| 26 |
|
| 27 |
{$mode objfpc}{$H+} |
| 28 |
{$include calling.inc} |
| 29 |
|
| 30 |
interface |
| 31 |
|
| 32 |
uses |
| 33 |
Classes, |
| 34 |
WcxPlugin, AbArcTyp, AbZipTyp, Extension, |
| 35 |
AbExcept, AbUtils, AbConst, ZipApp; |
| 36 |
|
| 37 |
type |
| 38 |
|
| 39 |
{ TAbZipKitEx } |
| 40 |
|
| 41 |
TAbZipKitEx = class (TAbZipKit) |
| 42 |
private |
| 43 |
FItemProgress: Byte; |
| 44 |
FItem: TAbArchiveItem; |
| 45 |
FNeedPassword: Boolean; |
| 46 |
FOperationResult: LongInt; |
| 47 |
FChangeVolProcW: TChangeVolProcW; |
| 48 |
FProcessDataProcW : TProcessDataProcW; |
| 49 |
procedure AbOneItemProgressEvent(Sender : TObject; Item : TAbArchiveItem; Progress : Byte; |
| 50 |
var Abort : Boolean); |
| 51 |
procedure AbArchiveItemProgressEvent(Sender : TObject; Item : TAbArchiveItem; Progress : Byte; |
| 52 |
var Abort : Boolean); |
| 53 |
procedure AbArchiveProgressEvent (Sender : TObject; Progress : Byte; var Abort : Boolean); |
| 54 |
procedure AbNeedPasswordEvent(Sender : TObject; var NewPassword : AnsiString); |
| 55 |
procedure AbProcessItemFailureEvent(Sender: TObject; Item: TAbArchiveItem; ProcessType: TAbProcessType; |
| 56 |
ErrorClass: TAbErrorClass; ErrorCode: Integer); |
| 57 |
procedure AbRequestImageEvent(Sender : TObject; ImageNumber : Integer; |
| 58 |
var ImageName : String; var Abort : Boolean); |
| 59 |
public |
| 60 |
constructor Create(AOwner: TComponent); override; |
| 61 |
end; |
| 62 |
|
| 63 |
{Mandatory functions} |
| 64 |
function OpenArchive (var ArchiveData : tOpenArchiveData) : TArcHandle;dcpcall; export; |
| 65 |
function OpenArchiveW(var ArchiveData : tOpenArchiveDataW) : TArcHandle;dcpcall; export; |
| 66 |
function ReadHeader(hArcData : TArcHandle; var HeaderData: THeaderData) : Integer;dcpcall; export; |
| 67 |
function ReadHeaderExW(hArcData : TArcHandle; var HeaderData: THeaderDataExW) : Integer;dcpcall; export; |
| 68 |
function ProcessFile (hArcData : TArcHandle; Operation : Integer; DestPath, DestName : PChar) : Integer;dcpcall; export; |
| 69 |
function ProcessFileW(hArcData : TArcHandle; Operation : Integer; DestPath, DestName : PWideChar) : Integer;dcpcall; export; |
| 70 |
function CloseArchive (hArcData : TArcHandle) : Integer;dcpcall; export; |
| 71 |
procedure SetChangeVolProc (hArcData : TArcHandle; pChangeVolProc : PChangeVolProc);dcpcall; export; |
| 72 |
procedure SetChangeVolProcW(hArcData : TArcHandle; pChangeVolProc : TChangeVolProcW);dcpcall; export; |
| 73 |
procedure SetProcessDataProc (hArcData : TArcHandle; pProcessDataProc : TProcessDataProc);dcpcall; export; |
| 74 |
procedure SetProcessDataProcW(hArcData : TArcHandle; pProcessDataProc : TProcessDataProcW);dcpcall; export; |
| 75 |
{Optional functions} |
| 76 |
function PackFilesW(PackedFile: PWideChar; SubPath: PWideChar; SrcPath: PWideChar; AddList: PWideChar; Flags: Integer): Integer;dcpcall; export; |
| 77 |
function DeleteFilesW(PackedFile, DeleteList : PWideChar) : Integer;dcpcall; export; |
| 78 |
function GetPackerCaps : Integer;dcpcall; export; |
| 79 |
function GetBackgroundFlags: Integer; dcpcall; export; |
| 80 |
procedure ConfigurePacker (Parent: HWND; DllInstance: THandle);dcpcall; export; |
| 81 |
function CanYouHandleThisFileW(FileName: PWideChar): Boolean; dcpcall; export; |
| 82 |
{Extension API} |
| 83 |
procedure ExtensionInitialize(StartupInfo: PExtensionStartupInfo); dcpcall; export; |
| 84 |
|
| 85 |
const |
| 86 |
IniFileName = 'zip.ini'; |
| 87 |
|
| 88 |
var |
| 89 |
gStartupInfo: TExtensionStartupInfo; |
| 90 |
gTarAutoHandle : Boolean; |
| 91 |
|
| 92 |
implementation |
| 93 |
|
| 94 |
uses |
| 95 |
SysUtils, LazUTF8, ZipConfDlg, AbBrowse, DCConvertEncoding, DCOSUtils, ZipOpt, |
| 96 |
ZipLng, ZipCache, DCDateTimeUtils; |
| 97 |
|
| 98 |
var |
| 99 |
PasswordCache: TPasswordCache; |
| 100 |
|
| 101 |
threadvar |
| 102 |
gProcessDataProcW : TProcessDataProcW; |
| 103 |
|
| 104 |
procedure StringToArrayW(src: UnicodeString; |
| 105 |
pDst: PWideChar; |
| 106 |
MaxDstLength: Integer); |
| 107 |
begin |
| 108 |
if Length(src) < MaxDstLength then |
| 109 |
MaxDstLength := Length(src) |
| 110 |
else |
| 111 |
MaxDstLength := MaxDstLength - 1; // for ending #0 |
| 112 |
|
| 113 |
if Length(src) > 0 then |
| 114 |
Move(src[1], pDst^, SizeOf(WideChar) * MaxDstLength); |
| 115 |
pDst[MaxDstLength] := WideChar(0); |
| 116 |
end; |
| 117 |
|
| 118 |
function StrEndsWith(S : String; SearchPhrase : String) : Boolean; |
| 119 |
begin |
| 120 |
Result := (RightStr(S, Length(SearchPhrase)) = SearchPhrase); |
| 121 |
end; |
| 122 |
|
| 123 |
function GetArchiveError(const E : Exception): Integer; |
| 124 |
begin |
| 125 |
if E is EAbUserAbort then |
| 126 |
Result := E_EABORTED |
| 127 |
else if E is EAbFileNotFound then |
| 128 |
Result := E_EOPEN |
| 129 |
else if E is EAbUnhandledType then |
| 130 |
Result := E_UNKNOWN_FORMAT |
| 131 |
else if E is EFCreateError then |
| 132 |
Result := E_ECREATE |
| 133 |
else if E is EFOpenError then |
| 134 |
Result := E_EOPEN |
| 135 |
else if E is EReadError then |
| 136 |
Result := E_EREAD |
| 137 |
else if E is EWriteError then |
| 138 |
Result := E_EWRITE |
| 139 |
else |
| 140 |
Result := E_UNKNOWN; |
| 141 |
end; |
| 142 |
|
| 143 |
procedure CheckError(Arc: TAbZipKitEx; E: Exception; const FileName: String); |
| 144 |
var |
| 145 |
AMessage: String; |
| 146 |
begin |
| 147 |
if (Arc.FOperationResult = E_UNKNOWN) then |
| 148 |
begin |
| 149 |
AMessage:= E.Message + LineEnding + LineEnding + FileName; |
| 150 |
if gStartupInfo.MessageBox(PAnsiChar(AMessage), nil, MB_OKCANCEL or MB_ICONERROR) = ID_OK then |
| 151 |
Arc.FOperationResult:= E_HANDLED |
| 152 |
else begin |
| 153 |
Arc.FOperationResult:= E_EABORTED; |
| 154 |
end; |
| 155 |
end; |
| 156 |
end; |
| 157 |
|
| 158 |
// -- Exported functions ------------------------------------------------------ |
| 159 |
|
| 160 |
function OpenArchive (var ArchiveData : tOpenArchiveData) : TArcHandle;dcpcall; export; |
| 161 |
begin |
| 162 |
Result := 0; |
| 163 |
ArchiveData.OpenResult := E_NOT_SUPPORTED; |
| 164 |
end; |
| 165 |
|
| 166 |
function OpenArchiveW(var ArchiveData : tOpenArchiveDataW) : TArcHandle;dcpcall; export; |
| 167 |
var |
| 168 |
Arc : TAbZipKitEx; |
| 169 |
begin |
| 170 |
Result := 0; |
| 171 |
Arc := TAbZipKitEx.Create(nil); |
| 172 |
try |
| 173 |
Arc.OnArchiveProgress := @Arc.AbArchiveProgressEvent; |
| 174 |
Arc.OnProcessItemFailure := @Arc.AbProcessItemFailureEvent; |
| 175 |
Arc.OnNeedPassword:= @Arc.AbNeedPasswordEvent; |
| 176 |
Arc.OnRequestImage:= @Arc.AbRequestImageEvent; |
| 177 |
|
| 178 |
Arc.TarAutoHandle := gTarAutoHandle; |
| 179 |
Arc.OpenArchive(UTF16ToUTF8(UnicodeString(ArchiveData.ArcName))); |
| 180 |
|
| 181 |
if Arc.ArchiveType in [atGzip, atBzip2, atXz, atLzma, atZstd] then |
| 182 |
Arc.OnArchiveItemProgress := @Arc.AbOneItemProgressEvent |
| 183 |
else begin |
| 184 |
Arc.OnArchiveItemProgress := @Arc.AbArchiveItemProgressEvent; |
| 185 |
end; |
| 186 |
|
| 187 |
Arc.Password := PasswordCache.GetPassword(Arc.FileName); |
| 188 |
Arc.Tag := 0; |
| 189 |
Result := TArcHandle(Arc); |
| 190 |
except |
| 191 |
on E: Exception do |
| 192 |
begin |
| 193 |
Arc.Free; |
| 194 |
ArchiveData.OpenResult := GetArchiveError(E); |
| 195 |
if (ArchiveData.OpenResult = E_UNKNOWN) then |
| 196 |
begin |
| 197 |
ArchiveData.OpenResult := E_HANDLED; |
| 198 |
gStartupInfo.MessageBox(PAnsiChar(E.Message), nil, MB_OK or MB_ICONERROR); |
| 199 |
end; |
| 200 |
end; |
| 201 |
end; |
| 202 |
end; |
| 203 |
|
| 204 |
function ReadHeader(hArcData : TArcHandle; var HeaderData: THeaderData) : Integer;dcpcall; export; |
| 205 |
begin |
| 206 |
Result := E_NOT_SUPPORTED; |
| 207 |
end; |
| 208 |
|
| 209 |
function ReadHeaderExW(hArcData : TArcHandle; var HeaderData: THeaderDataExW) : Integer;dcpcall; export; |
| 210 |
var |
| 211 |
sFileName : String; |
| 212 |
Arc : TAbZipKitEx absolute hArcData; |
| 213 |
begin |
| 214 |
if Arc.Tag > Arc.Count - 1 then |
| 215 |
Exit(E_END_ARCHIVE); |
| 216 |
|
| 217 |
sFileName := Arc.GetFileName(Arc.Tag); |
| 218 |
|
| 219 |
StringToArrayW(CeUtf8ToUtf16(sFileName), @HeaderData.FileName, SizeOf(HeaderData.FileName)); |
| 220 |
|
| 221 |
with Arc.Items[Arc.Tag] do |
| 222 |
begin |
| 223 |
HeaderData.PackSize := Lo(CompressedSize); |
| 224 |
HeaderData.PackSizeHigh := Hi(CompressedSize); |
| 225 |
HeaderData.UnpSize := Lo(UncompressedSize); |
| 226 |
HeaderData.UnpSizeHigh := Hi(UncompressedSize); |
| 227 |
HeaderData.FileCRC := CRC32; |
| 228 |
HeaderData.FileTime := NativeLastModFileTime; |
| 229 |
HeaderData.FileAttr := NativeFileAttributes; |
| 230 |
HeaderData.MfileTime := DateTimeToWinFileTime(LastModTimeAsDateTime); |
| 231 |
|
| 232 |
if IsEncrypted then begin |
| 233 |
HeaderData.Flags := RHDF_ENCRYPTED; |
| 234 |
end; |
| 235 |
|
| 236 |
if IsDirectory then begin |
| 237 |
HeaderData.FileAttr := HeaderData.FileAttr or faFolder; |
| 238 |
end; |
| 239 |
end; |
| 240 |
|
| 241 |
Result := E_SUCCESS; |
| 242 |
end; |
| 243 |
|
| 244 |
function ProcessFile (hArcData : TArcHandle; Operation : Integer; DestPath, DestName : PChar) : Integer;dcpcall; export; |
| 245 |
begin |
| 246 |
Result := E_NOT_SUPPORTED; |
| 247 |
end; |
| 248 |
|
| 249 |
function ProcessFileW(hArcData : TArcHandle; Operation : Integer; DestPath, DestName : PWideChar) : Integer;dcpcall; export; |
| 250 |
var |
| 251 |
Abort: Boolean; |
| 252 |
DestNameUtf8: String; |
| 253 |
Arc : TAbZipKitEx absolute hArcData; |
| 254 |
begin |
| 255 |
try |
| 256 |
Arc.FOperationResult := E_SUCCESS; |
| 257 |
|
| 258 |
case Operation of |
| 259 |
PK_TEST: |
| 260 |
begin |
| 261 |
Arc.TestItemAt(Arc.Tag); |
| 262 |
|
| 263 |
if (Arc.FNeedPassword) and (Arc.FOperationResult = E_SUCCESS) and Arc.Items[Arc.Tag].IsEncrypted then |
| 264 |
begin |
| 265 |
Arc.FNeedPassword:= False; |
| 266 |
PasswordCache.SetPassword(Arc.FileName, Arc.Password); |
| 267 |
end; |
| 268 |
|
| 269 |
// Show progress and ask if aborting. |
| 270 |
if Assigned(Arc.FProcessDataProcW) then |
| 271 |
begin |
| 272 |
Abort := False; |
| 273 |
Arc.OnArchiveItemProgress(Arc, Arc.Items[Arc.Tag], 100, Abort); |
| 274 |
if Abort then Arc.FOperationResult := E_EABORTED; |
| 275 |
end; |
| 276 |
end; |
| 277 |
|
| 278 |
PK_EXTRACT: |
| 279 |
begin |
| 280 |
DestNameUtf8 := UTF16ToUTF8(UnicodeString(DestName)); |
| 281 |
if (DestPath <> nil) and (DestPath[0] <> #0) then |
| 282 |
Arc.BaseDirectory := DestPath |
| 283 |
else begin |
| 284 |
Arc.BaseDirectory := ExtractFilePath(DestNameUtf8); |
| 285 |
end; |
| 286 |
repeat |
| 287 |
Arc.FOperationResult := E_SUCCESS; |
| 288 |
Arc.ExtractAt(Arc.Tag, DestNameUtf8); |
| 289 |
until (Arc.FOperationResult <> maxLongint); |
| 290 |
|
| 291 |
if (Arc.FNeedPassword) and (Arc.FOperationResult = E_SUCCESS) and Arc.Items[Arc.Tag].IsEncrypted then |
| 292 |
begin |
| 293 |
Arc.FNeedPassword:= False; |
| 294 |
PasswordCache.SetPassword(Arc.FileName, Arc.Password); |
| 295 |
end; |
| 296 |
|
| 297 |
// Show progress and ask if aborting. |
| 298 |
if Assigned(Arc.FProcessDataProcW) then |
| 299 |
begin |
| 300 |
Abort := False; |
| 301 |
Arc.OnArchiveItemProgress(Arc, Arc.Items[Arc.Tag], 100, Abort); |
| 302 |
if Abort then Arc.FOperationResult := E_EABORTED; |
| 303 |
end; |
| 304 |
end; |
| 305 |
|
| 306 |
PK_SKIP: |
| 307 |
begin |
| 308 |
|
| 309 |
end; |
| 310 |
end; {case} |
| 311 |
|
| 312 |
except |
| 313 |
on E: Exception do |
| 314 |
begin |
| 315 |
Arc.FOperationResult := GetArchiveError(E); |
| 316 |
if (Operation = PK_TEST) then CheckError(Arc, E, Arc.Items[Arc.Tag].FileName); |
| 317 |
end; |
| 318 |
end; |
| 319 |
|
| 320 |
Result:= Arc.FOperationResult; |
| 321 |
Arc.Tag := Arc.Tag + 1; |
| 322 |
end; |
| 323 |
|
| 324 |
function CloseArchive (hArcData : TArcHandle) : Integer;dcpcall; export; |
| 325 |
var |
| 326 |
Arc : TAbZipKitEx absolute hArcData; |
| 327 |
begin |
| 328 |
Arc.CloseArchive; |
| 329 |
FreeAndNil(Arc); |
| 330 |
Result := E_SUCCESS; |
| 331 |
end; |
| 332 |
|
| 333 |
procedure SetChangeVolProc (hArcData : TArcHandle; pChangeVolProc : PChangeVolProc);dcpcall; export; |
| 334 |
begin |
| 335 |
end; |
| 336 |
|
| 337 |
procedure SetChangeVolProcW(hArcData : TArcHandle; pChangeVolProc : TChangeVolProcW);dcpcall; export; |
| 338 |
var |
| 339 |
Arc : TAbZipKitEx absolute hArcData; |
| 340 |
begin |
| 341 |
if (hArcData <> wcxInvalidHandle) then |
| 342 |
begin |
| 343 |
Arc.FChangeVolProcW := pChangeVolProc; |
| 344 |
end; |
| 345 |
end; |
| 346 |
|
| 347 |
procedure SetProcessDataProc (hArcData : TArcHandle; pProcessDataProc : TProcessDataProc);dcpcall; export; |
| 348 |
begin |
| 349 |
end; |
| 350 |
|
| 351 |
procedure SetProcessDataProcW(hArcData : TArcHandle; pProcessDataProc : TProcessDataProcW);dcpcall; export; |
| 352 |
var |
| 353 |
Arc : TAbZipKitEx absolute hArcData; |
| 354 |
begin |
| 355 |
if (hArcData <> wcxInvalidHandle) then // if archive is open |
| 356 |
Arc.FProcessDataProcW := pProcessDataProc |
| 357 |
else begin // if archive is close |
| 358 |
gProcessDataProcW := pProcessDataProc; |
| 359 |
end; |
| 360 |
end; |
| 361 |
|
| 362 |
{Optional functions} |
| 363 |
|
| 364 |
function PackFilesW(PackedFile: PWideChar; SubPath: PWideChar; SrcPath: PWideChar; AddList: PWideChar; Flags: Integer): Integer;dcpcall; export; |
| 365 |
var |
| 366 |
FileExt: String; |
| 367 |
FilePath: String; |
| 368 |
Arc : TAbZipKitEx; |
| 369 |
FileName: UnicodeString; |
| 370 |
sPassword: AnsiString; |
| 371 |
sPackedFile: String; |
| 372 |
ArchiveFormat: TArchiveFormat; |
| 373 |
begin |
| 374 |
if (Flags and PK_PACK_MOVE_FILES) <> 0 then begin |
| 375 |
Exit(E_NOT_SUPPORTED); |
| 376 |
end; |
| 377 |
|
| 378 |
Arc := TAbZipKitEx.Create(nil); |
| 379 |
try |
| 380 |
Arc.AutoSave := False; |
| 381 |
Arc.TarAutoHandle:= True; |
| 382 |
Arc.FProcessDataProcW := gProcessDataProcW; |
| 383 |
Arc.OnProcessItemFailure := @Arc.AbProcessItemFailureEvent; |
| 384 |
|
| 385 |
sPackedFile := UTF16ToUTF8(UnicodeString(PackedFile)); |
| 386 |
|
| 387 |
try |
| 388 |
FileExt:= LowerCase(ExtractFileExt(sPackedFile)); |
| 389 |
|
| 390 |
if ((Flags and PK_PACK_ENCRYPT) <> 0) then |
| 391 |
begin |
| 392 |
// Only zip/zipx supports encryption |
| 393 |
if (FileExt = '.zip') or (FileExt = '.zipx') then |
| 394 |
begin |
| 395 |
sPassword:= EmptyStr; |
| 396 |
Arc.AbNeedPasswordEvent(Arc, sPassword); |
| 397 |
Arc.Password:= sPassword; |
| 398 |
end; |
| 399 |
end; |
| 400 |
|
| 401 |
Arc.OpenArchive(sPackedFile); |
| 402 |
|
| 403 |
ArchiveFormat:= ARCHIVE_FORMAT[Arc.ArchiveType]; |
| 404 |
|
| 405 |
if (ArchiveFormat = afZip) then |
| 406 |
begin |
| 407 |
if (FileExt = '.zipx') then |
| 408 |
ArchiveFormat:= afZipx |
| 409 |
else begin |
| 410 |
case PluginConfig[ArchiveFormat].Level of |
| 411 |
1: Arc.DeflationOption:= doSuperFast; |
| 412 |
3: Arc.DeflationOption:= doFast; |
| 413 |
6: Arc.DeflationOption:= doNormal; |
| 414 |
9: Arc.DeflationOption:= doMaximum; |
| 415 |
end; |
| 416 |
case PluginConfig[ArchiveFormat].Method of |
| 417 |
PtrInt(cmStored): Arc.CompressionMethodToUse:= smStored; |
| 418 |
PtrInt(cmDeflated): Arc.CompressionMethodToUse:= smDeflated; |
| 419 |
PtrInt(cmEnhancedDeflated): Arc.CompressionMethodToUse:= smBestMethod; |
| 420 |
end; |
| 421 |
end; |
| 422 |
end; |
| 423 |
Arc.ZipArchive.CompressionLevel:= PluginConfig[ArchiveFormat].Level; |
| 424 |
Arc.ZipArchive.CompressionMethod:= PluginConfig[ArchiveFormat].Method; |
| 425 |
|
| 426 |
Arc.OnArchiveProgress := @Arc.AbArchiveProgressEvent; |
| 427 |
Arc.StoreOptions := Arc.StoreOptions + [soReplace]; |
| 428 |
|
| 429 |
Arc.BaseDirectory := UTF16ToUTF8(UnicodeString(SrcPath)); |
| 430 |
|
| 431 |
FilePath:= UTF16ToUTF8(UnicodeString(SubPath)); |
| 432 |
while True do |
| 433 |
begin |
| 434 |
FileName := UnicodeString(AddList); |
| 435 |
Arc.Archive.AddEntry(UTF16ToUTF8(FileName), FilePath); |
| 436 |
if (AddList + Length(FileName) + 1)^ = #0 then |
| 437 |
Break; |
| 438 |
Inc(AddList, Length(FileName) + 1); |
| 439 |
end; |
| 440 |
|
| 441 |
if Arc.ArchiveType in [atGzip, atBzip2, atXz, atLzma, atZstd] then |
| 442 |
begin |
| 443 |
with Arc.Archive.ItemList[0] do |
| 444 |
begin |
| 445 |
UncompressedSize := mbFileSize(DiskFileName); |
| 446 |
end; |
| 447 |
Arc.OnArchiveItemProgress := @Arc.AbOneItemProgressEvent |
| 448 |
end |
| 449 |
else begin |
| 450 |
Arc.OnArchiveItemProgress := @Arc.AbArchiveItemProgressEvent; |
| 451 |
end; |
| 452 |
|
| 453 |
Arc.Save; |
| 454 |
Arc.CloseArchive; |
| 455 |
except |
| 456 |
on E: Exception do |
| 457 |
begin |
| 458 |
Arc.FOperationResult := GetArchiveError(E); |
| 459 |
CheckError(Arc, E, sPackedFile); |
| 460 |
end; |
| 461 |
end; |
| 462 |
finally |
| 463 |
Result := Arc.FOperationResult; |
| 464 |
FreeAndNil(Arc); |
| 465 |
end; |
| 466 |
end; |
| 467 |
|
| 468 |
function DeleteFilesW(PackedFile, DeleteList : PWideChar) : Integer;dcpcall; export; |
| 469 |
var |
| 470 |
Arc : TAbZipKitEx; |
| 471 |
FileNameUTF8 : String; |
| 472 |
pFileName : PWideChar; |
| 473 |
FileName : UnicodeString; |
| 474 |
ArchiveFormat: TArchiveFormat; |
| 475 |
begin |
| 476 |
Arc := TAbZipKitEx.Create(nil); |
| 477 |
try |
| 478 |
Arc.TarAutoHandle:= True; |
| 479 |
Arc.FProcessDataProcW := gProcessDataProcW; |
| 480 |
Arc.OnProcessItemFailure := @Arc.AbProcessItemFailureEvent; |
| 481 |
Arc.OnNeedPassword:= @Arc.AbNeedPasswordEvent; |
| 482 |
|
| 483 |
try |
| 484 |
Arc.OpenArchive(UTF16ToUTF8(UnicodeString(PackedFile))); |
| 485 |
|
| 486 |
ArchiveFormat:= ARCHIVE_FORMAT[Arc.ArchiveType]; |
| 487 |
|
| 488 |
Arc.ZipArchive.CompressionLevel:= PluginConfig[ArchiveFormat].Level; |
| 489 |
|
| 490 |
// Set this after opening archive, to get only progress of deleting. |
| 491 |
Arc.OnArchiveItemProgress := @Arc.AbArchiveItemProgressEvent; |
| 492 |
Arc.OnArchiveProgress := @Arc.AbArchiveProgressEvent; |
| 493 |
|
| 494 |
// Parse file list. |
| 495 |
pFileName := DeleteList; |
| 496 |
while pFileName^ <> #0 do |
| 497 |
begin |
| 498 |
FileName := pFileName; // Convert PWideChar to UnicodeString (up to first #0). |
| 499 |
|
| 500 |
FileNameUTF8 := UTF16ToUTF8(FileName); |
| 501 |
|
| 502 |
// If ends with '.../*.*' or '.../' then delete directory. |
| 503 |
if StrEndsWith(FileNameUTF8, PathDelim + '*.*') or |
| 504 |
StrEndsWith(FileNameUTF8, PathDelim) |
| 505 |
then |
| 506 |
Arc.DeleteDirectoriesRecursively(ExtractFilePath(FileNameUTF8)) |
| 507 |
else |
| 508 |
Arc.DeleteFile(FileNameUTF8); |
| 509 |
|
| 510 |
pFileName := pFileName + Length(FileName) + 1; // move after filename and ending #0 |
| 511 |
if pFileName^ = #0 then |
| 512 |
Break; // end of list |
| 513 |
end; |
| 514 |
|
| 515 |
Arc.Save; |
| 516 |
Arc.CloseArchive; |
| 517 |
except |
| 518 |
on E: Exception do |
| 519 |
begin |
| 520 |
Arc.FOperationResult := GetArchiveError(E); |
| 521 |
CheckError(Arc, E, Arc.FileName); |
| 522 |
end; |
| 523 |
end; |
| 524 |
finally |
| 525 |
Result := Arc.FOperationResult; |
| 526 |
FreeAndNil(Arc); |
| 527 |
end; |
| 528 |
end; |
| 529 |
|
| 530 |
function GetPackerCaps : Integer;dcpcall; export; |
| 531 |
begin |
| 532 |
Result := PK_CAPS_NEW or PK_CAPS_DELETE or PK_CAPS_MODIFY |
| 533 |
or PK_CAPS_MULTIPLE or PK_CAPS_OPTIONS or PK_CAPS_BY_CONTENT |
| 534 |
or PK_CAPS_ENCRYPT; |
| 535 |
end; |
| 536 |
|
| 537 |
function GetBackgroundFlags: Integer; dcpcall; export; |
| 538 |
begin |
| 539 |
Result:= BACKGROUND_UNPACK or BACKGROUND_PACK; |
| 540 |
end; |
| 541 |
|
| 542 |
procedure ConfigurePacker(Parent: HWND; DllInstance: THandle);dcpcall; export; |
| 543 |
begin |
| 544 |
CreateZipConfDlg; |
| 545 |
end; |
| 546 |
|
| 547 |
function CanYouHandleThisFileW(FileName: PWideChar): Boolean; dcpcall; export; |
| 548 |
begin |
| 549 |
try |
| 550 |
Result:= (AbDetermineArcType(UTF16ToUTF8(UnicodeString(FileName)), atUnknown) <> atUnknown); |
| 551 |
except |
| 552 |
Result := False; |
| 553 |
end; |
| 554 |
end; |
| 555 |
|
| 556 |
procedure ExtensionInitialize(StartupInfo: PExtensionStartupInfo); dcpcall; export; |
| 557 |
begin |
| 558 |
gStartupInfo:= StartupInfo^; |
| 559 |
// Load configuration from ini file |
| 560 |
LoadConfiguration; |
| 561 |
TranslateResourceStrings; |
| 562 |
// Create password cache object |
| 563 |
PasswordCache:= TPasswordCache.Create; |
| 564 |
end; |
| 565 |
|
| 566 |
{ TAbZipKitEx } |
| 567 |
|
| 568 |
constructor TAbZipKitEx.Create(AOwner: TComponent); |
| 569 |
begin |
| 570 |
inherited Create(AOwner); |
| 571 |
|
| 572 |
FOperationResult := E_SUCCESS; |
| 573 |
FProcessDataProcW := nil; |
| 574 |
|
| 575 |
TempDirectory := GetTempDir; |
| 576 |
end; |
| 577 |
|
| 578 |
procedure TAbZipKitEx.AbProcessItemFailureEvent(Sender: TObject; |
| 579 |
Item: TAbArchiveItem; ProcessType: TAbProcessType; |
| 580 |
ErrorClass: TAbErrorClass; ErrorCode: Integer); |
| 581 |
var |
| 582 |
Message: AnsiString; |
| 583 |
begin |
| 584 |
// Unknown error |
| 585 |
FOperationResult:= E_UNKNOWN; |
| 586 |
// Check error class |
| 587 |
if (ErrorClass = ecAbbrevia) then |
| 588 |
begin |
| 589 |
case ErrorCode of |
| 590 |
AbUserAbort: |
| 591 |
FOperationResult:= E_EABORTED; |
| 592 |
AbZipBadCRC: |
| 593 |
FOperationResult:= E_BAD_ARCHIVE; |
| 594 |
AbFileNotFound: |
| 595 |
FOperationResult:= E_NO_FILES; |
| 596 |
AbReadError: |
| 597 |
FOperationResult:= E_EREAD; |
| 598 |
end; |
| 599 |
end |
| 600 |
// Has exception message? Show it! |
| 601 |
else if Assigned(ExceptObject) and (ExceptObject is Exception) then |
| 602 |
begin |
| 603 |
Message := Exception(ExceptObject).Message; |
| 604 |
if Assigned(Item) then Message += LineEnding + LineEnding + Item.FileName; |
| 605 |
if (ProcessType = ptExtract) then |
| 606 |
begin |
| 607 |
case gStartupInfo.MessageBox(PAnsiChar(Message), nil, MB_ABORTRETRYIGNORE or MB_ICONERROR) of |
| 608 |
ID_RETRY: |
| 609 |
FOperationResult:= maxLongint; |
| 610 |
ID_IGNORE: |
| 611 |
FOperationResult:= E_HANDLED; |
| 612 |
ID_ABORT: |
| 613 |
raise EAbUserAbort.Create; |
| 614 |
end; |
| 615 |
end |
| 616 |
else begin |
| 617 |
if gStartupInfo.MessageBox(PAnsiChar(Message), nil, MB_OKCANCEL or MB_ICONERROR) = ID_OK then |
| 618 |
FOperationResult:= E_HANDLED |
| 619 |
else begin |
| 620 |
raise EAbUserAbort.Create; |
| 621 |
end; |
| 622 |
end; |
| 623 |
end |
| 624 |
// Check error class |
| 625 |
else case ErrorClass of |
| 626 |
ecFileOpenError: |
| 627 |
begin |
| 628 |
ErrorClass:= ecAbbrevia; |
| 629 |
ErrorCode:= AbFCIFileOpenError; |
| 630 |
FOperationResult:= E_EOPEN; |
| 631 |
end; |
| 632 |
ecFileCreateError: |
| 633 |
begin |
| 634 |
ErrorClass:= ecAbbrevia; |
| 635 |
ErrorCode:= AbFCICreateError; |
| 636 |
FOperationResult:= E_ECREATE; |
| 637 |
end; |
| 638 |
end; |
| 639 |
// Show abbrevia specific errors |
| 640 |
if (ErrorClass = ecAbbrevia) and (ProcessType in [ptAdd, ptFreshen, ptReplace]) then |
| 641 |
begin |
| 642 |
Message:= AbStrRes(ErrorCode) + LineEnding + LineEnding + Item.FileName; |
| 643 |
if gStartupInfo.MessageBox(PAnsiChar(Message), nil, MB_OKCANCEL or MB_ICONERROR) = ID_OK then |
| 644 |
FOperationResult:= E_HANDLED |
| 645 |
else begin |
| 646 |
raise EAbUserAbort.Create; |
| 647 |
end; |
| 648 |
end; |
| 649 |
end; |
| 650 |
|
| 651 |
procedure TAbZipKitEx.AbRequestImageEvent(Sender: TObject; |
| 652 |
ImageNumber: Integer; var ImageName: String; var Abort: Boolean); |
| 653 |
var |
| 654 |
AVolume: array[0..MAX_PATH] of WideChar; |
| 655 |
begin |
| 656 |
if (not mbFileExists(ImageName)) and Assigned(FChangeVolProcW) then |
| 657 |
begin |
| 658 |
StrPCopy(AVolume, CeUtf8ToUtf16(ImageName)); |
| 659 |
Abort := (FChangeVolProcW(AVolume, PK_VOL_ASK) = 0); |
| 660 |
if not Abort then ImageName:= CeUtf16ToUtf8(UnicodeString(AVolume)); |
| 661 |
end; |
| 662 |
end; |
| 663 |
|
| 664 |
procedure TAbZipKitEx.AbOneItemProgressEvent(Sender: TObject; |
| 665 |
Item: TAbArchiveItem; Progress: Byte; var Abort: Boolean); |
| 666 |
var |
| 667 |
ASize: Int64; |
| 668 |
begin |
| 669 |
if Assigned(FProcessDataProcW) then |
| 670 |
begin |
| 671 |
ASize := Item.UncompressedSize; |
| 672 |
if ASize = 0 then |
| 673 |
ASize := -Progress |
| 674 |
else if FItemProgress = Progress then |
| 675 |
ASize := 0 |
| 676 |
else begin |
| 677 |
ASize := (Int64(Progress) - Int64(FItemProgress)) * ASize div 100; |
| 678 |
if ASize > High(Int32) then ASize := -Progress; |
| 679 |
FItemProgress := Progress; |
| 680 |
end; |
| 681 |
Abort := (FProcessDataProcW(PWideChar(CeUtf8ToUtf16(Item.FileName)), ASize) = 0); |
| 682 |
end; |
| 683 |
end; |
| 684 |
|
| 685 |
procedure TAbZipKitEx.AbArchiveItemProgressEvent(Sender: TObject; |
| 686 |
Item: TAbArchiveItem; Progress: Byte; var Abort: Boolean); |
| 687 |
var |
| 688 |
ASize: Int64; |
| 689 |
begin |
| 690 |
if Assigned(FProcessDataProcW) then |
| 691 |
begin |
| 692 |
if (Item = nil) then |
| 693 |
Abort := (FProcessDataProcW(nil, -(Progress + 1000)) = 0) |
| 694 |
else begin |
| 695 |
if Item.IsDirectory then |
| 696 |
ASize:= 0 |
| 697 |
else if Item.UncompressedSize = 0 then |
| 698 |
ASize:= -(Progress + 1000) |
| 699 |
else begin |
| 700 |
if FItem <> Item then |
| 701 |
begin |
| 702 |
FItem := Item; |
| 703 |
FItemProgress := 0; |
| 704 |
end; |
| 705 |
if FItemProgress = Progress then |
| 706 |
ASize := 0 |
| 707 |
else begin |
| 708 |
ASize := Item.UncompressedSize; |
| 709 |
ASize := (Int64(Progress) - Int64(FItemProgress)) * ASize div 100; |
| 710 |
if ASize > High(Int32) then ASize := -(Progress + 1000); |
| 711 |
FItemProgress := Progress; |
| 712 |
end; |
| 713 |
end; |
| 714 |
Abort := (FProcessDataProcW(PWideChar(CeUtf8ToUtf16(Item.FileName)), ASize) = 0) |
| 715 |
end; |
| 716 |
end; |
| 717 |
end; |
| 718 |
|
| 719 |
procedure TAbZipKitEx.AbArchiveProgressEvent(Sender: TObject; |
| 720 |
Progress: Byte; var Abort: Boolean); |
| 721 |
begin |
| 722 |
try |
| 723 |
if Assigned(FProcessDataProcW) then |
| 724 |
Abort := (FProcessDataProcW(nil, -(Progress)) = 0); |
| 725 |
except |
| 726 |
Abort := True; |
| 727 |
end; |
| 728 |
end; |
| 729 |
|
| 730 |
procedure TAbZipKitEx.AbNeedPasswordEvent(Sender: TObject; |
| 731 |
var NewPassword: AnsiString); |
| 732 |
var |
| 733 |
aNewPassword: array[0..MAX_PATH-1] of AnsiChar; |
| 734 |
Result: Boolean; |
| 735 |
begin |
| 736 |
aNewPassword := Copy(NewPassword, 1, MAX_PATH); |
| 737 |
Result:= gStartupInfo.InputBox('Zip', 'Please enter the password:', True, PAnsiChar(aNewPassword), MAX_PATH); |
| 738 |
if Result then |
| 739 |
NewPassword := aNewPassword |
| 740 |
else begin |
| 741 |
raise EAbUserAbort.Create; |
| 742 |
end; |
| 743 |
FNeedPassword:= True; |
| 744 |
end; |
| 745 |
|
| 746 |
end. |
| 747 |
|