From 006ea5b62b01ac90b5557860cd2a7e05c0d1d596 Mon Sep 17 00:00:00 2001 From: Juno Kim Date: Fri, 7 Aug 2026 09:05:31 +0900 Subject: [PATCH] add basic binary fread --- Define.h | 40 +++++++++++++++++++++++++++++----------- main.cpp | 45 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/Define.h b/Define.h index fcf3c40..341d7ba 100644 --- a/Define.h +++ b/Define.h @@ -56,10 +56,10 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30; uint16_t lastModTime; // 압축 시점의 시간 (1980년 기준 시간) uint16_t lastModDate; // 압축 시점의 날짜 (1980년 기준 연도) uint32_t crc32; - uint32_t compressedSize; // same as unCompression (cuz no compreesion) - uint32_t unCompressedSize; - uint16_t fileNameLength; - uint16_t extraFieldLength; + uint32_t sizeOfCompressed; // same as unCompression (cuz no compreesion) + uint32_t sizeOfUncompressed; + uint16_t lengthOfFileName; + uint16_t lengthOfExtraField; // var[fileNameLength] fileName // var[extraFieldLength] extraField @@ -101,7 +101,6 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30; constexpr int CENTRAL_DIRECTORY_BYTE = 46; struct CentralDirectory { - uint32_t signature; uint16_t versionMadeBy; uint16_t versionNeededToExtract; @@ -130,16 +129,26 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30; end of central dir signature 4 bytes (0x0605 + 4b50) number of this disk 2 bytes + + // 중앙 디렉터리가 시작되는 디스크의 번호. number of the disk with the start of the central directory 2 bytes + + // 이 디스크에 있는 중앙 디렉터리 항목의 수 total number of entries in the central directory on this disk 2 bytes + + // .ZIP 파일 내 전체 파일 수 total number of entries in the central directory 2 bytes + size of the central directory 4 bytes + + // 중앙 디렉터리가 시작되는 디스크 상에서 중앙 디렉터리 시작 지점의 오프셋 offset of start of central directory with respect to the starting disk number 4 bytes + .ZIP file comment length 2 bytes .ZIP file comment (variable size) */ @@ -147,13 +156,22 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30; struct EoCDRecord { uint32_t signature; + + // central directory end record을 포함하는 이 디스크의 번호 uint16_t diskNumber; - uint16_t diskNumber_CentralDirectoryStart; - uint16_t numOfCentralDirectory; // TODO : 이름 애매함. - uint16_t numEntries; // TODO : 이름 애매함. - uint32_t centralDirectorySize; - uint32_t centralDirectoryOffset; - uint16_t zipFileCommentLength; + + // 중앙 디렉터리가 시작되는 디스크의 번호. + uint16_t diskNumber_CentralDirStart; + + // 이 디스크에 있는 중앙 디렉터리 항목의 수 + uint16_t numOfCentralDir; + + // .ZIP 파일 내 전체 파일 수 + uint16_t numOfEntries; + + uint32_t SizeOfCentralDir; + uint32_t OffsetOfCentralDirOffset; + uint16_t LengthOfCommentLength; // var[zipFileCommentLength] zipFileComment }; diff --git a/main.cpp b/main.cpp index 17c052a..8cb65fe 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,17 @@ #include +#include +#include #include "Define.h" -void Zipper(); +int ZipArchiver(); int main() { std::printf("==================== Hello Zip! ====================\r\n"); - int iRet = 0; - int iSizeTest = sizeof(LocalFileHeader); + // Zip Format define check + size_t iSizeTest = sizeof(LocalFileHeader); if (LOCAL_FILE_HEADER_BYTE != iSizeTest) { std::printf("1 Wrong Header Size : %d / %d \r\n", iSizeTest, LOCAL_FILE_HEADER_BYTE); @@ -28,19 +30,48 @@ int main() iRet = -1; } + // Main Zip archive method if (iRet != -1) { - Zipper(); + iRet = ZipArchiver(); } - return 0; + return iRet; } -void Zipper() +int ZipArchiver() { LocalFileHeader LFHeader; CentralDirectory CentDirec; EoCDRecord ECDR; + using namespace std; -} \ No newline at end of file + ifstream fileIn("MyZip.txt", ios::binary); + bool iRet = fileIn.is_open(); + if (!iRet) + { + printf("File open failed"); + } + else + { + // ũ + // seekg(offset, ǥ) + fileIn.seekg(0, ios::end); + streamsize fileInSize = fileIn.tellg(); + // beg == begin + fileIn.seekg(0, ios::beg); + + // TODO : Buffering . + // ޸ Ҵ + uint8_t* pFile = (uint8_t *)malloc(fileInSize); + + // Read + fileIn.read((char *)pFile, fileInSize); + + // Free + free(pFile); + } + + return iRet; +}