Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
liveldy authored Jun 23, 2024
1 parent 89e5363 commit 7b8b6da
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/extractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "..\header\extractor.h"
#include "..\lib\ProgressBar\ProgressBar.h"

namespace fs = std::filesystem;

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

fs::path zipFilePath = apkFilePath;
zipFilePath.replace_extension(".zip");
fs::copy(apkFilePath, zipFilePath, fs::copy_options::overwrite_existing);

fs::path extractedDir = apkFilePath.parent_path() / "apk_extracted";
if (fs::exists(extractedDir)) {
fs::remove_all(extractedDir);
}
fs::create_directory(extractedDir);

std::string command = "tar -xf " + zipFilePath.string() + " -C " + extractedDir.string();
std::system(command.c_str());

std::string outname = apkFilePath.string() + "_apkout";
fs::path outputDir = apkFilePath.parent_path() / outname;
if (fs::exists(outputDir)) {
fs::remove_all(outputDir);
}
fs::create_directory(outputDir);

extractMediaFiles(extractedDir, outputDir);

fs::remove(zipFilePath);
fs::remove_all(extractedDir);

std::cout << std::endl << "Resource extraction completed." << std::endl;
}

bool ApkExtractor::isImageFile(const std::string& extension) {
static const std::vector<std::string> imageExtensions = {".jpg", ".jpeg", ".png", ".gif", ".bmp"};
return std::find(imageExtensions.begin(), imageExtensions.end(), extension) != imageExtensions.end();
}

bool ApkExtractor::isVideoFile(const std::string& extension) {
static const std::vector<std::string> videoExtensions = {".mp4", ".avi", ".mov", ".mkv", ".flv"};
return std::find(videoExtensions.begin(), videoExtensions.end(), extension) != videoExtensions.end();
}

bool ApkExtractor::isAudioFile(const std::string& extension) {
static const std::vector<std::string> audioExtensions = {".mp3", ".wav", ".ogg", ".flac"};
return std::find(audioExtensions.begin(), audioExtensions.end(), extension) != audioExtensions.end();
}

void ApkExtractor::createDirectories(const fs::path& baseOutputDir, const fs::path& relativePath) {
fs::path fullPath = baseOutputDir / relativePath;
if (!fs::exists(fullPath)) {
fs::create_directories(fullPath);
}
}

void ApkExtractor::copyFile(const fs::path& filePath, const fs::path& outputDir, const fs::path& relativePath) {
fs::path fullOutputPath = outputDir / relativePath;
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), '=');
for (const auto& entry : fs::recursive_directory_iterator(extractedDir)) {
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();

if (isImageFile(extension)) {
createDirectories(outputDir / "images", relativePath.parent_path());
copyFile(entry.path(), outputDir / "images", relativePath);
} else if (isVideoFile(extension)) {
createDirectories(outputDir / "videos", relativePath.parent_path());
copyFile(entry.path(), outputDir / "videos", relativePath);
} else if (isAudioFile(extension)) {
createDirectories(outputDir / "audios", 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]);
}
}

0 comments on commit 7b8b6da

Please sign in to comment.