add basic binary fread

This commit is contained in:
2026-08-07 09:06:39 +09:00
parent f175be3938
commit 006ea5b62b
2 changed files with 67 additions and 18 deletions
+29 -11
View File
@@ -56,10 +56,10 @@ 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;
// var[fileNameLength] fileName // var[fileNameLength] fileName
// var[extraFieldLength] extraField // var[extraFieldLength] extraField
@@ -101,7 +101,6 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30;
constexpr int CENTRAL_DIRECTORY_BYTE = 46; constexpr int CENTRAL_DIRECTORY_BYTE = 46;
struct CentralDirectory { struct CentralDirectory {
uint32_t signature; uint32_t signature;
uint16_t versionMadeBy; uint16_t versionMadeBy;
uint16_t versionNeededToExtract; uint16_t versionNeededToExtract;
@@ -130,16 +129,26 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30;
end of central dir signature 4 bytes (0x0605 + 4b50) end of central dir signature 4 bytes (0x0605 + 4b50)
number of this disk 2 bytes number of this disk 2 bytes
// 중앙 디렉터리가 시작되는 디스크의 번호.
number of the disk with the number of the disk with the
start of the central directory 2 bytes start of the central directory 2 bytes
// 이 디스크에 있는 중앙 디렉터리 항목의 수
total number of entries in the total number of entries in the
central directory on this disk 2 bytes central directory on this disk 2 bytes
// .ZIP 파일 내 전체 파일 수
total number of entries in total number of entries in
the central directory 2 bytes the central directory 2 bytes
size of the central directory 4 bytes size of the central directory 4 bytes
// 중앙 디렉터리가 시작되는 디스크 상에서 중앙 디렉터리 시작 지점의 오프셋
offset of start of central offset of start of central
directory with respect to directory with respect to
the starting disk number 4 bytes the starting disk number 4 bytes
.ZIP file comment length 2 bytes .ZIP file comment length 2 bytes
.ZIP file comment (variable size) .ZIP file comment (variable size)
*/ */
@@ -147,13 +156,22 @@ constexpr int LOCAL_FILE_HEADER_BYTE = 30;
struct EoCDRecord { struct EoCDRecord {
uint32_t signature; uint32_t signature;
// central directory end record을 포함하는 이 디스크의 번호
uint16_t diskNumber; uint16_t diskNumber;
uint16_t diskNumber_CentralDirectoryStart;
uint16_t numOfCentralDirectory; // TODO : 이름 애매함. // 중앙 디렉터리가 시작되는 디스크의 번호.
uint16_t numEntries; // TODO : 이름 애매함. uint16_t diskNumber_CentralDirStart;
uint32_t centralDirectorySize;
uint32_t centralDirectoryOffset; // 이 디스크에 있는 중앙 디렉터리 항목의 수
uint16_t zipFileCommentLength; uint16_t numOfCentralDir;
// .ZIP 파일 내 전체 파일 수
uint16_t numOfEntries;
uint32_t SizeOfCentralDir;
uint32_t OffsetOfCentralDirOffset;
uint16_t LengthOfCommentLength;
// var[zipFileCommentLength] zipFileComment // var[zipFileCommentLength] zipFileComment
}; };
+38 -7
View File
@@ -1,15 +1,17 @@
#include <cstdio> #include <cstdio>
#include <iostream>
#include <fstream>
#include "Define.h" #include "Define.h"
void Zipper(); int ZipArchiver();
int main() int main()
{ {
std::printf("==================== Hello Zip! ====================\r\n"); std::printf("==================== Hello Zip! ====================\r\n");
int iRet = 0; int iRet = 0;
int iSizeTest = sizeof(LocalFileHeader); // Zip Format define check
size_t iSizeTest = sizeof(LocalFileHeader);
if (LOCAL_FILE_HEADER_BYTE != iSizeTest) if (LOCAL_FILE_HEADER_BYTE != iSizeTest)
{ {
std::printf("1 Wrong Header Size : %d / %d \r\n", iSizeTest, LOCAL_FILE_HEADER_BYTE); std::printf("1 Wrong Header Size : %d / %d \r\n", iSizeTest, LOCAL_FILE_HEADER_BYTE);
@@ -28,19 +30,48 @@ int main()
iRet = -1; iRet = -1;
} }
// Main Zip archive method
if (iRet != -1) if (iRet != -1)
{ {
Zipper(); iRet = ZipArchiver();
} }
return 0; return iRet;
} }
void Zipper() int ZipArchiver()
{ {
LocalFileHeader LFHeader; LocalFileHeader LFHeader;
CentralDirectory CentDirec; CentralDirectory CentDirec;
EoCDRecord ECDR; EoCDRecord ECDR;
using namespace std;
} 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;
}