fill basic ZIP header format

This commit is contained in:
2026-08-07 10:26:20 +09:00
parent 1d5dd34bed
commit 969bce540e
2 changed files with 75 additions and 20 deletions
+16 -12
View File
@@ -2,6 +2,11 @@
#include <cstdint> #include <cstdint>
const static uint32_t PK_SIGNATURE_LF_HEADER = 0x04034b50; // PK + 0x0403
const static uint32_t PK_SIGNATURE_CENT_DIR = 0x02014b50; // PK + 0x0201
const static uint32_t PK_SIGNATURE_EO_CDR = 0x06054b50; // PK + 0x0605
const static uint16_t METHOD_UNCOMPRESSED = 0;
/** Overall .ZIP file format: /** Overall .ZIP file format:
[local file header 1] -> "PK" + 0x4b50 + ... [local file header 1] -> "PK" + 0x4b50 + ...
@@ -61,8 +66,8 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30;
uint16_t lengthOfFileName; uint16_t lengthOfFileName;
uint16_t lengthOfExtraField; uint16_t lengthOfExtraField;
// var[fileNameLength] fileName // var[lengthOfFileName] fileName
// var[extraFieldLength] extraField // var[lengthOfExtraField] extraField
}; };
/** Central directory structure: /** Central directory structure:
@@ -109,20 +114,20 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30;
uint16_t lastModTime; // 압축 시점의 시간 (1980년 기준 시간) uint16_t lastModTime; // 압축 시점의 시간 (1980년 기준 시간)
uint16_t lastModDate; // 압축 시점의 날짜 (1980년 기준 연도) uint16_t lastModDate; // 압축 시점의 날짜 (1980년 기준 연도)
uint32_t crc32; uint32_t crc32;
uint32_t compressedSize; // same as unCompression (cuz no compreesion) uint32_t sizeOfCompressed; // same as unCompression (cuz no compreesion)
uint32_t unCompressedSize; uint32_t sizeOfUncompressed;
uint16_t fileNameLength; uint16_t lengthOfFileName;
uint16_t extraFieldLength; uint16_t lengthOfExtraField;
uint16_t fileCommentLength; uint16_t lengthOfFileComment;
uint16_t diskNumStart; uint16_t diskNumStart;
uint16_t attributeInternal; uint16_t attributeInternal;
uint32_t attributeExternal; uint32_t attributeExternal;
uint32_t offsetLocalHeader; uint32_t offsetLocalHeader;
// var[fileNameLength] fileName // var[lengthOfFileName] fileName
// var[extraFieldLength] extraField // var[lengthOfExtraField] extraField
// var[fileCommentLength] fileComment // var[lengthOfFileComment] fileComment
}; };
/** End of central directory record: /** End of central directory record:
@@ -168,12 +173,11 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30;
// .ZIP 파일 내 전체 파일 수 // .ZIP 파일 내 전체 파일 수
uint16_t numOfEntries; uint16_t numOfEntries;
uint32_t SizeOfCentralDir; uint32_t SizeOfCentralDir;
uint32_t OffsetOfCentralDirOffset; uint32_t OffsetOfCentralDirOffset;
uint16_t LengthOfCommentLength; uint16_t LengthOfCommentLength;
// var[zipFileCommentLength] zipFileComment // var[LengthOfCommentLength] zipFileComment
}; };
#pragma pack(pop) #pragma pack(pop)
+59 -8
View File
@@ -5,6 +5,8 @@
#include "ZipDefine.h" #include "ZipDefine.h"
#include "CRC.h" #include "CRC.h"
#define TARGET_FILE_NAME "MyZip.txt"
bool ZipArchiver(); bool ZipArchiver();
uint32_t getCRC32(const uint8_t* pData, size_t length); uint32_t getCRC32(const uint8_t* pData, size_t length);
@@ -49,11 +51,11 @@ bool ZipArchiver()
{ {
LocalFileHeader LFHeader = {}; LocalFileHeader LFHeader = {};
CentralDirectory CentDir = {}; CentralDirectory CentDir = {};
EoCDRecord ECDR = {}; EoCDRecord EOCDR = {};
using namespace std; using namespace std;
ifstream fileIn("MyZip.txt", ios::binary); ifstream fileIn(TARGET_FILE_NAME, ios::binary);
bool iRet = fileIn.is_open(); bool iRet = fileIn.is_open();
if (!iRet) if (!iRet)
{ {
@@ -75,13 +77,62 @@ bool ZipArchiver()
// Read // Read
fileIn.read((char *)pFile, fileInSize); fileIn.read((char *)pFile, fileInSize);
// Get CRC value // ZIP 포맷 채워넣기
// TODO : streamsize 와 size_t 의 mismatch (long long <-> unsigned long long) {
uint32_t myCRC = getCRC32(pFile, fileInSize); // Get CRC value
// TODO : streamsize 와 size_t 의 mismatch (long long <-> unsigned long long)
uint32_t myCRC = getCRC32(pFile, fileInSize);
// Put CRC value // Put PK Signature
LFHeader.crc32 = myCRC; LFHeader.signature = PK_SIGNATURE_LF_HEADER;
CentDir.crc32 = myCRC; CentDir.signature = PK_SIGNATURE_CENT_DIR;
EOCDR.signature = PK_SIGNATURE_EO_CDR;
// TOOD : versionMadeBy (Only CentDir)
// TODO : versionNeededToExtract
// TODO : generalBitFlag
// Put compressed method (uncompressed)
LFHeader.compressionMethod = METHOD_UNCOMPRESSED;
CentDir.compressionMethod = METHOD_UNCOMPRESSED;
// TODO : lastMode[Time|Date]
// 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 = strlen(TARGET_FILE_NAME);
LFHeader.lengthOfFileName = fileNameLeng;
CentDir.lengthOfFileName = fileNameLeng;
// Extra Field 는 사용하지 않음
// LFHeader.lengthOfExtraField = NOT_USED;
// CentDir.lengthOfExtraField = NOT_USED;
// TODO : CentDir 나머지 format
// - lengthOfFileComment
// - diskNumStart;
// - attributeInternal;
// - attributeExternal;
// - offsetLocalHeader;
// TODO : EOCDR 나머지 format
// - diskNumber
// - diskNumber_CentralDirStart
// - numOfCentralDir
// - numOfEntries;
// - SizeOfCentralDir;
// - OffsetOfCentralDirOffset;
// - LengthOfCommentLength;
}
// Free // Free
free(pFile); free(pFile);