add ZIP archive file write
- 기초적인 Zip File Write 구현 - 로컬 테스트 (MyZip.txt -> MyZip.zip) 통과.
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
|
||||
#include "ZipDefine.h"
|
||||
#include "CRC.h"
|
||||
|
||||
#define TARGET_FILE_NAME "MyZip.txt"
|
||||
#define INPUT_FILE_NAME "MyZip.txt"
|
||||
#define OUTPUT_FILE_NAME "MyZip.zip"
|
||||
|
||||
bool ZipArchiver();
|
||||
uint32_t getCRC32(const uint8_t* pData, size_t length);
|
||||
uint32_t getCRC32(const char* pData, size_t length);
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -55,11 +57,11 @@ bool ZipArchiver()
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream fileIn(TARGET_FILE_NAME, ios::binary);
|
||||
ifstream fileIn(INPUT_FILE_NAME, ios::binary);
|
||||
bool iRet = fileIn.is_open();
|
||||
if (!iRet)
|
||||
{
|
||||
printf("File open failed");
|
||||
printf("FileIn open failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -72,15 +74,15 @@ bool ZipArchiver()
|
||||
|
||||
// TODO : Buffering 으로 구현. 현재는 파일 통짜로 읽음.
|
||||
// 메모리 할당
|
||||
uint8_t* pFile = (uint8_t *)malloc(fileInSize);
|
||||
auto pFile = (char *)malloc(fileInSize);
|
||||
|
||||
// Read
|
||||
fileIn.read((char *)pFile, fileInSize);
|
||||
|
||||
fileIn.read(pFile, fileInSize);
|
||||
// ZIP 포맷 채워넣기
|
||||
// TODO : 여기있을 필요까진 없는듯. 코드 이관 필요
|
||||
{
|
||||
// Get CRC value
|
||||
// TODO : streamsize 와 size_t 의 mismatch (long long <-> unsigned long long)
|
||||
// TODO : 현재 streamsize 와 size_t 의 mismatch 존재. 해결필요.(long long <-> unsigned long long)
|
||||
uint32_t myCRC = getCRC32(pFile, fileInSize);
|
||||
|
||||
// Put PK Signature
|
||||
@@ -88,15 +90,18 @@ bool ZipArchiver()
|
||||
CentDir.signature = PK_SIGNATURE_CENT_DIR;
|
||||
EOCDR.signature = PK_SIGNATURE_EO_CDR;
|
||||
|
||||
// TOOD : versionMadeBy (Only CentDir)
|
||||
// TODO : versionNeededToExtract
|
||||
// TODO : generalBitFlag
|
||||
// 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;
|
||||
|
||||
// TODO : lastMode[Time|Date]
|
||||
// lastMode[Time|Date] => Not Used.
|
||||
|
||||
// Put CRC value
|
||||
LFHeader.crc32 = myCRC;
|
||||
@@ -109,7 +114,7 @@ bool ZipArchiver()
|
||||
CentDir.sizeOfCompressed = CentDir.sizeOfUncompressed;
|
||||
|
||||
// Put File name length
|
||||
uint16_t fileNameLeng = strlen(TARGET_FILE_NAME);
|
||||
uint16_t fileNameLeng = strlen(INPUT_FILE_NAME);
|
||||
LFHeader.lengthOfFileName = fileNameLeng;
|
||||
CentDir.lengthOfFileName = fileNameLeng;
|
||||
|
||||
@@ -117,21 +122,52 @@ bool ZipArchiver()
|
||||
// LFHeader.lengthOfExtraField = NOT_USED;
|
||||
// CentDir.lengthOfExtraField = NOT_USED;
|
||||
|
||||
// TODO : CentDir 나머지 format
|
||||
// - lengthOfFileComment
|
||||
// - diskNumStart;
|
||||
// - attributeInternal;
|
||||
// - attributeExternal;
|
||||
// - offsetLocalHeader;
|
||||
// TODO : CentDir Temp format 채우기.
|
||||
CentDir.lengthOfFileComment = 0;
|
||||
CentDir.diskNumStart = 0; // Temp
|
||||
CentDir.attributeInternal = 0; // Temp
|
||||
CentDir.attributeExternal = 0; // Temp
|
||||
// CentDir.offsetLocalFileHeader -> 실제 파일 Write 중에 작성.
|
||||
|
||||
// TODO : EOCDR 나머지 format
|
||||
// - diskNumber
|
||||
// - diskNumber_CentralDirStart
|
||||
// - numOfCentralDir
|
||||
// - numOfEntries;
|
||||
// - SizeOfCentralDir;
|
||||
// - OffsetOfCentralDirOffset;
|
||||
// - LengthOfCommentLength;
|
||||
// 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 = fileOut.tellp();
|
||||
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(pFile, fileInSize);
|
||||
|
||||
// 3. CentralDirectory, file name (var)
|
||||
EOCDR.offsetCentralDir = fileOut.tellp();
|
||||
fileOut.write(reinterpret_cast<const char*>(&CentDir), sizeof(CentDir));
|
||||
fileOut.write(INPUT_FILE_NAME, CentDir.lengthOfFileName);
|
||||
|
||||
// 4. End of CentralDirectory Record
|
||||
fileOut.write(reinterpret_cast<const char*>(&EOCDR), sizeof(EOCDR));
|
||||
|
||||
fileOut.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Free
|
||||
@@ -141,7 +177,7 @@ bool ZipArchiver()
|
||||
return iRet;
|
||||
}
|
||||
|
||||
uint32_t getCRC32(const uint8_t* pData, size_t length)
|
||||
uint32_t getCRC32(const char* pData, size_t length)
|
||||
{
|
||||
uint32_t crc = 0xFFFFFFFF;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user