CEncoder::CEncoder() { _encoder = 0; _encoder = Lzma2Enc_Create(&g_Alloc, &g_BigAlloc); if (_encoder == 0) throw 1; }
static SRes Lzma2WithFilters_Create(CLzma2WithFilters *p) { p->lzma2 = Lzma2Enc_Create(p->alloc, p->bigAlloc); if (p->lzma2 == 0) return SZ_ERROR_MEM; return SZ_OK; }
void CompressWithLZMA2(std::vector<unsigned char> &outBuf, const std::vector<unsigned char> &inBuf, Byte* ptrProperties) { VectorInStream inStream = { &VectorInStream_Read, &inBuf, 0 }; VectorOutStream outStream = { &VectorOutStream_Write, &outBuf }; CLzma2EncHandle enc; enc = Lzma2Enc_Create(&g_Alloc, &g_BigAlloc); assert(enc); CLzma2EncProps props; Lzma2EncProps_Init(&props); //Ivan need change the parameters later //props.lzmaProps.writeEndMark = 1; // 0 or 1 //props.lzmaProps.level = 6; //props.lzmaProps.dictSize = 1 << 14; //props.lzmaProps.numThreads = 8; //props.numTotalThreads = 8; SRes res = Lzma2Enc_SetProps(enc, &props); assert(res == SZ_OK); outBuf.resize(1); // no need outBuf[0] = Lzma2Enc_WriteProperties(enc); *ptrProperties = outBuf[0]; //no need this parameter. UInt64 resLen = inBuf.size(); Byte header[8]; for (int i = 0; i < 8; i++) header[i] = (Byte)(resLen >> (8 * i)); res = Lzma2Enc_Encode(enc, &outStream.SeqOutStream, &inStream.SeqInStream, 0); //res = Lzma2Enc_Encode(enc, (ISeqOutStream*)&outStream, (ISeqInStream*)&inStream, 0); assert(res == SZ_OK); Lzma2Enc_Destroy(enc); FILE *fout = fopen("data.dc.dat", "wb+"); //change the file name. fwrite(&outBuf[0], 1, 1, fout); fwrite(&header[0], 1, 8, fout); fwrite(&outBuf[1], 1, outBuf.size() - 1, fout); fclose(fout); }