Skip to content

Commit

Permalink
Implement Smart Extraction (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
R-YaTian authored Jan 13, 2025
1 parent 2d5178f commit a4fd9bf
Show file tree
Hide file tree
Showing 33 changed files with 254 additions and 32 deletions.
16 changes: 15 additions & 1 deletion NanaZip.Core/SevenZip/CPP/7zip/UI/Common/ArchiveCommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ enum Enum
kUseSlashMark,
kDisableWildcardParsing,
kElimDup,
// **************** NanaZip Modification Start ****************
kSmartExtract,
// **************** NanaZip Modification End ****************
kFullPathMode,

kHardLinks,
Expand Down Expand Up @@ -309,6 +312,9 @@ static const CSwitchForm kSwitchForms[] =
{ "spm", SWFRM_STRING_SINGL(0) },
{ "spd", SWFRM_SIMPLE },
{ "spe", SWFRM_MINUS },
// **************** NanaZip Modification Start ****************
{ "sps", SWFRM_MINUS },
// **************** NanaZip Modification End ****************
{ "spf", SWFRM_STRING_SINGL(0) },

{ "snh", SWFRM_MINUS },
Expand Down Expand Up @@ -1329,7 +1335,15 @@ void CArcCmdLineParser::Parse2(CArcCmdLineOptions &options)
options.ExtractOptions.ElimDup.Def = true;
options.ExtractOptions.ElimDup.Val = !parser[NKey::kElimDup].WithMinus;
}


// **************** NanaZip Modification Start ****************
if (parser[NKey::kSmartExtract].ThereIs)
{
options.ExtractOptions.SmartExtract.Def = true;
options.ExtractOptions.SmartExtract.Val = !parser[NKey::kSmartExtract].WithMinus;
}
// **************** NanaZip Modification End ****************

NWildcard::ECensorPathMode censorPathMode = NWildcard::k_RelatPath;
bool fullPathMode = parser[NKey::kFullPathMode].ThereIs;
if (fullPathMode)
Expand Down
25 changes: 24 additions & 1 deletion NanaZip.Core/SevenZip/CPP/7zip/UI/Common/Extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,30 @@ static HRESULT DecompressArchive(

realIndices.Add(i);
}


// **************** NanaZip Modification Start ****************
if (options.SmartExtract.Val)
{
UInt32 firstLevelCount = 0;
for (UInt32 i = 0; i < numItems; i++)
{
RINOK(arc.GetItem(i, item));
const UString &path =
#ifdef SUPPORT_ALT_STREAMS
item.MainPath;
#else
item.Path;
#endif
if (path.Find(L'/') == -1 && path.Find(L'\\') == -1)
firstLevelCount++;
if (firstLevelCount > 1)
break;
}
if (firstLevelCount > 1)
outDir += replaceName;
}
// **************** NanaZip Modification End ****************

if (realIndices.Size() == 0)
{
callback->ThereAreNoFiles();
Expand Down
3 changes: 3 additions & 0 deletions NanaZip.Core/SevenZip/CPP/7zip/UI/Common/Extract.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
struct CExtractOptionsBase
{
CBoolPair ElimDup;
// **************** NanaZip Modification Start ****************
CBoolPair SmartExtract;
// **************** NanaZip Modification End ****************

bool ExcludeDirItems;
bool ExcludeFileItems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ enum Enum
kUseSlashMark,
kDisableWildcardParsing,
kElimDup,
// **************** NanaZip Modification Start ****************
kSmartExtract,
// **************** NanaZip Modification End ****************
kFullPathMode,

kHardLinks,
Expand Down Expand Up @@ -302,6 +305,9 @@ static const CSwitchForm kSwitchForms[] =
{ "spm", SWFRM_STRING_SINGL(0) },
{ "spd", SWFRM_SIMPLE },
{ "spe", SWFRM_MINUS },
// **************** NanaZip Modification Start ****************
{ "sps", SWFRM_MINUS },
// **************** NanaZip Modification End ****************
{ "spf", SWFRM_STRING_SINGL(0) },

{ "snh", SWFRM_MINUS },
Expand Down Expand Up @@ -1256,6 +1262,14 @@ void CArcCmdLineParser::Parse2(CArcCmdLineOptions &options)
options.ExtractOptions.ElimDup.Val = !parser[NKey::kElimDup].WithMinus;
}

// **************** NanaZip Modification Start ****************
if (parser[NKey::kSmartExtract].ThereIs)
{
options.ExtractOptions.SmartExtract.Def = true;
options.ExtractOptions.SmartExtract.Val = !parser[NKey::kSmartExtract].WithMinus;
}
// **************** NanaZip Modification End ****************

NWildcard::ECensorPathMode censorPathMode = NWildcard::k_RelatPath;
bool fullPathMode = parser[NKey::kFullPathMode].ThereIs;
if (fullPathMode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ static void ExtractGroupCommand(const UStringVector &arcPaths, UString &params,
ErrorMessageHRESULT(result);
}

void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup, UInt32 writeZone)
// **************** NanaZip Modification Start ****************
// void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup, UInt32 writeZone);
void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup, UInt32 writeZone, bool smartExtract)
// **************** NanaZip Modification End ****************
{
MY_TRY_BEGIN
UString params ('x');
Expand All @@ -345,6 +348,10 @@ void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bo
}
if (elimDup)
params += " -spe";
// **************** NanaZip Modification Start ****************
if (smartExtract)
params += " -sps";
// **************** NanaZip Modification End ****************
if (writeZone != (UInt32)(Int32)-1)
{
params += " -snz";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ HRESULT CompressFiles(
const UStringVector &names,
bool email, bool showDialog, bool waitFinish);

void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup, UInt32 writeZone);
// **************** NanaZip Modification Start ****************
// void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup, UInt32 writeZone);
void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup, UInt32 writeZone, bool smartExtract = false);
// **************** NanaZip Modification End ****************
void TestArchives(const UStringVector &arcPaths, bool hashMode = false);

void CalcChecksum(const UStringVector &paths,
Expand Down
23 changes: 23 additions & 0 deletions NanaZip.UI.Classic/SevenZip/CPP/7zip/UI/Common/Extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,29 @@ static HRESULT DecompressArchive(
realIndices.Add(i);
}

// **************** NanaZip Modification Start ****************
if (options.SmartExtract.Val)
{
UInt32 firstLevelCount = 0;
for (UInt32 i = 0; i < numItems; i++)
{
RINOK(arc.GetItem(i, item));
const UString &path =
#ifdef SUPPORT_ALT_STREAMS
item.MainPath;
#else
item.Path;
#endif
if (path.Find(L'/') == -1 && path.Find(L'\\') == -1)
firstLevelCount++;
if (firstLevelCount > 1)
break;
}
if (firstLevelCount > 1)
outDir += replaceName;
}
// **************** NanaZip Modification End ****************

if (realIndices.Size() == 0)
{
callback->ThereAreNoFiles();
Expand Down
3 changes: 3 additions & 0 deletions NanaZip.UI.Classic/SevenZip/CPP/7zip/UI/Common/Extract.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
struct CExtractOptionsBase
{
CBoolPair ElimDup;
// **************** NanaZip Modification Start ****************
CBoolPair SmartExtract;
// **************** NanaZip Modification End ****************

bool ExcludeDirItems;
bool ExcludeFileItems;
Expand Down
20 changes: 19 additions & 1 deletion NanaZip.UI.Classic/SevenZip/CPP/7zip/UI/Explorer/ContextMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ static const CContextMenuCommand g_Commands[] =
CMD_REC( kOpen, "Open", IDS_CONTEXT_OPEN),
CMD_REC( kExtract, "Extract", IDS_CONTEXT_EXTRACT),
CMD_REC( kExtractHere, "ExtractHere", IDS_CONTEXT_EXTRACT_HERE),
// **************** NanaZip Modification Start ****************
CMD_REC( kExtractHereSmart, "ExtractHereSmart", IDS_CONTEXT_EXTRACT_HERE_SMART),
// **************** NanaZip Modification End ****************
CMD_REC( kExtractTo, "ExtractTo", IDS_CONTEXT_EXTRACT_TO),
CMD_REC( kTest, "Test", IDS_CONTEXT_TEST),
CMD_REC( kCompress, "Compress", IDS_CONTEXT_COMPRESS),
Expand Down Expand Up @@ -753,6 +756,17 @@ STDMETHODIMP CZipContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu,
MyInsertMenu(popupMenu, subIndex++, currentCommandID++, mainString, bitmap);
}

// **************** NanaZip Modification Start ****************
if ((contextMenuFlags & NContextMenuFlags::kExtractHereSmart) != 0)
{
// Extract Here
CCommandMapItem cmi;
cmi.Folder = baseFolder;
AddCommand(kExtractHereSmart, mainString, cmi);
MyInsertMenu(popupMenu, subIndex++, currentCommandID++, mainString, bitmap);
}
// **************** NanaZip Modification End ****************

if ((contextMenuFlags & NContextMenuFlags::kExtractTo) != 0)
{
// Extract To
Expand Down Expand Up @@ -1179,15 +1193,19 @@ HRESULT CZipContextMenu::InvokeCommandCommon(const CCommandMapItem &cmi)
}
case kExtract:
case kExtractHere:
// **************** NanaZip Modification Start ****************
case kExtractHereSmart:
case kExtractTo:
{
ExtractArchives(_fileNames, cmi.Folder,
(cmdID == kExtract), // showDialog
(cmdID == kExtractTo) && _elimDup.Val, // elimDup
_writeZone
_writeZone,
(cmdID == kExtractHereSmart)
);
break;
}
// **************** NanaZip Modification End ****************
case kTest:
{
TestArchives(_fileNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class CZipContextMenu:
kOpen,
kExtract,
kExtractHere,
// **************** NanaZip Modification Start ****************
kExtractHereSmart,
// **************** NanaZip Modification End ****************
kExtractTo,
kTest,
kCompress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace NContextMenuFlags
const UInt32 kExtract = 1 << 0;
const UInt32 kExtractHere = 1 << 1;
const UInt32 kExtractTo = 1 << 2;
// **************** NanaZip Modification Start ****************
const UInt32 kExtractHereSmart = 1 << 3;
// **************** NanaZip Modification End ****************

const UInt32 kTest = 1 << 4;
const UInt32 kOpen = 1 << 5;
Expand Down
3 changes: 3 additions & 0 deletions NanaZip.UI.Classic/SevenZip/CPP/7zip/UI/Explorer/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
#define IDS_CONTEXT_COMPRESS_TO 2328
#define IDS_CONTEXT_COMPRESS_EMAIL 2329
#define IDS_CONTEXT_COMPRESS_TO_EMAIL 2330
// **************** NanaZip Modification Start ****************
#define IDS_CONTEXT_EXTRACT_HERE_SMART 2331
// **************** NanaZip Modification End ****************

#define IDB_MENU_LOGO 190
3 changes: 3 additions & 0 deletions NanaZip.UI.Classic/SevenZip/CPP/7zip/UI/Explorer/resource2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ BEGIN
IDS_CONTEXT_COMPRESS "Add to archive..."
IDS_CONTEXT_TEST "Test archive"
IDS_CONTEXT_EXTRACT_HERE "Extract Here"
// **************** NanaZip Modification Start ****************
IDS_CONTEXT_EXTRACT_HERE_SMART "Extract Here (Smart)"
// **************** NanaZip Modification End ****************
IDS_CONTEXT_EXTRACT_TO "Extract to {0}"
IDS_CONTEXT_COMPRESS_TO "Add to {0}"
IDS_CONTEXT_COMPRESS_EMAIL "Compress and email..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ static const CContextMenuItem kMenuItems[] =

{ IDS_CONTEXT_EXTRACT, kExtract },
{ IDS_CONTEXT_EXTRACT_HERE, kExtractHere },
// **************** NanaZip Modification Start ****************
{ IDS_CONTEXT_EXTRACT_HERE_SMART, kExtractHereSmart },
// **************** NanaZip Modification End ****************
{ IDS_CONTEXT_EXTRACT_TO, kExtractTo },

{ IDS_CONTEXT_COMPRESS, kCompress },
Expand Down
19 changes: 18 additions & 1 deletion NanaZip.UI.Modern/NanaZip.ShellExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ namespace NanaZip::ShellExtension

Extract,
ExtractHere,
ExtractHereSmart,
ExtractTo,

Compress,
Expand Down Expand Up @@ -442,6 +443,7 @@ namespace NanaZip::ShellExtension
}
case CommandID::Extract:
case CommandID::ExtractHere:
case CommandID::ExtractHereSmart:
case CommandID::ExtractTo:
{
if (!NeedExtract)
Expand All @@ -461,7 +463,8 @@ namespace NanaZip::ShellExtension
(this->m_CommandID == CommandID::Extract),
((this->m_CommandID == CommandID::ExtractTo)
&& this->m_ElimDup.Val),
this->m_WriteZone);
this->m_WriteZone,
(this->m_CommandID == CommandID::ExtractHereSmart));

break;
}
Expand Down Expand Up @@ -765,6 +768,20 @@ namespace NanaZip::ShellExtension
ContextMenuWriteZone));
}

if (ContextMenuFlags & NContextMenuFlags::kExtractHereSmart)
{
UString TranslatedString;
LangString(IDS_CONTEXT_EXTRACT_HERE_SMART, TranslatedString);
this->m_SubCommands.push_back(
winrt::make<ExplorerCommandBase>(
std::wstring(
TranslatedString.Ptr(),
TranslatedString.Len()),
CommandID::ExtractHereSmart,
ContextMenuElimDup,
ContextMenuWriteZone));
}

if (ContextMenuFlags & NContextMenuFlags::kExtractTo)
{
UString TranslatedString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ enum Enum
kUseSlashMark,
kDisableWildcardParsing,
kElimDup,
// **************** NanaZip Modification Start ****************
kSmartExtract,
// **************** NanaZip Modification End ****************
kFullPathMode,

kHardLinks,
Expand Down Expand Up @@ -302,6 +305,9 @@ static const CSwitchForm kSwitchForms[] =
{ "spm", SWFRM_STRING_SINGL(0) },
{ "spd", SWFRM_SIMPLE },
{ "spe", SWFRM_MINUS },
// **************** NanaZip Modification Start ****************
{ "sps", SWFRM_MINUS },
// **************** NanaZip Modification End ****************
{ "spf", SWFRM_STRING_SINGL(0) },

{ "snh", SWFRM_MINUS },
Expand Down Expand Up @@ -1256,6 +1262,14 @@ void CArcCmdLineParser::Parse2(CArcCmdLineOptions &options)
options.ExtractOptions.ElimDup.Val = !parser[NKey::kElimDup].WithMinus;
}

// **************** NanaZip Modification Start ****************
if (parser[NKey::kSmartExtract].ThereIs)
{
options.ExtractOptions.SmartExtract.Def = true;
options.ExtractOptions.SmartExtract.Val = !parser[NKey::kSmartExtract].WithMinus;
}
// **************** NanaZip Modification End ****************

NWildcard::ECensorPathMode censorPathMode = NWildcard::k_RelatPath;
bool fullPathMode = parser[NKey::kFullPathMode].ThereIs;
if (fullPathMode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ static void ExtractGroupCommand(const UStringVector &arcPaths, UString &params,
ErrorMessageHRESULT(result);
}

void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup, UInt32 writeZone)
// **************** NanaZip Modification Start ****************
// void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup, UInt32 writeZone);
void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bool showDialog, bool elimDup, UInt32 writeZone, bool smartExtract)
// **************** NanaZip Modification End ****************
{
MY_TRY_BEGIN
UString params ('x');
Expand All @@ -345,6 +348,10 @@ void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder, bo
}
if (elimDup)
params += " -spe";
// **************** NanaZip Modification Start ****************
if (smartExtract)
params += " -sps";
// **************** NanaZip Modification End ****************
if (writeZone != (UInt32)(Int32)-1)
{
params += " -snz";
Expand Down
Loading

0 comments on commit a4fd9bf

Please sign in to comment.