From 0339ddcd914efb6dd7fc3cb937392586820443d8 Mon Sep 17 00:00:00 2001 From: Juno Kim Date: Mon, 31 Aug 2026 16:45:19 +0900 Subject: [PATCH] edit CWD problem --- MyZip.cpp | 39 ++++++++++++++++----------------------- MyZip.h | 2 +- main.cpp | 24 ++++++++++++++---------- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/MyZip.cpp b/MyZip.cpp index bd42530..2a3161b 100644 --- a/MyZip.cpp +++ b/MyZip.cpp @@ -3,26 +3,24 @@ #include #include "MyZip.h" -// Phase A(AddFile) / Phase B(Finalize) 구조: -// - AddFile() : 파일 하나 읽어서 [LocalFileHeader][파일명][데이터]를 fileOut에 이어 쓰고, -// 이 파일의 CentralDirectory 정보는 vecEntries에 누적만 해둠 (아직 안 씀). -// - Finalize() : 전체 순회가 끝난 뒤 딱 한 번 호출. 누적된 CentralDirectory들을 순서대로 쓰고, -// EOCD 작성 후 스트림을 닫음. +// Phase A(AddFileToZip) / Phase B(FinalizeZip) 구조: +// - AddFileToZip() : 파일 하나 읽어서 [LocalFileHeader][파일명][데이터]를 fileOut에 이어 쓰고, +// 이 파일의 CentralDirectory 정보는 vecEntries에 누적만 해둠 (아직 안 씀). +// - FinalizeZip() : 전체 순회가 끝난 뒤 딱 한 번 호출. 누적된 CentralDirectory들을 순서대로 쓰고, +// EOCD 작성 후 스트림을 닫음. // Phase A // - strFilePath 파일을 열어 크기 계산 + CRC 계산 // - LocalFileHeader 채워서 [헤더][파일명][데이터] 순서로 fileOut에 씀 // - 이 파일의 CentralDirectory를 채워서 vecEntries에 push_back -// - 이 시점에는 CentralDirectory/EOCD를 파일에 쓰지 않음 (Finalize 몫) -// strFilePath : 디스크 상의 실제 파일 경로 (FileReader가 넘겨주는 경로) -// 3. LocalFileHeader 채우기 (signature/crc32/size/lengthOfFileName 등) -// 4. fileOut에 [LocalFileHeader][파일명][데이터] 쓰기 -// 5. 이 파일의 CentralDirectory 채워서 vecEntries.push_back({header, fileName}) -bool MyZip::AddFileToZip(const std::string &strFilePath) +// - 이 시점에는 CentralDirectory/EOCD를 파일에 쓰지 않음 (Finalize 단계) +bool MyZip::AddFileToZip(const std::string & strFileDiskPath, const std::string& strFileEntryPath) { - std::ifstream fileIn(strFilePath, std::ios::binary); + std::ifstream fileIn(strFileDiskPath, std::ios::binary); if (!fileIn.is_open()) { + printf("[ ERROR ](AddFileToZip) Something went Worng..."); + printf("fileIn open failed"); return false; } @@ -39,12 +37,14 @@ bool MyZip::AddFileToZip(const std::string &strFilePath) // 파일 읽기 if (!fileIn.read(pInBuffer, fileInSize)) { + printf("[ ERROR ](AddFileToZip) Something went Worng..."); + printf("fileIn read failed"); return false; } // Gathering data uint32_t myCRC = getCRC32(pInBuffer, fileInSize); - uint16_t fileNameLeng = static_cast(strFilePath.length()); + uint16_t fileNameLeng = static_cast(strFileEntryPath.length()); //////////////////////////////////////////////////////////////////////////////////////// LocalFileHeader LFHeader{}; CentralDirectory CentDir{}; @@ -57,11 +57,11 @@ bool MyZip::AddFileToZip(const std::string &strFilePath) fiiledCDH.offsetLocalFileHeader = static_cast(fileOut.tellp()); // 우선 CDH 는 vector 에 저장 (나중에 파일 쓸 때 마저 채움.) - vecEntries.push_back({fiiledCDH, strFilePath}); + vecEntries.push_back({fiiledCDH, strFileEntryPath }); // 1. Local File Header, file name (var), extra field (var) fileOut.write(reinterpret_cast(&LFHeader), sizeof(LFHeader)); - fileOut.write(strFilePath.c_str(), LFHeader.lengthOfFileName); + fileOut.write(strFileEntryPath.c_str(), LFHeader.lengthOfFileName); // fileOut.write(NOT_USED, LFHeader.lengthOfExtraField); // 2. File Data fileOut.write(pInBuffer, fileInSize); @@ -74,24 +74,17 @@ bool MyZip::AddFileToZip(const std::string &strFilePath) // - vecEntries를 순서대로 순회하며 [CentralDirectory][파일명] 씀 // - EoCDRecord 채우기 (numOfEntries = vecEntries.size() 등 누적값 반영) // - EoCDRecord 쓰기 -> fileOut.close() -// TODO : 1. vecEntries 순서대로 [CentralDirectory][파일명] 쓰기 -// 2. EoCDRecord 채우기 (entries 수, central dir 시작 오프셋/크기 등) -// 3. EoCDRecord 쓰기 -// 4. fileOut.close() bool MyZip::FinalizeZip() { bool iRet = false; iRet = fileOut.is_open(); if (!iRet) { + printf("[ ERROR ](FinalizeZip) Something went Worng..."); printf("FileOut open failed"); } else { - // TODO : sizeof 의 값이 스펙과 정확히 일치한다는 보장 필요 (현재는 compiler 에 종속적) - // : offsetof((LocalFileHeader, crc32) 으로 멤버별 static_assert 로 변경할 지 고민. - // : 아니면 필드 단위 직접쓰기로 struct 분해하기? - // TODO : EOCDR Temp format 채우기. this->EOCDR.diskNumber = 0; // Temp this->EOCDR.diskNumber_CentralDirStart = 0; // Temp diff --git a/MyZip.h b/MyZip.h index acbd388..ad837f0 100644 --- a/MyZip.h +++ b/MyZip.h @@ -50,7 +50,7 @@ public: } // Zip 에 파일 추가 - bool AddFileToZip(const std::string &strFilePath); + bool AddFileToZip(const std::string &strFileDiskPath, const std::string& strFileEntryPath); // Zip 마무리 bool FinalizeZip(); diff --git a/main.cpp b/main.cpp index a24132b..9bee453 100644 --- a/main.cpp +++ b/main.cpp @@ -20,9 +20,8 @@ bool FileReader(MyZip &zip, std::string strInputPath, const std::filesystem::pat // file valid check if (!filesystem::exists(pathObj)) { - string strDbg = "[ERROR] File not found : " + pathObj.string(); - strDbg += "\r\n"; - OutputDebugStringA(strDbg.c_str()); + printf("[ERROR](FileReader) Something went Wrong..!! \r\n"); + printf("[ERROR] File not found : %s ", pathObj.string().c_str()); bRet = false; } @@ -48,7 +47,8 @@ bool FileReader(MyZip &zip, std::string strInputPath, const std::filesystem::pat strDbg += "\r\n"; OutputDebugStringA(strDbg.c_str()); - bRet = zip.AddFileToZip(pathObj.string()); + auto absPathOjb = filesystem::absolute(pathObj); + bRet = zip.AddFileToZip(absPathOjb.generic_string(), pathObj.generic_string()); break; } @@ -73,6 +73,7 @@ bool FileReader(MyZip &zip, std::string strInputPath, const std::filesystem::pat OutputDebugStringA(strDbg.c_str()); // recursive traversal + // TODO : bRet 쓰는 곳이 없음. 파일 처리 실패 시 어떻게 할 건지 정해야 함. bRet = FileReader(zip, currentAbsPath.string(), rootPath); } else if (filesystem::is_regular_file(currentEntry)) @@ -85,11 +86,15 @@ bool FileReader(MyZip &zip, std::string strInputPath, const std::filesystem::pat // archiving file auto relativePath = filesystem::relative(currentAbsPath, rootPath); - bRet = zip.AddFileToZip(relativePath.string()); + // generic_string : make back-slash to OS independant delimeter ("\\" -> "/") + // TODO : bRet 쓰는 곳이 없음. 파일 처리 실패 시 어떻게 할 건지 정해야 함. + bRet = zip.AddFileToZip(currentAbsPath.generic_string(), relativePath.generic_string()); } else { - OutputDebugStringA("[ERROR] Wrong Type of File"); + printf("[ERROR](FileReader) Something went Wrong..!! \r\n"); + printf("[ERROR](FileReader) Maybe its corrupted file. \r\n"); + bRet = false; } @@ -103,9 +108,8 @@ bool FileReader(MyZip &zip, std::string strInputPath, const std::filesystem::pat // case 3 : ERROR case eFILETYPE::ETC: { - // 이런 경우가 있으려나? 일단 혹시 모르니까 - string strErr = "[ERROR] Something went Wrong..!!"; - OutputDebugStringA(strErr.c_str()); + printf("[ERROR](FileReader) Something went Wrong..!! \r\n"); + printf("[ERROR](FileReader) Maybe its corrupted file. \r\n"); bRet = false; break; @@ -114,7 +118,7 @@ bool FileReader(MyZip &zip, std::string strInputPath, const std::filesystem::pat break; }; - // TODO : 실패 상황 별 다른 코드 부여 + 코드 별 처리 어떻게 할 건지? + // TODO : 실패 상황 별 다른 코드 부여? + 코드 별 처리 어떻게 할 건지? return bRet; }