Files
dnf-utils/sgutil/sgu_mode_npk_extract_files.cpp
T
2025-09-02 22:45:41 +08:00

90 lines
2.7 KiB
C++

#include "sgu_stdafx.h"
#include "sgu_mode_npk_extract_files.h"
bool extractNPKFiles(const char* szNPKFileName, const char* szOutputDir)
{
#if 0
if (!szNPKFileName || !szOutputDir || szNPKFileName[0] == 0 || szOutputDir[0] == 0)
{
printf("Error: Invalid parameters. NPK file name and output directory must be specified.\n");
return false; // Invalid parameters
}
if (!createEmptyPath(szOutputDir))
{
printf("Error: Failed to create output directory '%s'.\n", szOutputDir);
return false;
}
NPK::INPKFile* npkFile = NPK::createNPKFile();
if (!npkFile)
{
return false; // Failed to create NPK file interface
}
std::wstring strFullName = convertAnsiStringToWide(szNPKFileName);
if (!(npkFile->openPakFile(strFullName.c_str())))
{
printf("Failed to open NPK file: '%s', Error=%s\n", szNPKFileName, NPK::getLastErrorMessage());
NPK::closeNPKFile(npkFile);
return false; // Failed to open NPK file
}
int32_t fileCount = npkFile->getFileCounts();
for (int32_t i = 0; i < fileCount; ++i)
{
NPK::FileItemBaseInfo baseInfo;
if (!npkFile->getFileItemBaseInfo(i, &baseInfo))
{
std::string outputPathName = std::string(szOutputDir) + "/" + baseInfo.keyName;
char szOutputPath[MAX_PATH];
StringCbCopyA(szOutputPath, sizeof(szOutputPath), outputPathName.c_str());
getParentPath(szOutputPath);// Get the directory part of the path
if (!forceCreatePath(szOutputPath))
{
printf("Error: Failed to create output path '%s'.\n", szOutputPath);
NPK::closeNPKFile(npkFile);
return false;
}
NPK::IFileStream* fileStream = npkFile->openFileStream(baseInfo.keyName.c_str());
if(fileStream == nullptr)
{
printf("Error: Failed to open file stream for '%s', Error=%s\n",
baseInfo.keyName.c_str(), NPK::getLastErrorMessage());
NPK::closeNPKFile(npkFile);
return false;
}
FILE* fpOutput = fopen(outputPathName.c_str(), "wb");
if (!fpOutput)
{
printf("Error: Failed to open output file '%s' for writing.\n", outputPathName.c_str());
npkFile->closeFileStream(fileStream);
NPK::closeNPKFile(npkFile);
return false; // Failed to open output file
}
char buffer[4096] = { 0 };
uint32_t bytesRead = 0;
while ((bytesRead = fileStream->read(buffer, sizeof(buffer))) > 0)
{
if (fwrite(buffer, 1, bytesRead, fpOutput) != bytesRead)
{
printf("Error: Failed to write to output file '%s'.\n", outputPathName.c_str());
fclose(fpOutput);
npkFile->closeFileStream(fileStream);
NPK::closeNPKFile(npkFile);
return false; // Failed to write to output file
}
}
fclose(fpOutput);
npkFile->closeFileStream(fileStream);
}
}
NPK::closeNPKFile(npkFile);
#endif
return true;
}