📄 stafziplocalfileheadercommon.cpp
字号:
+ "]"); if (stream.avail_in > rest_compressed) { stream.avail_in = (uInt)rest_compressed; } // set file position to the next compressed data if (fseek(zf, pos + startPos, SEEK_SET) != 0) { *result = STAFString("STAFZipLocalFileHeader::doExtract: ") + "Error set pos to next compressed data [" + pos + "].\n"; rc = kZIPGeneralZipError; break; } // read stream.avail_in bytes of data from file to inbuf if (fread(inbuf, stream.avail_in, 1, zf) != 1) { *result = STAFString("STAFZipLocalFileHeader::doExtract: ") + "Error reading file bytes [" + stream.avail_in + "].\n"; rc = kSTAFFileReadError; break; } // prepare file position to next compressed data pos += stream.avail_in; // reduce rest_compressed data size rest_compressed -= stream.avail_in; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP11") + " stream.avail_in [" + stream.avail_in + "] rest_compressed [" + rest_compressed + "] pos [" + pos + "]"); // set zip stream to point to inbuf stream.next_in = (Bytef*)inbuf; } STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP12") + " compressionMethod [" + compressionMethod + "]"); // if no compression if (compressionMethod == 0) { uLong processed_bytes; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP13") + " stream.avail_in [" + stream.avail_in + "] stream.avail_out [" + stream.avail_out + "]"); if (stream.avail_out < stream.avail_in) { processed_bytes = stream.avail_out; } else { processed_bytes = stream.avail_in; } STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP14") + " processed_bytes [" + processed_bytes + "]"); for (int i = 0; i < processed_bytes; i++) { *(stream.next_out + i) = *(stream.next_in + i); } newcrc32 = crc32(newcrc32, stream.next_out, processed_bytes); stream.avail_in -= processed_bytes; stream.avail_out -= processed_bytes; stream.next_out += processed_bytes; stream.next_in += processed_bytes; stream.total_out += processed_bytes; uncompressed_bytes += processed_bytes; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP15") + " stream.avail_in [" + stream.avail_in + " stream.avail_out [" + stream.avail_out + " stream.total_out [" + stream.total_out + " uncompressed_bytes [" + uncompressed_bytes + " newcrc32 [" + newcrc32 + "]"); } else { // start deflating the file uLong total_out_before, processed_bytes; total_out_before = stream.total_out; const Bytef *bufBefore; bufBefore = stream.next_out; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP16") + " total_out_before [" + total_out_before + "]"); // unzip err = inflate(&stream, Z_SYNC_FLUSH); processed_bytes = stream.total_out - total_out_before; // calculate crc newcrc32 = crc32(newcrc32, bufBefore, (uInt)(processed_bytes)); // calculate uncompressed bytes uncompressed_bytes += (uInt)processed_bytes; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP17") + " err [" + err + "] stream.total_out [" + stream.total_out + "] processed_bytes [" + processed_bytes + "] newcrc32 [" + newcrc32 + "] uncompressed_bytes [" + uncompressed_bytes + "]"); if (err != Z_OK) { *result = STAFString("STAFZipLocalFileHeader::doExtract: ") + "Error inflate file, err [" + err + "].\n"; break; } } // compressionMethod == 0? } // while stream.avail_out > 0 if (rc != kSTAFOk || (err != Z_OK && err != Z_STREAM_END)) { if ((*result).length() == 0) { *result = STAFString("STAFZipLocalFileHeader::doExtract: ") + "Error deflating file, err [" + err + "].\n"; } rc = kZIPGeneralZipError; break; } STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP20") + " uncompressed_bytes [" + uncompressed_bytes + "]"); // write the unzipped buffer to file if (uncompressed_bytes > 0) { if (fwrite(outbuf, uncompressed_bytes, 1, outfile) != 1) { *result = STAFString("STAFZipLocalFileHeader::doExtract: ") + "Error writing file bytes [" + uncompressed_bytes + "].\n"; rc = kSTAFFileWriteError; break; } rest_uncompressed -= uncompressed_bytes; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP22") + " rest_uncompressed [" + rest_uncompressed + "]"); } } // loop to read compressed data from zip file if (compressionMethod != 0) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP23") + " compressionMethod [" + compressionMethod + "]"); // inflate end inflateEnd(&stream); } // compare crc if (rc == kSTAFOk) { if (newcrc32 != crc) { *result = STAFString("STAFZipLocalFileHeader::doExtract: ") + "Bad CRC new crc [" + newcrc32 + "] old crc [" + crc + "].\n"; rc = kZIPBadCRC; } } free(inbuf); free(outbuf); return rc;}// destructorSTAFZipLocalFileHeader::~STAFZipLocalFileHeader(){ if (fullFileName != NULL) { free(fullFileName); } if (extraField != NULL) { free(extraField); }}// extract zip archive to output dirSTAFRC_t STAFZipLocalFileHeader::extract(FILE *zf, uLong startPos, const char *outputdir, STAFString *result){ STAFRC_t rc; STAFZipUtil util = STAFZipUtil(); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::extract_CP1") + " outputdir [" + outputdir + "]"); fullFileName = (char*)calloc(strlen(outputdir) + fileNameLength + 1, 1); if (fullFileName == NULL) { *result = STAFString("STAFZipLocalFileHeader::extract: ") + "Error allocating memory for fullFileName [" + (strlen(outputdir) + fileNameLength + 1) + "].\n"; return kZIPNotEnoughMemory; } strcpy(fullFileName, outputdir); strcat(fullFileName, fileName); // free the allocated space in constructer free(fileName); fileName = fullFileName + strlen(outputdir); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::extract_CP3") + " fileName [" + fileName + "] fullFileName [" + fullFileName + "]"); if (*(fileName + strlen(fileName) - 1) == '\\' || *(fileName + strlen(fileName) - 1) == '/') { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::extract_CP4")); // create dir if ((rc = util.makeDir(fullFileName)) != kSTAFOk) { *result = STAFString("STAFZipLocalFileHeader::extract: ") + "Error making dir [" + fullFileName + "].\n"; } return rc; } FILE *outfile; // make sure the target directories exist before extracting files if ((outfile = fopen(fullFileName, "wb")) == NULL) { // finding fileName without path char *p, *filename_withoutpath; p = filename_withoutpath = fullFileName; while ((*p) != '\0') { if (((*p) == '/') || ((*p) == '\\')) { filename_withoutpath = p + 1; } p++; } STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::extract_CP5") + " filename_withoutpath [" + filename_withoutpath + "]"); // store the char before the file name without path char c = *(filename_withoutpath - 1); // to form a string which contains only the directory *(filename_withoutpath - 1) = '\0'; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::extract_CP6") + " fullFileName [" + fullFileName + "]"); // make the directory if ((rc = util.makeDir(fullFileName)) != kSTAFOk) { *result = STAFString("STAFZipLocalFileHeader::extract: ") + "Error making directory: [" + fullFileName + "].\n"; return rc; } *(filename_withoutpath - 1) = c; if ((outfile = fopen(fullFileName, "wb")) == NULL) { *result = STAFString("STAFZipLocalFileHeader::extract: ") + "Error creating file [" + fullFileName + "].\n"; return kZIPGeneralZipError; } } rc = doExtract(zf, startPos, outfile, result); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::extract_CP8") + " rc [" + rc + "]"); fclose(outfile); if (rc == kSTAFOk) { tm filedate; util.fileTime(lastModifiedTimeDate, &filedate); util.changeFileDate(fullFileName, lastModifiedTimeDate, filedate); } return rc;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -