📄 stafzipcentraldir.cpp
字号:
rc = util.putValue(zf, (uLong)cder->centralDirSize, 4); } // central dir offset if (rc == kSTAFOk) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::flush_CP27") + " centralDirOffset [" + cder->centralDirOffset + "]"); rc = util.putValue(zf, (uLong)cder->centralDirOffset, 4); } // comment length if (rc == kSTAFOk) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::flush_CP28") + " commentLength [" + cder->commentLength + "]"); rc = util.putValue(zf, (uLong)cder->commentLength, 2); } // comment if (rc == kSTAFOk && cder->commentLength > 0) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::flush_CP29") + " comment [" + cder->comment + "]"); if (fwrite(cder->comment, (uInt)cder->commentLength, 1, zf) != 1) { *result = STAFString("STAFZipCentralDir::flush: ") + "Error writing comment to end record [" + cder->comment + "].\n"; rc = kZIPGeneralZipError; } } if (rc != kSTAFOk) { if ((*result).length() == 0) { *result = STAFString("STAFZipCentralDir::flush: ") + "Error writing data.\n"; } } return rc;}// Read in data from Zip archiveSTAFRC_t STAFZipCentralDir::readInData(FILE *zf, uLong startPos, uLong endPos, STAFString *result){ STAFZipUtil util = STAFZipUtil(); unsigned char* buf; uLong uSizeFile; uLong uBackRead; uLong uMaxBack = 0xffff; /* maximum size of global comment */ uLong uPosFound = 0; // go to the end of stream if (fseek(zf, endPos, SEEK_SET) != 0) { *result = STAFString("STAFZipCentralDir::readInData: ") + "Error in fseek to the end of zip stream: " + endPos; return kZIPGeneralZipError; } // get stream size uSizeFile = endPos - startPos; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP2") + " startPos [" + startPos + "]" + " endPos [" + endPos + "]" + " uSizeFile [" + uSizeFile + "]" ); if (uMaxBack > uSizeFile) { uMaxBack = uSizeFile; } buf = (unsigned char*)malloc(BUFREADCOMMENT + 4); if (buf == NULL) { *result = STAFString("STAFZipCentralDir::readInData: ") + "Error allocating buffer read comment memory [" + (BUFREADCOMMENT + 4) + "].\n"; return kZIPNotEnoughMemory; } uBackRead = 4; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP4") + " uBackRead [" + uBackRead + "] uMaxBack [" + uMaxBack + "]"); // search offset of central dir end record while (uBackRead < uMaxBack) { uLong uReadSize, uReadPos; int i; if (uBackRead + BUFREADCOMMENT > uMaxBack) { uBackRead = uMaxBack; } else { uBackRead += BUFREADCOMMENT; } uReadPos = uSizeFile - uBackRead; uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? (BUFREADCOMMENT+4) : (uSizeFile-uReadPos); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP5") + " uReadSize [" + uReadSize + "] uReadPos [" + uReadPos + "]"); // move to uReadPos of the file if (fseek(zf, uReadPos + startPos, SEEK_SET) != 0) { *result = STAFString("STAFZipCentralDir::readInData: ") + "Error fseek zip file uReadPos [" + uReadPos + "].\n"; break; } // read in a block of data from the file if (fread(buf, (uInt)uReadSize, 1, zf) != 1) { *result = STAFString("STAFZipCentralDir::readInData: ") + "Error read zip file size [" + uReadSize + "].\n"; break; } // search for the signature of central dir end record for (i = (int)uReadSize - 3; (i--) > 0;) { if (((*(buf + i)) == 0x50) && ((*(buf + i + 1)) == 0x4b) && ((*(buf + i + 2)) == 0x05) && ((*(buf + i + 3))== 0x06)) { uPosFound = uReadPos + i; break; } } if (uPosFound != 0) { break; } } free(buf); if (uPosFound <= 0) { if((*result).length() == 0) { *result = STAFString("STAFZipCentralDir::readInData: ") + "Can't find central dir end record.\n"; } return kZIPGeneralZipError; } // get attributes of central dir end record STAFRC_t rc; if (fseek(zf, uPosFound + startPos, SEEK_SET) != 0) { *result = STAFString("STAFZipCentralDir::readInData: ") + "Error in fseek central dir end record [" + uPosFound + "].\n"; return kZIPGeneralZipError; } // get signature rc = util.getLong(zf, &cder->signature); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP10") + " signature [" + cder->signature + "]"); // get number of this disk if (rc == kSTAFOk) { uLong iL; rc = util.getShort(zf, &iL); cder->numberDisk = (unsigned short)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP11") + " numberDisk [" + cder->numberDisk + "]"); } // get number of the disk with the start of central directory if (rc == kSTAFOk) { uLong iL; rc = util.getShort(zf, &iL); cder->numberDiskWithCentralDir = (unsigned short)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP12") + " numberDiskWithCentralDir [" + cder->numberDiskWithCentralDir + "]"); } // get total number of entries in the central directory on this disk if (rc == kSTAFOk) { uLong iL; rc = util.getShort(zf, &iL); cder->numberEntry = (unsigned short)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP13") + " numberEntry [" + STAFString(cder->numberEntry) + "]"); } // get total number of entries in the central directory if (rc == kSTAFOk) { uLong iL; rc = util.getShort(zf, &iL); cder->numberEntryWithCentralDir = (unsigned short)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP14") + " numberEntryWithCentralDir [" + STAFString(cder->numberEntryWithCentralDir) + "]"); } // check if (rc == kSTAFOk && (cder->numberEntryWithCentralDir != cder->numberEntry || cder->numberDiskWithCentralDir != 0 || cder->numberDisk != 0 || cder->numberEntry <= 0)) { *result = STAFString("STAFZipCentralDir::readInData: ") + "Check integrity error: number disk" + STAFString(cder->numberDisk) + "] number disk with central dir [" + STAFString(cder->numberDiskWithCentralDir) + "] number entry with central dir [" + STAFString(cder->numberEntryWithCentralDir) + "] number entry [" + STAFString(cder->numberEntry) + "].\n"; rc = kZIPGeneralZipError; } // get size of the central directory if (rc == kSTAFOk) { rc = util.getLong(zf, &cder->centralDirSize); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP15") + " centralDirSize [" + cder->centralDirSize + "]"); } // get offset of start of central directory with respect to the starting // disk number if (rc == kSTAFOk) { rc = util.getLong(zf, &cder->centralDirOffset); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP16") + " centralDirOffset [" + cder->centralDirOffset + "]"); } // get zipfile comment length if (rc == kSTAFOk) { uLong iL; rc = util.getShort(zf, &iL); cder->commentLength = (unsigned short)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP17") + " commentLength [" + cder->commentLength + "]"); } // get zipfile comment if (rc == kSTAFOk && cder->commentLength > 0) { cder->comment = (char*)calloc(cder->commentLength + 1, 1); if (cder->comment == NULL) { *result = STAFString("STAFZipCentralDir::readInData: ") + "Error allocating memory for comment length [" + (cder->commentLength + 1) + "].\n"; return kZIPNotEnoughMemory; } if (fread(cder->comment, (uInt)cder->commentLength, 1, zf) !=1 ) { *result = STAFString("STAFZipCentralDir::readInData: ") + "Error reading comment.\n"; rc = kSTAFFileReadError; } STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDir::readInData_CP20") + " comment [" + cder->comment + "]"); } // check data integrity if ((uPosFound != cder->centralDirOffset + startPos + cder->centralDirSize) && (rc == kSTAFOk)) { *result = STAFString("STAFZipCentralDir::readInData: ")
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -