make class works. need to be tested
This commit is contained in:
@@ -1,107 +1,86 @@
|
|||||||
|
#include <Windows.h>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <debugapi.h>
|
||||||
#include "MyZip.h"
|
#include "MyZip.h"
|
||||||
|
|
||||||
|
|
||||||
// Phase A(AddFile) / Phase B(Finalize) 구조:
|
// Phase A(AddFile) / Phase B(Finalize) 구조:
|
||||||
// - AddFile() : 파일 하나 읽어서 [LocalFileHeader][파일명][데이터]를 fileOut에 이어 쓰고,
|
// - AddFile() : 파일 하나 읽어서 [LocalFileHeader][파일명][데이터]를 fileOut에 이어 쓰고,
|
||||||
// 이 파일의 CentralDirectory 정보는 vecEntries에 누적만 해둠 (아직 안 씀).
|
// 이 파일의 CentralDirectory 정보는 vecEntries에 누적만 해둠 (아직 안 씀).
|
||||||
// - Finalize() : 전체 순회가 끝난 뒤 딱 한 번 호출. 누적된 CentralDirectory들을 순서대로 쓰고,
|
// - Finalize() : 전체 순회가 끝난 뒤 딱 한 번 호출. 누적된 CentralDirectory들을 순서대로 쓰고,
|
||||||
// EOCD 작성 후 스트림을 닫음.
|
// EOCD 작성 후 스트림을 닫음.
|
||||||
bool MyZip::AddFileToZip(const std::string& strFilePath)
|
|
||||||
{
|
|
||||||
LocalFileHeader LFHeader{};
|
|
||||||
// - strFilePath 파일을 열어 크기 계산 + CRC 계산
|
|
||||||
// - LocalFileHeader 채워서 [헤더][파일명][데이터] 순서로 fileOut에 씀
|
|
||||||
// - 이 파일의 CentralDirectory를 채워서 vecEntries에 push_back
|
|
||||||
// - 이 시점에는 CentralDirectory/EOCD를 파일에 쓰지 않음 (Finalize 몫)
|
|
||||||
// strFilePath : 디스크 상의 실제 파일 경로 (FileReader가 넘겨주는 경로)
|
|
||||||
|
|
||||||
// 3. LocalFileHeader 채우기 (signature/crc32/size/lengthOfFileName 등)
|
// Phase A
|
||||||
// 4. fileOut에 [LocalFileHeader][파일명][데이터] 쓰기
|
// - strFilePath 파일을 열어 크기 계산 + CRC 계산
|
||||||
// 5. 이 파일의 CentralDirectory 채워서 vecEntries.push_back({header, fileName})
|
// - 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)
|
||||||
|
{
|
||||||
std::ifstream fileIn(strFilePath, std::ios::binary);
|
std::ifstream fileIn(strFilePath, std::ios::binary);
|
||||||
if (fileIn.is_open())
|
if (!fileIn.is_open())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 파일 크기 계산
|
// 파일 크기 계산
|
||||||
fileIn.seekg(0, std::ios::end); // seekg (offset, 목표점)
|
fileIn.seekg(0, std::ios::end); // seekg (offset, 목표점)
|
||||||
std::streamsize fileInSize = fileIn.tellg();
|
std::streamsize fileInSize = fileIn.tellg();
|
||||||
fileIn.seekg(0, std::ios::beg); // beg == begin
|
fileIn.seekg(0, std::ios::beg); // beg == begin
|
||||||
|
|
||||||
// 메모리 할당
|
// 메모리 할당
|
||||||
std::vector<uint8_t> fileInBuffer(fileInSize);
|
std::vector<uint8_t> fileInBuffer(fileInSize);
|
||||||
// TODO : Buffering 으로 구현. 현재는 파일 통짜로 읽음.
|
// TODO : Buffering 으로 구현. 현재는 파일 통짜로 읽음.
|
||||||
auto* pInBuffer = reinterpret_cast<char*>(fileInBuffer.data());
|
char *pInBuffer = reinterpret_cast<char *>(fileInBuffer.data());
|
||||||
|
|
||||||
// Get CRC value
|
// 파일 읽기
|
||||||
|
if (!fileIn.read(pInBuffer, fileInSize))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gathering data
|
||||||
uint32_t myCRC = getCRC32(pInBuffer, fileInSize);
|
uint32_t myCRC = getCRC32(pInBuffer, fileInSize);
|
||||||
|
|
||||||
// ZIP 포맷 채워넣기
|
|
||||||
// Put PK Signature
|
|
||||||
LFHeader.signature = PK_SIGNATURE_LF_HEADER;
|
|
||||||
|
|
||||||
// TOOD : Temp Format 채우기.
|
|
||||||
LFHeader.versionNeededToExtract = 0; // Temp
|
|
||||||
LFHeader.generalBitFlag = 0; // Temp
|
|
||||||
|
|
||||||
// Put compressed method (uncompressed)
|
|
||||||
LFHeader.compressionMethod = METHOD_UNCOMPRESSED;
|
|
||||||
|
|
||||||
// lastMode[Time|Date] => Not Used.
|
|
||||||
|
|
||||||
// Put CRC value
|
|
||||||
LFHeader.crc32 = myCRC;
|
|
||||||
CentDir.crc32 = myCRC;
|
|
||||||
|
|
||||||
// Put File size
|
|
||||||
LFHeader.sizeOfUncompressed = (uint32_t)fileInSize;
|
|
||||||
LFHeader.sizeOfCompressed = LFHeader.sizeOfUncompressed;
|
|
||||||
CentDir.sizeOfUncompressed = (uint32_t)fileInSize;
|
|
||||||
CentDir.sizeOfCompressed = CentDir.sizeOfUncompressed;
|
|
||||||
|
|
||||||
// Put File name length
|
|
||||||
uint16_t fileNameLeng = static_cast<uint16_t>(strFilePath.length());
|
uint16_t fileNameLeng = static_cast<uint16_t>(strFilePath.length());
|
||||||
LFHeader.lengthOfFileName = fileNameLeng;
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
CentDir.lengthOfFileName = fileNameLeng;
|
LocalFileHeader LFHeader{};
|
||||||
|
CentralDirectory CentDir{};
|
||||||
|
|
||||||
|
// LocalFile Header
|
||||||
|
FillLocalFileHeader(LFHeader, myCRC, static_cast<uint32_t>(fileInSize), fileNameLeng);
|
||||||
|
// CentralDirectory Header
|
||||||
|
CentralDirectory fiiledCDH = GetFilledCentDirHeader(CentDir, myCRC, static_cast<uint32_t>(fileInSize), fileNameLeng);
|
||||||
|
// LocalFile Header 의 시작위치
|
||||||
|
fiiledCDH.offsetLocalFileHeader = static_cast<uint32_t>(fileOut.tellp());
|
||||||
|
|
||||||
// Extra Field 는 사용하지 않음
|
// 우선 CDH 는 vector 에 저장 (나중에 파일 쓸 때 마저 채움.)
|
||||||
// LFHeader.lengthOfExtraField = NOT_USED;
|
vecEntries.push_back({fiiledCDH, strFilePath});
|
||||||
// CentDir.lengthOfExtraField = NOT_USED;
|
|
||||||
|
|
||||||
// TODO : CentDir Temp format 채우기.
|
// 1. Local File Header, file name (var), extra field (var)
|
||||||
CentDir.lengthOfFileComment = 0;
|
fileOut.write(reinterpret_cast<const char *>(&LFHeader), sizeof(LFHeader));
|
||||||
CentDir.diskNumStart = 0; // Temp
|
fileOut.write(strFilePath.c_str(), LFHeader.lengthOfFileName);
|
||||||
CentDir.attributeInternal = 0; // Temp
|
// fileOut.write(NOT_USED, LFHeader.lengthOfExtraField);
|
||||||
CentDir.attributeExternal = 0; // Temp
|
// 2. File Data
|
||||||
// CentDir.offsetLocalFileHeader -> 실제 파일 Write 중에 작성.
|
fileOut.write(pInBuffer, fileInSize);
|
||||||
|
|
||||||
// TODO : EOCDR Temp format 채우기.
|
|
||||||
EOCDR.diskNumber = 0; // Temp
|
|
||||||
EOCDR.diskNumber_CentralDirStart = 0; // Temp
|
|
||||||
EOCDR.numOfCentralDir = 1;
|
|
||||||
EOCDR.numOfEntries = 1;
|
|
||||||
EOCDR.sizeOfCentralDir = sizeof(CentDir) + CentDir.lengthOfFileName;
|
|
||||||
// EOCDR.offsetCentralDir -> 실제 파일 Write 중에 작성.
|
|
||||||
EOCDR.lengthOfCommentLength = 0;
|
|
||||||
|
|
||||||
// TODO : 성공여부 처리 어떻게 할 건지?
|
// TODO : 성공여부 처리 어떻게 할 건지?
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Phase B
|
||||||
// - vecEntries를 순서대로 순회하며 [CentralDirectory][파일명] 씀
|
// - vecEntries를 순서대로 순회하며 [CentralDirectory][파일명] 씀
|
||||||
// - EoCDRecord 채우기 (numOfEntries = vecEntries.size() 등 누적값 반영)
|
// - EoCDRecord 채우기 (numOfEntries = vecEntries.size() 등 누적값 반영)
|
||||||
// - EoCDRecord 쓰기 -> fileOut.close()
|
// - EoCDRecord 쓰기 -> fileOut.close()
|
||||||
// Zip arhcive 파일 쓰기
|
// TODO : 1. vecEntries 순서대로 [CentralDirectory][파일명] 쓰기
|
||||||
// EOCD 채우기
|
// 2. EoCDRecord 채우기 (entries 수, central dir 시작 오프셋/크기 등)
|
||||||
|
// 3. EoCDRecord 쓰기
|
||||||
|
// 4. fileOut.close()
|
||||||
bool MyZip::FinalizeZip()
|
bool MyZip::FinalizeZip()
|
||||||
{
|
{
|
||||||
bool iRet = false;
|
bool iRet = false;
|
||||||
// TODO : 1. vecEntries 순서대로 [CentralDirectory][파일명] 쓰기
|
|
||||||
// 2. EoCDRecord 채우기 (entries 수, central dir 시작 오프셋/크기 등)
|
|
||||||
// 3. EoCDRecord 쓰기
|
|
||||||
// 4. fileOut.close()
|
|
||||||
std::ofstream fileOut(OUTPUT_FILE_NAME, std::ios::binary);
|
|
||||||
iRet = fileOut.is_open();
|
iRet = fileOut.is_open();
|
||||||
if (!iRet)
|
if (!iRet)
|
||||||
{
|
{
|
||||||
@@ -109,31 +88,87 @@ bool MyZip::FinalizeZip()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 1. Local File Header, file name (var), extra field (var)
|
|
||||||
CentDir.offsetLocalFileHeader = static_cast<uint32_t>(fileOut.tellp());
|
|
||||||
// TODO : sizeof 의 값이 스펙과 정확히 일치한다는 보장 필요 (현재는 compiler 에 종속적)
|
// TODO : sizeof 의 값이 스펙과 정확히 일치한다는 보장 필요 (현재는 compiler 에 종속적)
|
||||||
// : offsetof((LocalFileHeader, crc32) 으로 멤버별 static_assert 로 변경할 지 고민.
|
// : offsetof((LocalFileHeader, crc32) 으로 멤버별 static_assert 로 변경할 지 고민.
|
||||||
// : 아니면 필드 단위 직접쓰기로 struct 분해하기?
|
// : 아니면 필드 단위 직접쓰기로 struct 분해하기?
|
||||||
fileOut.write(reinterpret_cast<const char*>(&LFHeader), sizeof(LFHeader));
|
|
||||||
fileOut.write(INPUT_FILE_NAME, LFHeader.lengthOfFileName);
|
|
||||||
//fileOut.write(NOT_USED, LFHeader.LFHeader.lengthOfExtraField);
|
|
||||||
|
|
||||||
// 2. File Data
|
// TODO : EOCDR Temp format 채우기.
|
||||||
fileOut.write(pInBuffer, fileInSize);
|
this->EOCDR.diskNumber = 0; // Temp
|
||||||
|
this->EOCDR.diskNumber_CentralDirStart = 0; // Temp
|
||||||
|
this->EOCDR.numOfCentralDir = static_cast<uint16_t>(vecEntries.size());
|
||||||
|
this->EOCDR.numOfEntries = static_cast<uint16_t>(vecEntries.size());
|
||||||
|
this->EOCDR.lengthOfCommentLength = 0;
|
||||||
|
for (const auto &p : vecEntries)
|
||||||
|
{
|
||||||
|
this->EOCDR.sizeOfCentralDir += (sizeof(p.header) + p.header.lengthOfFileName);
|
||||||
|
}
|
||||||
|
this->EOCDR.offsetCentralDir = static_cast<uint32_t>(fileOut.tellp());
|
||||||
|
|
||||||
// 3. CentralDirectory, file name (var)
|
// Write to file (archiving)
|
||||||
EOCDR.offsetCentralDir = static_cast<uint32_t>(fileOut.tellp());
|
for (const auto &p : vecEntries)
|
||||||
// TODO : sizeof 의 값이 스펙과 정확히 일치한다는 보장 필요 (현재는 compiler 에 종속적)
|
{
|
||||||
fileOut.write(reinterpret_cast<const char*>(&CentDir), sizeof(CentDir));
|
// CentralDirectory, file name (var)
|
||||||
fileOut.write(INPUT_FILE_NAME, CentDir.lengthOfFileName);
|
fileOut.write(reinterpret_cast<const char *>(&p.header), sizeof(p.header));
|
||||||
|
fileOut.write(p.fileName.c_str(), p.header.lengthOfFileName);
|
||||||
|
}
|
||||||
|
|
||||||
// 4. End of CentralDirectory Record
|
// End of CentralDirectory Record
|
||||||
// TODO : sizeof 의 값이 스펙과 정확히 일치한다는 보장 필요 (현재는 compiler 에 종속적)
|
fileOut.write(reinterpret_cast<const char *>(&this->EOCDR), sizeof(this->EOCDR));
|
||||||
fileOut.write(reinterpret_cast<const char*>(&EOCDR), sizeof(EOCDR));
|
|
||||||
|
|
||||||
fileOut.close();
|
fileOut.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : 성공여부 처리 어떻게 할 건지?
|
// TODO : 성공여부 처리 어떻게 할 건지?
|
||||||
return false;
|
return iRet;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyZip::FillLocalFileHeader(LocalFileHeader &targetLF, const uint32_t targetCRC, const uint32_t targetSize, const uint16_t targetNameLeng)
|
||||||
|
{
|
||||||
|
// ZIP 포맷 채워넣기
|
||||||
|
// Put PK Signature
|
||||||
|
targetLF.signature = PK_SIGNATURE_LF_HEADER;
|
||||||
|
|
||||||
|
// TOOD : Temp Format 채우기.
|
||||||
|
targetLF.versionNeededToExtract = 0; // Temp
|
||||||
|
targetLF.generalBitFlag = 0; // Temp
|
||||||
|
|
||||||
|
// Put compressed method (uncompressed)
|
||||||
|
targetLF.compressionMethod = METHOD_UNCOMPRESSED;
|
||||||
|
|
||||||
|
// lastMode[Time|Date] => Not Used.
|
||||||
|
|
||||||
|
// Put CRC value
|
||||||
|
targetLF.crc32 = targetCRC;
|
||||||
|
|
||||||
|
// Put File size
|
||||||
|
targetLF.sizeOfUncompressed = targetSize;
|
||||||
|
targetLF.sizeOfCompressed = targetLF.sizeOfUncompressed;
|
||||||
|
|
||||||
|
// Put File name length
|
||||||
|
targetLF.lengthOfFileName = targetNameLeng;
|
||||||
|
|
||||||
|
// Extra Field 는 사용하지 않음
|
||||||
|
// LFHeader.lengthOfExtraField = NOT_USED;
|
||||||
|
}
|
||||||
|
|
||||||
|
CentralDirectory MyZip::GetFilledCentDirHeader(CentralDirectory &targetCDH, const uint32_t targetCRC, const uint32_t targetSize, const uint16_t targetNameLeng)
|
||||||
|
{
|
||||||
|
targetCDH.crc32 = targetCRC;
|
||||||
|
targetCDH.sizeOfUncompressed = targetSize;
|
||||||
|
targetCDH.sizeOfCompressed = targetCDH.sizeOfUncompressed;
|
||||||
|
|
||||||
|
// Put File name length
|
||||||
|
targetCDH.lengthOfFileName = targetNameLeng;
|
||||||
|
|
||||||
|
// Extra Field 는 사용하지 않음
|
||||||
|
// targetCDH.lengthOfExtraField = NOT_USED;
|
||||||
|
|
||||||
|
// TODO : targetCDH Temp format 채우기.
|
||||||
|
targetCDH.lengthOfFileComment = 0;
|
||||||
|
targetCDH.diskNumStart = 0; // Temp
|
||||||
|
targetCDH.attributeInternal = 0; // Temp
|
||||||
|
targetCDH.attributeExternal = 0; // Temp
|
||||||
|
// targetCDH.offsetLocalFileHeader -> 실제 파일 Write 중에 작성.
|
||||||
|
|
||||||
|
return targetCDH;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
#include "CRC.h"
|
#include "CRC.h"
|
||||||
|
|
||||||
#define OUTPUT_FILE_NAME "MyZip.zip"
|
#define OUTPUT_FILE_NAME "MyZip.zip"
|
||||||
|
#define INPUT_FILE_NAME "Target_File.txt"
|
||||||
|
#define INPUT_DIR_NAME "Target_File"
|
||||||
|
|
||||||
class MyZip
|
class MyZip
|
||||||
{
|
{
|
||||||
@@ -16,30 +18,43 @@ private:
|
|||||||
std::string strOutputPath;
|
std::string strOutputPath;
|
||||||
|
|
||||||
// Zip Format struct
|
// Zip Format struct
|
||||||
CentralDirectory CentDir{};
|
|
||||||
EoCDRecord EOCDR{};
|
EoCDRecord EOCDR{};
|
||||||
|
|
||||||
struct ArchivedEntry
|
struct ArchivedEntry
|
||||||
{
|
{
|
||||||
CentralDirectory header;
|
CentralDirectory header;
|
||||||
std::string fileName;
|
std::string fileName;
|
||||||
|
|
||||||
|
// TODO : 아래 기본값 어디서 채울 지 고민 (GetFiiled or 여기서)
|
||||||
|
ArchivedEntry(CentralDirectory cdh, std::string name) : header(cdh), fileName(name)
|
||||||
|
{
|
||||||
|
header.signature = PK_SIGNATURE_CENT_DIR;
|
||||||
|
header.versionMadeBy = 0; // Temp
|
||||||
|
header.versionNeededToExtract = 0; // Temp
|
||||||
|
header.generalBitFlag = 0; // Temp
|
||||||
|
header.compressionMethod = METHOD_UNCOMPRESSED;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
std::vector<ArchivedEntry> vecEntries;
|
std::vector<ArchivedEntry> vecEntries;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MyZip(std::string strfilePath)
|
MyZip() : strOutputPath(std::string(OUTPUT_FILE_NAME))
|
||||||
: strOutputPath(strfilePath)
|
{
|
||||||
|
fileOut = std::ofstream(strOutputPath, std::ios::binary);
|
||||||
|
EOCDR.signature = PK_SIGNATURE_EO_CDR;
|
||||||
|
}
|
||||||
|
MyZip(const std::string& strfilePath) : strOutputPath(strfilePath)
|
||||||
{
|
{
|
||||||
fileOut = std::ofstream(strOutputPath, std::ios::binary);
|
fileOut = std::ofstream(strOutputPath, std::ios::binary);
|
||||||
CentDir.signature = PK_SIGNATURE_CENT_DIR;
|
|
||||||
EOCDR.signature = PK_SIGNATURE_EO_CDR;
|
EOCDR.signature = PK_SIGNATURE_EO_CDR;
|
||||||
|
|
||||||
CentDir.versionMadeBy = 0; // Temp
|
|
||||||
CentDir.versionNeededToExtract = 0; // Temp
|
|
||||||
CentDir.generalBitFlag = 0; // Temp
|
|
||||||
CentDir.compressionMethod = METHOD_UNCOMPRESSED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Zip 에 파일 추가
|
||||||
bool AddFileToZip(const std::string &strFilePath);
|
bool AddFileToZip(const std::string &strFilePath);
|
||||||
|
// Zip 마무리
|
||||||
bool FinalizeZip();
|
bool FinalizeZip();
|
||||||
|
|
||||||
|
// Header Functions
|
||||||
|
void FillLocalFileHeader(LocalFileHeader &targetLF, const uint32_t targetCRC, const uint32_t targetSize, const uint16_t targetNameLeng);
|
||||||
|
CentralDirectory GetFilledCentDirHeader(CentralDirectory &targetCDH, const uint32_t targetCRC, const uint32_t targetSize, const uint16_t targetNameLeng);
|
||||||
};
|
};
|
||||||
|
|||||||
+5
-5
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
const static uint32_t PK_SIGNATURE_LF_HEADER = 0x04034b50; // PK + 0x0403
|
constexpr uint32_t PK_SIGNATURE_LF_HEADER = 0x04034b50; // PK + 0x0403
|
||||||
const static uint32_t PK_SIGNATURE_CENT_DIR = 0x02014b50; // PK + 0x0201
|
constexpr uint32_t PK_SIGNATURE_CENT_DIR = 0x02014b50; // PK + 0x0201
|
||||||
const static uint32_t PK_SIGNATURE_EO_CDR = 0x06054b50; // PK + 0x0605
|
constexpr uint32_t PK_SIGNATURE_EO_CDR = 0x06054b50; // PK + 0x0605
|
||||||
const static uint16_t METHOD_UNCOMPRESSED = 0;
|
constexpr uint16_t METHOD_UNCOMPRESSED = 0;
|
||||||
|
|
||||||
/** Overall .ZIP file format:
|
/** Overall .ZIP file format:
|
||||||
|
|
||||||
@@ -186,6 +186,6 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30;
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 정적 메모리 사이즈 검사
|
// 정적 메모리 사이즈 검사
|
||||||
static_assert(sizeof(EoCDRecord) == EOCD_BYTE);
|
static_assert(sizeof(EoCDRecord) == EOCD_BYTE, "EoCDRecord Size is mismatch");
|
||||||
|
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|||||||
@@ -14,19 +14,22 @@
|
|||||||
|
|
||||||
static uint32_t g_recursive_counter = 0;
|
static uint32_t g_recursive_counter = 0;
|
||||||
|
|
||||||
bool ZipArchiver(std::string strInputPath)
|
bool ZipArchiver(std::string strInputPath, bool bIsFinal = false)
|
||||||
{
|
{
|
||||||
MyZip myZIP(strInputPath);
|
MyZip zip;
|
||||||
|
bool bRet = false;
|
||||||
|
|
||||||
myZIP.AddFileToZip();
|
bRet = zip.AddFileToZip(strInputPath);
|
||||||
|
if(bIsFinal)
|
||||||
|
bRet = zip.FinalizeZip();
|
||||||
|
|
||||||
return false;
|
return bRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileReader(std::string strInputPath)
|
bool FileReader(std::string strInputPath)
|
||||||
{
|
{
|
||||||
using namespace std;
|
using namespace std;
|
||||||
bool iRet = true;
|
bool bRet = true;
|
||||||
|
|
||||||
// path class obj 생성.
|
// path class obj 생성.
|
||||||
filesystem::path pathObj(strInputPath);
|
filesystem::path pathObj(strInputPath);
|
||||||
@@ -37,7 +40,8 @@ bool FileReader(std::string strInputPath)
|
|||||||
string strDbg = "[ERROR] File not found : " + pathObj.string();
|
string strDbg = "[ERROR] File not found : " + pathObj.string();
|
||||||
strDbg += "\r\n";
|
strDbg += "\r\n";
|
||||||
OutputDebugStringA(strDbg.c_str());
|
OutputDebugStringA(strDbg.c_str());
|
||||||
iRet = false;
|
|
||||||
|
bRet = false;
|
||||||
}
|
}
|
||||||
enum class eFILETYPE
|
enum class eFILETYPE
|
||||||
{
|
{
|
||||||
@@ -50,12 +54,14 @@ bool FileReader(std::string strInputPath)
|
|||||||
else if (filesystem::is_regular_file(pathObj)) eFileDist = eFILETYPE::isFile;
|
else if (filesystem::is_regular_file(pathObj)) eFileDist = eFILETYPE::isFile;
|
||||||
else eFileDist = eFILETYPE::ETC;
|
else eFileDist = eFILETYPE::ETC;
|
||||||
|
|
||||||
|
const filesystem::path& rootPath = pathObj;
|
||||||
switch (eFileDist)
|
switch (eFileDist)
|
||||||
{
|
{
|
||||||
// case 1 : input file
|
// case 1 : input file
|
||||||
case eFILETYPE::isFile:
|
case eFILETYPE::isFile:
|
||||||
{
|
{
|
||||||
iRet = ZipArchiver(pathObj.string());
|
bool bIsFinal = g_recursive_counter > 0 ? false : true;
|
||||||
|
bRet = ZipArchiver(pathObj.string(), bIsFinal );
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -68,37 +74,37 @@ bool FileReader(std::string strInputPath)
|
|||||||
{
|
{
|
||||||
// get entry one by one
|
// get entry one by one
|
||||||
const filesystem::directory_entry& currentEntry = *itrDir;
|
const filesystem::directory_entry& currentEntry = *itrDir;
|
||||||
const filesystem::path& currentPath = currentEntry.path();
|
const filesystem::path& currentAbsPath = currentEntry.path();
|
||||||
if (filesystem::is_directory(currentEntry))
|
if (filesystem::is_directory(currentEntry))
|
||||||
{
|
{
|
||||||
|
// 있으면 좋을거같아서 만든 변수 "g_recursive_counter"
|
||||||
g_recursive_counter++;
|
g_recursive_counter++;
|
||||||
// TODO : export to debugging log function
|
// TODO : export to debugging log function
|
||||||
string strDbg = "[INFO] Current Entry (Dir)\t: " + currentPath.string();
|
string strDbg = "[INFO] Current Entry (Dir)\t: " + currentAbsPath.string();
|
||||||
strDbg += "\t Counter : " + to_string(g_recursive_counter);
|
strDbg += "\t Counter : " + to_string(g_recursive_counter);
|
||||||
strDbg += "\r\n";
|
strDbg += "\r\n";
|
||||||
OutputDebugStringA(strDbg.c_str());
|
OutputDebugStringA(strDbg.c_str());
|
||||||
|
|
||||||
// recursive traversal
|
// recursive traversal
|
||||||
iRet = FileReader(currentPath.string());
|
bRet = FileReader(currentAbsPath.string());
|
||||||
g_recursive_counter--;
|
g_recursive_counter--;
|
||||||
}
|
}
|
||||||
else if (filesystem::is_regular_file(currentEntry))
|
else if (filesystem::is_regular_file(currentEntry))
|
||||||
{
|
{
|
||||||
// TODO : export to debugging log function
|
// TODO : export to debugging log function
|
||||||
string strDbg = "[INFO] Current Entry (File)\t: " + currentPath.string();
|
string strDbg = "[INFO] Current Entry (File)\t: " + currentAbsPath.string();
|
||||||
strDbg += "\t Counter : " + to_string(g_recursive_counter);
|
strDbg += "\t Counter : " + to_string(g_recursive_counter);
|
||||||
strDbg += "\r\n";
|
strDbg += "\r\n";
|
||||||
|
|
||||||
OutputDebugStringA(strDbg.c_str());
|
OutputDebugStringA(strDbg.c_str());
|
||||||
|
|
||||||
// archiving file
|
// archiving file
|
||||||
iRet = ZipArchiver(currentPath.string());
|
auto relativePath = filesystem::relative(currentAbsPath, rootPath);
|
||||||
|
bRet = ZipArchiver(relativePath.string());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// TODO : 근데 애초에 제 3의 Type 이 있나? (여기 의미 있는건가?)
|
|
||||||
OutputDebugStringA("[ERROR] Wrong Type of File");
|
OutputDebugStringA("[ERROR] Wrong Type of File");
|
||||||
iRet = false;
|
bRet = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// step forward entry
|
// step forward entry
|
||||||
@@ -114,24 +120,29 @@ bool FileReader(std::string strInputPath)
|
|||||||
string strErr = "[ERROR] Something went Wrong..!!";
|
string strErr = "[ERROR] Something went Wrong..!!";
|
||||||
OutputDebugStringA(strErr.c_str());
|
OutputDebugStringA(strErr.c_str());
|
||||||
|
|
||||||
iRet = false;
|
bRet = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if(bRet)
|
||||||
|
{
|
||||||
|
bRet = ZipArchiver(strInputPath, true);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO : 실패 상황 별 다른 코드 부여 + 코드 별 처리 어떻게 할 건지?
|
// TODO : 실패 상황 별 다른 코드 부여 + 코드 별 처리 어떻게 할 건지?
|
||||||
return iRet;
|
return bRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
std::printf("==================== Hello Zip! ====================\r\n");
|
std::printf("==================== Hello Zip! ====================\r\n");
|
||||||
int iRet = 0;
|
int iRet = 0;
|
||||||
|
|
||||||
// Main Zip archive method
|
// Main Zip archive method
|
||||||
if (!FileReader(INPUT_FILE_NAME))
|
if (!FileReader(std::string(INPUT_FILE_NAME)))
|
||||||
{
|
{
|
||||||
iRet = -1;
|
iRet = -1;
|
||||||
std::printf("[ ERROR ] Somthing went Wrong... \r\n");
|
std::printf("[ ERROR ] Somthing went Wrong... \r\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user