edit code to split cpp, header
This commit is contained in:
@@ -1,145 +1,26 @@
|
||||
// 1. C++ 표준 라이브러리 (C 호환 및 C++ 표준을 알파벳 순으로 통합하거나 C/C++ 분리)
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// 2. OS 전용 / 플랫폼 API (Windows)
|
||||
#include <Windows.h>
|
||||
#include <debugapi.h>
|
||||
|
||||
#include "ZipDefine.h"
|
||||
#include "CRC.h"
|
||||
|
||||
#define INPUT_FILE_NAME "Target_Dir"
|
||||
#define OUTPUT_FILE_NAME "MyZip.zip"
|
||||
#include "MyZip.h"
|
||||
|
||||
static uint32_t g_recursive_counter = 0;
|
||||
|
||||
bool ZipArchiver(std::string strInputPath, std::string strFileName)
|
||||
bool ZipArchiver(std::string strInputPath)
|
||||
{
|
||||
LocalFileHeader LFHeader{};
|
||||
CentralDirectory CentDir{};
|
||||
EoCDRecord EOCDR{};
|
||||
MyZip myZIP(strInputPath);
|
||||
|
||||
using namespace std;
|
||||
myZIP.AddFileToZip();
|
||||
|
||||
ifstream fileIn(strInputPath, ios::binary);
|
||||
bool iRet = fileIn.is_open();
|
||||
|
||||
// 파일 크기 계산
|
||||
// TODO : Buffering 으로 구현. 현재는 파일 통짜로 읽음.
|
||||
fileIn.seekg(0, ios::end); // seekg (offset, 목표점)
|
||||
streamsize fileInSize = fileIn.tellg();
|
||||
fileIn.seekg(0, ios::beg); // beg == begin
|
||||
|
||||
// 메모리 할당
|
||||
vector<uint8_t> fileInBuffer(fileInSize);
|
||||
auto* pInBuffer = reinterpret_cast<char*>(fileInBuffer.data());
|
||||
|
||||
// Read
|
||||
fileIn.read(pInBuffer, fileInSize);
|
||||
// ZIP 포맷 채워넣기
|
||||
// TODO : 클래스화 대상
|
||||
{
|
||||
// Get CRC value
|
||||
uint32_t myCRC = getCRC32(pInBuffer, fileInSize);
|
||||
|
||||
// Put PK Signature
|
||||
LFHeader.signature = PK_SIGNATURE_LF_HEADER;
|
||||
CentDir.signature = PK_SIGNATURE_CENT_DIR;
|
||||
EOCDR.signature = PK_SIGNATURE_EO_CDR;
|
||||
|
||||
// TOOD : Temp Format 채우기.
|
||||
CentDir.versionMadeBy = 0; // Temp
|
||||
LFHeader.versionNeededToExtract = 0; // Temp
|
||||
CentDir.versionNeededToExtract = 0; // Temp
|
||||
LFHeader.generalBitFlag = 0; // Temp
|
||||
CentDir.generalBitFlag = 0; // Temp
|
||||
|
||||
// Put compressed method (uncompressed)
|
||||
LFHeader.compressionMethod = METHOD_UNCOMPRESSED;
|
||||
CentDir.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;
|
||||
CentDir.sizeOfUncompressed = (uint32_t)fileInSize;
|
||||
LFHeader.sizeOfCompressed = LFHeader.sizeOfUncompressed;
|
||||
CentDir.sizeOfCompressed = CentDir.sizeOfUncompressed;
|
||||
|
||||
// Put File name length
|
||||
uint16_t fileNameLeng = static_cast<uint16_t>(strFileName.length());
|
||||
LFHeader.lengthOfFileName = fileNameLeng;
|
||||
CentDir.lengthOfFileName = fileNameLeng;
|
||||
|
||||
// Extra Field 는 사용하지 않음
|
||||
// LFHeader.lengthOfExtraField = NOT_USED;
|
||||
// CentDir.lengthOfExtraField = NOT_USED;
|
||||
|
||||
// TODO : CentDir Temp format 채우기.
|
||||
CentDir.lengthOfFileComment = 0;
|
||||
CentDir.diskNumStart = 0; // Temp
|
||||
CentDir.attributeInternal = 0; // Temp
|
||||
CentDir.attributeExternal = 0; // Temp
|
||||
// CentDir.offsetLocalFileHeader -> 실제 파일 Write 중에 작성.
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Zip arhcive 파일 쓰기
|
||||
{
|
||||
ofstream fileOut(OUTPUT_FILE_NAME, ios::binary);
|
||||
iRet = fileOut.is_open();
|
||||
if (!iRet)
|
||||
{
|
||||
printf("FileOut open failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 1. Local File Header, file name (var), extra field (var)
|
||||
CentDir.offsetLocalFileHeader = static_cast<uint32_t>(fileOut.tellp());
|
||||
// TODO : sizeof 의 값이 스펙과 정확히 일치한다는 보장 필요 (현재는 compiler 에 종속적)
|
||||
// : offsetof((LocalFileHeader, crc32) 으로 멤버별 static_assert 로 변경할 지 고민.
|
||||
// : 아니면 필드 단위 직접쓰기로 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
|
||||
fileOut.write(pInBuffer, fileInSize);
|
||||
|
||||
// 3. CentralDirectory, file name (var)
|
||||
EOCDR.offsetCentralDir = static_cast<uint32_t>(fileOut.tellp());
|
||||
// TODO : sizeof 의 값이 스펙과 정확히 일치한다는 보장 필요 (현재는 compiler 에 종속적)
|
||||
fileOut.write(reinterpret_cast<const char*>(&CentDir), sizeof(CentDir));
|
||||
fileOut.write(INPUT_FILE_NAME, CentDir.lengthOfFileName);
|
||||
|
||||
// 4. End of CentralDirectory Record
|
||||
// TODO : sizeof 의 값이 스펙과 정확히 일치한다는 보장 필요 (현재는 compiler 에 종속적)
|
||||
fileOut.write(reinterpret_cast<const char*>(&EOCDR), sizeof(EOCDR));
|
||||
|
||||
fileOut.close();
|
||||
}
|
||||
}
|
||||
|
||||
return iRet;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileReader(std::string strInputPath)
|
||||
@@ -174,7 +55,7 @@ bool FileReader(std::string strInputPath)
|
||||
// case 1 : input file
|
||||
case eFILETYPE::isFile:
|
||||
{
|
||||
iRet = ZipArchiver(pathObj.string(), pathObj.filename().string());
|
||||
iRet = ZipArchiver(pathObj.string());
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -211,7 +92,7 @@ bool FileReader(std::string strInputPath)
|
||||
OutputDebugStringA(strDbg.c_str());
|
||||
|
||||
// archiving file
|
||||
iRet = ZipArchiver(currentPath.string(), currentPath.filename().string());
|
||||
iRet = ZipArchiver(currentPath.string());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user