int memZipFileWrite(MemZipFile* zipFile, const char* filename, void* buffer, int size) { static const int MemFileCount = sizeof(zipFile->memFile) / sizeof(zipFile->memFile[0]); unsigned long compSize; void* compBuf; MemFile* memFile = NULL; int i; for (i = 0; i < zipFile->count; i++) { if (strcmp(filename, zipFile->memFile[i].filename) == 0) { memFile = &zipFile->memFile[i]; free(memFile->data); } } if (memFile == NULL && zipFile->count < MemFileCount) { memFile = &zipFile->memFile[zipFile->count++]; } if (memFile == NULL) { return 0; } compBuf = zipCompress(buffer, size, &compSize); memFile->data = malloc(compSize); memcpy(memFile->data, compBuf, compSize); memFile->size = memFile->data ? size : 0; memFile->compSize = compSize; strcpy(memFile->filename, filename); free(compBuf); return 1; }
ALERROR WriteResource (CTDBCompiler &Ctx, const CString &sFilename, const CString &sFolder, bool bCompress, CDataFile &Out) { ALERROR error; CString sFilespec = pathAddComponent(sFolder, sFilename); CFileReadBlock theFile(pathAddComponent(Ctx.GetRootPath(), sFilespec)); if (error = theFile.Open()) { Ctx.ReportError(strPatternSubst(CONSTLIT("Unable to open '%s'."), sFilespec)); return error; } CString sData(theFile.GetPointer(0, -1), theFile.GetLength(), TRUE); if (bCompress) { CBufferReadBlock Input(sData); CMemoryWriteStream Output; if (error = Output.Create()) return ERR_FAIL; CString sError; if (!zipCompress(Input, compressionZlib, Output, &sError)) { Ctx.ReportError(sError); return ERR_FAIL; } sData = CString(Output.GetPointer(), Output.GetLength()); } int iEntry; if (error = Out.AddEntry(sData, &iEntry)) { Ctx.ReportError(strPatternSubst(CONSTLIT("Unable to store '%s'."), sFilespec)); return error; } Ctx.AddResource(sFilespec, iEntry, bCompress); printf(" %s\n", sFilespec.GetASCIIZPointer()); return NOERROR; }
int main() { const char *dataString = "*#$&$(@KFI#*$(SDBM@#*!(@%" "*#$&$(@KFI#*$(SDBM@#*!(@%" "*#$&$(@KFI#*$(SDBM@#*!(@F" "*#$&$(@KFI#*$(SDBM@#*!(@F" "*#$&$(@KFI#*$(SDBM@#*!(@F" "*#$&$(@KFI#*$(SDBM@#*!(@%a"; printf( "base64 encoding the string: %s\n", dataString ); char *encoding = base64Encode( (unsigned char *)dataString, strlen( dataString ), true ); printf( "Encoded as:\n%s\n", encoding ); int decodedLength; unsigned char *decoding = base64Decode( encoding, &decodedLength ); char *buffer = new char[ decodedLength + 1 ]; memcpy( (void *)buffer, (void *)decoding, decodedLength ); buffer[ decodedLength ] = '\0'; printf( "Decoded as: %s\n", buffer ); if( strcmp( buffer, dataString ) == 0 ) { printf( "Test passed\n" ); } else { printf( "Test failed\n" ); } delete [] buffer; delete [] decoding; delete [] encoding; printf( "zipCompressing the string: %s\n", dataString ); int compLength; unsigned char *compressed = zipCompress( (unsigned char *)dataString, strlen( dataString ) + 1, &compLength ); if( compressed != NULL ) { unsigned char *rawData = zipDecompress( compressed, compLength, strlen( dataString ) + 1 ); if( rawData != NULL ) { printf( "zipDecompressed as: %s\n", rawData ); if( strcmp( (char*)rawData, dataString ) == 0 ) { printf( "Test passed\n" ); } else { printf( "Test failed\n" ); } delete [] rawData; } delete [] compressed; } return 0; }