163 lines
4.7 KiB
ObjectPascal
163 lines
4.7 KiB
ObjectPascal
program jarunpacker;
|
||
|
||
{$mode delphi}
|
||
{$codepage UTF8}
|
||
uses
|
||
Interfaces,
|
||
Classes,
|
||
Windows,
|
||
FWZipReader,
|
||
SysUtils,
|
||
ParamsMngr,
|
||
LazUTF8Classes,
|
||
FWZipConsts,
|
||
FWZipWriter,
|
||
FWZipZLib,
|
||
SimplyJSON,
|
||
LazFileUtils,
|
||
ANBFormatString;
|
||
|
||
{$R *.res}
|
||
resourcestring
|
||
msg_consoletitle = 'JAR Unpacker Tool';
|
||
msg_noparametrsfound = #13#10 + '[ОШИБКА] Параметры не заданы!';
|
||
msg_extractingfile = 'Извлекаю файл: %s';
|
||
msg_buildingfile = 'Сжимаю файл: %s';
|
||
|
||
procedure ExtractJAR(const JARName, TargetDir, ListFileName: string);
|
||
var
|
||
Reader: TFWZipReader;
|
||
zItem: TFWZipReaderItem;
|
||
FS: TFileStreamUTF8;
|
||
FL: TStringList;
|
||
Indx: integer;
|
||
begin
|
||
FS := TFileStreamUTF8.Create(JARName, fmOpenRead + fmShareExclusive);
|
||
FL := TStringList.Create;
|
||
Reader := TFWZipReader.Create;
|
||
try
|
||
Reader.LoadFromStream(FS);
|
||
for Indx := 0 to Reader.Count - 1 do
|
||
begin
|
||
zItem := Reader.Item[Indx];
|
||
FL.Add(FormatStr(zItem.FileName + '=' + zItem.FileName, ['/'], ['\']));
|
||
end;
|
||
Reader.ExtractAll(TargetDir);
|
||
finally
|
||
Reader.Free;
|
||
if Trim(ListFileName) <> '' then
|
||
FL.SaveToFile(ListFileName);
|
||
FreeAndNil(FL);
|
||
FS.Free;
|
||
end;
|
||
end;
|
||
|
||
procedure CompressJAR(const JARName, SourceDir, ListFileName: string);
|
||
var
|
||
Writer: TFWZipWriter;
|
||
FS: TFileStreamUTF8;
|
||
FL: TStringList;
|
||
begin
|
||
if FileExistsUTF8(JARName) then
|
||
DeleteFileUTF8(JARName);
|
||
FS := TFileStreamUTF8.Create(JARName, fmCreate);
|
||
FL := TStringList.Create;
|
||
FL.LoadFromFile(ListFileName);
|
||
SetCurrentDir(SourceDir);
|
||
Writer := TFWZipWriter.Create(clMax);
|
||
try
|
||
Writer.AddFilesAndFolders(FL, True);
|
||
Writer.BuildZip(FS);
|
||
finally
|
||
Writer.Free;
|
||
FreeAndNil(FL);
|
||
FS.Free;
|
||
end;
|
||
end;
|
||
|
||
procedure WriteHelpExtract();
|
||
begin
|
||
WriteLn('Допустимые параметры команды extract:');
|
||
WriteLn(' target - папка извлечения;');
|
||
WriteLn(' listFile - имя файла для списка распакованных файлов.');
|
||
WriteLn('');
|
||
WriteLn('Пример:');
|
||
WriteLn('/extract="d:\file.jar" /target="d:\toextract\" /listFile="d:\1.txt"');
|
||
end;
|
||
|
||
procedure WriteHelpCompress();
|
||
begin
|
||
WriteLn('Допустимые параметры команды compress:');
|
||
WriteLn(' from - папка с файлами для архива;');
|
||
WriteLn(' filesList - имя файла со списком файлов для упаковки.');
|
||
WriteLn('');
|
||
WriteLn('Пример:');
|
||
WriteLn('/compress="d:\file.jar" /from="d:\toextract\" /filesList="d:\1.txt"');
|
||
end;
|
||
|
||
procedure WriteHelp();
|
||
begin
|
||
WriteLn('Допустимые параметры:');
|
||
WriteLn(' extract - извлечение jar-архива;');
|
||
WriteLn(' compres - сжатие jar-ахива.');
|
||
WriteLn('');
|
||
WriteHelpExtract;
|
||
WriteLn('');
|
||
WriteHelpCompress();
|
||
WriteLn(msg_noparametrsfound);
|
||
end;
|
||
|
||
var
|
||
isHandled: boolean;
|
||
JARName, TargetDir, ListFileName: string;
|
||
begin
|
||
SetConsoleTitle(PChar(msg_consoletitle));
|
||
WriteLn('--------------------------------------------------------------------');
|
||
WriteLn('****************Утилита упаковки/распаковки JAR файлов**************');
|
||
WriteLn('*************************Версия: 2023.01.31*************************');
|
||
WriteLn('**********Авторские права (c) 2016 - 2023, Александр Бабаев*********');
|
||
WriteLn('--------------------------------------------------------------------');
|
||
isHandled := False;
|
||
if HasParam('extract') then
|
||
begin
|
||
if not (HasParam('target') and HasParam('listFile')) then
|
||
begin
|
||
WriteHelpExtract();
|
||
ReadLn;
|
||
isHandled := True;
|
||
end
|
||
else
|
||
begin
|
||
JARName := GetParamValue('extract');
|
||
TargetDir := IncludeTrailingBackslash(GetParamValue('target'));
|
||
ListFileName := GetParamValue('listFile');
|
||
ExtractJAR(JARName, TargetDir, ListFileName);
|
||
WriteLn(Format(msg_extractingfile, [JARName]));
|
||
isHandled := True;
|
||
end;
|
||
end;
|
||
if HasParam('compress') then
|
||
begin
|
||
if not (HasParam('from') and HasParam('filesList')) then
|
||
begin
|
||
WriteHelpExtract();
|
||
ReadLn;
|
||
isHandled := True;
|
||
end
|
||
else
|
||
begin
|
||
JARName := GetParamValue('compress');
|
||
TargetDir := GetParamValue('from');
|
||
ListFileName := GetParamValue('filesList');
|
||
CompressJAR(JARName, TargetDir, ListFileName);
|
||
WriteLn(Format(msg_buildingfile, [JARName]));
|
||
isHandled := True;
|
||
end;
|
||
end;
|
||
if not isHandled then
|
||
begin
|
||
WriteHelp();
|
||
ReadLn;
|
||
end;
|
||
end.
|