diff --git a/main.cpp b/main.cpp index 56bb861..59483c3 100644 --- a/main.cpp +++ b/main.cpp @@ -16,12 +16,107 @@ #define INPUT_FILE_NAME "MyZip.txt" #define OUTPUT_FILE_NAME "MyZip.zip" +static uint32_t RECURSIVE_COUNTER = 0; + bool searchDir() { } -bool FileReader() +bool FileReader(std::string strInputPath) +{ + using namespace std; + bool iRet = true; + + // path class obj 생성. + filesystem::path pathObj(strInputPath); + + // file valid check + if (!filesystem::exists(pathObj)) + { + string strDbg = "[ERROR] File not found : " + pathObj.string(); + OutputDebugStringA(strDbg.c_str()); + iRet = 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 file + case eFILETYPE::isFile: + { + ZipArchiver(pathObj.string()); + + break; + } + // case 2 : input directory + case eFILETYPE::isDir: + { + // make iterator to traversal + filesystem::directory_iterator itrDir(pathObj); + while (itrDir != filesystem::end(itrDir)) + { + // get entry one by one + const filesystem::directory_entry& currentEntry = *itrDir; + if (filesystem::is_directory(currentEntry)) + { + RECURSIVE_COUNTER++; + string strDbg = "[INFO] Current Entry (Dir)\t: " + currentEntry.path().string() + "\t Counter : " + to_string(RECURSIVE_COUNTER); + OutputDebugStringA(strDbg.c_str()); + + // recursive traversal + iRet = FileReader(currentEntry.path().string()); + } + else if (filesystem::is_regular_file(currentEntry)) + { + RECURSIVE_COUNTER++; + string strDbg = "[INFO] Current Entry (File)\t: " + currentEntry.path().string() + "\t Counter : " + to_string(RECURSIVE_COUNTER); + OutputDebugStringA(strDbg.c_str()); + + // archiving file + ZipArchiver(currentEntry.path().string()); + } + else + { + // TODO : 근데 애초에 제 3의 Type 이 있나? (여기 의미 있는건가?) + OutputDebugStringA("[ERROR] Wrong Type of File"); + iRet = false; + } + + // step forward entry + itrDir++; + } + + break; + } + // case 3 : ERROR + case eFILETYPE::ETC: + { + // 이런 경우가 있으려나? 일단 혹시 모르니까 + string strErr = "[ERROR] Something went Wrong..!!"; + OutputDebugStringA(strErr.c_str()); + + iRet = false; + //break; + } + default: + break; + } + + // TODO : 실패 상황 별 다른 코드 부여 + 코드 별 처리 어떻게 할 건지? + return iRet; +} + +bool ZipArchiver(std::string strInputPath) { LocalFileHeader LFHeader{}; CentralDirectory CentDir{}; @@ -29,81 +124,9 @@ bool FileReader() using namespace std; - ifstream fileIn(INPUT_FILE_NAME, ios::binary); + ifstream fileIn(strInputPath, ios::binary); bool iRet = fileIn.is_open(); - if (!iRet) - { - printf("FileIn open failed"); - } - else - { - // path class obj 생성. - filesystem::path pathObj(INPUT_FILE_NAME); - // file valid check - if (!filesystem::exists(pathObj)) - { - string strErr = "File not found : " + pathObj.string(); - OutputDebugStringA(strErr.c_str()); - return false; - } - if (!filesystem::is_regular_file(pathObj)) - { - string strErr = "File is not regular file : " + pathObj.string(); - OutputDebugStringA(strErr.c_str()); - - // directory recursive search - - if (filesystem::is_directory(pathObj)) - { - filesystem::directory_iterator itrDir(pathObj); - - while (itrDir != filesystem::end(itrDir)) - { - // get entry one by one - const filesystem::directory_entry& currentEntry = *itrDir; - - if (filesystem::is_directory(currentEntry)) - { - // TODO : 다시 위로 돌아가야 함 + 재귀적으로 돌아가면서 처리 - // Do Something... - } - else if(filesystem::is_regular_file(currentEntry)) - { - // TODO : 파일 Archiving logic - ZipArchiver(currentEntry); - } - else - { - // 애초에 제 3의 Type 이 있나? - OutputDebugStringA("Wrong Type of File"); - } - - // step forward entry - itrDir++; - } - } - else - { - // 이런 경우가 있으려나? 일단 혹시 모르니까 - string strErr = "Something went Wrong..!!"; - OutputDebugStringA(strErr.c_str()); - - return false; - } - } - else - { - ZipArchiver(); - } - } - - // TODO : 어떤 식으로 Return 할 건지 하나만 정하기 - return iRet; -} - -bool ZipArchiver() -{ // 파일 크기 계산 // TODO : Buffering 으로 구현. 현재는 파일 통짜로 읽음. fileIn.seekg(0, ios::end); // seekg (offset, 목표점)