Skip to content

Commit

Permalink
Update extractor.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
liveldy authored Jun 24, 2024
1 parent df8487b commit 08bddc6
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

namespace fs = std::filesystem;

void ApkExtractor::extractResourcesFromApk(const std::string& apkPath) {
void ApkExtractor::extractResourcesFromApk(const std::string& apkPath, bool flattenStructure) {
fs::path apkFilePath(apkPath);
if (!fs::exists(apkFilePath)) {
std::cerr << "APK file not found: " << apkPath << std::endl;
return;
}

std::cout << "Initializing..." << std::endl;

fs::path zipFilePath = apkFilePath;
zipFilePath.replace_extension(".zip");
Expand All @@ -22,15 +24,15 @@ void ApkExtractor::extractResourcesFromApk(const std::string& apkPath) {

std::string command = "tar -xf " + zipFilePath.string() + " -C " + extractedDir.string();
std::system(command.c_str());
std::string outname = apkFilePath.string() + "_apkout";
std::string outname = apkFilePath.stem().string() + "_apkout";
fs::path outputDir = apkFilePath.parent_path() / outname;
if (fs::exists(outputDir)) {
fs::remove_all(outputDir);
}
fs::create_directory(outputDir);

extractMediaFiles(extractedDir, outputDir);
extractMediaFiles(extractedDir, outputDir, flattenStructure);

fs::remove(zipFilePath);
fs::remove_all(extractedDir);
Expand Down Expand Up @@ -65,34 +67,39 @@ void ApkExtractor::copyFile(const fs::path& filePath, const fs::path& outputDir,
fs::copy(filePath, fullOutputPath, fs::copy_options::overwrite_existing);
}

void ApkExtractor::extractMediaFiles(const fs::path& extractedDir, const fs::path& outputDir) {
size_t allfile=0, thefile=0;
for (const auto& entry : fs::recursive_directory_iterator(extractedDir))allfile++;
ProgressBar pb(0, int(allfile), '=');
void ApkExtractor::extractMediaFiles(const fs::path& extractedDir, const fs::path& outputDir, bool flattenStructure) {
size_t allfile = 0, thefile = 0;
for (const auto& entry : fs::recursive_directory_iterator(extractedDir)) allfile++;
ProgressBar pb(0, int(allfile), '=');

for (const auto& entry : fs::recursive_directory_iterator(extractedDir)) {
pb.setCurrentValue(++thefile);
pb.display();
pb.setCurrentValue(++thefile);
pb.display();

if (fs::is_regular_file(entry.path())) {
fs::path relativePath = fs::relative(entry.path(), extractedDir);
std::string extension = entry.path().extension().string();
fs::path fileName = entry.path().filename();
fs::path relativePath = flattenStructure ? fileName : fs::relative(entry.path(), extractedDir);

if (isImageFile(extension)) {
createDirectories(outputDir / "images", relativePath.parent_path());
createDirectories(outputDir / "images", flattenStructure ? "" : relativePath.parent_path());
copyFile(entry.path(), outputDir / "images", relativePath);
} else if (isVideoFile(extension)) {
createDirectories(outputDir / "videos", relativePath.parent_path());
createDirectories(outputDir / "videos", flattenStructure ? "" : relativePath.parent_path());
copyFile(entry.path(), outputDir / "videos", relativePath);
} else if (isAudioFile(extension)) {
createDirectories(outputDir / "audios", relativePath.parent_path());
createDirectories(outputDir / "audios", flattenStructure ? "" : relativePath.parent_path());
copyFile(entry.path(), outputDir / "audios", relativePath);
}
}
}
}



void extractor(int argc,char**argv){
if(argc>2){
if(std::string(argv[2])=="apk"&&fs::exists(argv[3]))ApkExtractor::extractResourcesFromApk(argv[3]);
if(argc==4&&std::string(argv[2])=="apk"&&fs::exists(argv[3]))ApkExtractor::extractResourcesFromApk(argv[3]);
if(argc==5&&std::string(argv[2])=="apk"&&fs::exists(argv[3])&&std::string(argv[4])=="-r")ApkExtractor::extractResourcesFromApk(argv[3],true);
}
}
}

0 comments on commit 08bddc6

Please sign in to comment.