diff --git a/ZipDefine.h b/ZipDefine.h index 341d7ba..58e0a00 100644 --- a/ZipDefine.h +++ b/ZipDefine.h @@ -2,6 +2,11 @@ #include +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: [local file header 1] -> "PK" + 0x4b50 + ... @@ -61,8 +66,8 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30; uint16_t lengthOfFileName; uint16_t lengthOfExtraField; - // var[fileNameLength] fileName - // var[extraFieldLength] extraField + // var[lengthOfFileName] fileName + // var[lengthOfExtraField] extraField }; /** Central directory structure: @@ -109,20 +114,20 @@ 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; - uint16_t fileCommentLength; + uint32_t sizeOfCompressed; // same as unCompression (cuz no compreesion) + uint32_t sizeOfUncompressed; + uint16_t lengthOfFileName; + uint16_t lengthOfExtraField; + uint16_t lengthOfFileComment; uint16_t diskNumStart; uint16_t attributeInternal; uint32_t attributeExternal; uint32_t offsetLocalHeader; - // var[fileNameLength] fileName - // var[extraFieldLength] extraField - // var[fileCommentLength] fileComment + // var[lengthOfFileName] fileName + // var[lengthOfExtraField] extraField + // var[lengthOfFileComment] fileComment }; /** End of central directory record: @@ -168,12 +173,11 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30; // .ZIP 파일 내 전체 파일 수 uint16_t numOfEntries; - uint32_t SizeOfCentralDir; uint32_t OffsetOfCentralDirOffset; uint16_t LengthOfCommentLength; - // var[zipFileCommentLength] zipFileComment + // var[LengthOfCommentLength] zipFileComment }; #pragma pack(pop) diff --git a/main.cpp b/main.cpp index 79618ec..adfae4c 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,8 @@ #include "ZipDefine.h" #include "CRC.h" +#define TARGET_FILE_NAME "MyZip.txt" + bool ZipArchiver(); uint32_t getCRC32(const uint8_t* pData, size_t length); @@ -49,11 +51,11 @@ bool ZipArchiver() { LocalFileHeader LFHeader = {}; CentralDirectory CentDir = {}; - EoCDRecord ECDR = {}; + EoCDRecord EOCDR = {}; using namespace std; - ifstream fileIn("MyZip.txt", ios::binary); + ifstream fileIn(TARGET_FILE_NAME, ios::binary); bool iRet = fileIn.is_open(); if (!iRet) { @@ -75,13 +77,62 @@ bool ZipArchiver() // Read fileIn.read((char *)pFile, fileInSize); - // Get CRC value - // TODO : streamsize 와 size_t 의 mismatch (long long <-> unsigned long long) - uint32_t myCRC = getCRC32(pFile, fileInSize); + // ZIP 포맷 채워넣기 + { + // Get CRC value + // TODO : streamsize 와 size_t 의 mismatch (long long <-> unsigned long long) + uint32_t myCRC = getCRC32(pFile, fileInSize); - // Put CRC value - LFHeader.crc32 = myCRC; - CentDir.crc32 = myCRC; + // Put PK Signature + LFHeader.signature = PK_SIGNATURE_LF_HEADER; + 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(pFile);