edit CWD problem

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