Files
MyZip/main.cpp
T
2026-08-31 16:51:47 +09:00

171 lines
4.6 KiB
C++

#include <cstdint>
#include <cstdio>
#include <iostream>
#include <filesystem>
#include <Windows.h>
#include <debugapi.h>
#include "MyZip.h"
static uint32_t g_recursive_counter = 0;
bool FileReader(MyZip &zip, std::string strInputPath, const std::filesystem::path &rootPath)
{
using namespace std;
bool bRet = true;
// path class obj 생성.
filesystem::path pathObj(strInputPath);
// file valid check
if (!filesystem::exists(pathObj))
{
printf("[ERROR](FileReader) Something went Wrong..!! \r\n");
printf("[ERROR] File not found : %s ", pathObj.string().c_str());
bRet = false;
}
enum class eFILETYPE
{
isFile, isDir, ETC
};
// distinguish file type
eFILETYPE eFileDist;
if (filesystem::is_directory(pathObj)) eFileDist = eFILETYPE::isDir;
else if (filesystem::is_regular_file(pathObj)) eFileDist = eFILETYPE::isFile;
else eFileDist = eFILETYPE::ETC;
switch (eFileDist)
{
// case 1 : input one file
case eFILETYPE::isFile:
{
// TODO : export to debugging log function
string strDbg = "[INFO] Current Entry (File)\t: " + pathObj.string();
strDbg += "\t Counter : " + to_string(g_recursive_counter);
strDbg += "\r\n";
OutputDebugStringA(strDbg.c_str());
auto absPathOjb = filesystem::absolute(pathObj);
bRet = zip.AddFileToZip(absPathOjb.generic_string(), pathObj.generic_string());
break;
}
// case 2 : input directory
case eFILETYPE::isDir:
{
// make iterator to traversal
filesystem::directory_iterator itrDir(pathObj);
g_recursive_counter++;
while (itrDir != filesystem::end(itrDir))
{
// get entry one by one
const filesystem::directory_entry& currentEntry = *itrDir;
const filesystem::path& currentAbsPath = currentEntry.path();
if (filesystem::is_directory(currentEntry))
{
// TODO : export to debugging log function
string strDbg = "[INFO] Current Entry (Dir)\t: " + currentAbsPath.string();
strDbg += "\t Counter : " + to_string(g_recursive_counter);
strDbg += "\r\n";
OutputDebugStringA(strDbg.c_str());
// recursive traversal
// TODO : bRet 쓰는 곳이 없음. 파일 처리 실패 시 어떻게 할 건지 정해야 함.
bRet = FileReader(zip, currentAbsPath.string(), rootPath);
}
else if (filesystem::is_regular_file(currentEntry))
{
// TODO : export to debugging log function
string strDbg = "[INFO] Current Entry (File)\t: " + currentAbsPath.string();
strDbg += "\t Counter : " + to_string(g_recursive_counter);
strDbg += "\r\n";
OutputDebugStringA(strDbg.c_str());
// archiving file
auto relativePath = filesystem::relative(currentAbsPath, rootPath);
// generic_string : make back-slash to OS independant delimeter ("\\" -> "/")
// TODO : bRet 쓰는 곳이 없음. 파일 처리 실패 시 어떻게 할 건지 정해야 함.
bRet = zip.AddFileToZip(currentAbsPath.generic_string(), relativePath.generic_string());
}
else
{
printf("[ERROR](FileReader) Something went Wrong..!! \r\n");
printf("[ERROR](FileReader) Maybe its corrupted file. \r\n");
bRet = false;
}
// step forward entry
itrDir++;
}
g_recursive_counter--;
break;
}
// case 3 : ERROR
case eFILETYPE::ETC:
{
printf("[ERROR](FileReader) Something went Wrong..!! \r\n");
printf("[ERROR](FileReader) Maybe its corrupted file. \r\n");
bRet = false;
break;
}
default:
break;
};
// TODO : 실패 상황 별 다른 코드 부여? + 코드 별 처리 어떻게 할 건지?
return bRet;
}
int main(int argc, char* argv[])
{
using namespace std;
int iRet = 0;
printf("==================== Hello Zip! ====================\r\n");
printf("=== Please enter Target Path ===\r\n");
printf("=== (FOR TEST, Input below) ===\r\n");
printf("=== (1 : File Test, 2 : Dir Test) ===\r\n");
printf("====================================================\r\n");
string strInputPath;
getline(cin, strInputPath);
// Testing input
if (stoi(strInputPath) == 1)
strInputPath = INPUT_FILE_NAME;
else if (stoi(strInputPath) == 2)
strInputPath = INPUT_DIR_NAME;
filesystem::path absPath = filesystem::absolute(strInputPath);
filesystem::path rootPath = absPath.parent_path();
// Main Zip archive method
MyZip zip;
if (!FileReader(zip, strInputPath, rootPath))
{
printf("[ ERROR ](FileReader) Somthing went Wrong... \r\n");
iRet = -1;
}
else
{
if (!zip.FinalizeZip())
{
printf("[ ERROR ](Finallize) Somthing went Wrong... \r\n");
iRet = -1;
}
else
{
printf("It Works Fine. \r\n");
printf("==================== Good Bye Zip! =================\r\n");
iRet = 0;
}
}
return iRet;
}