📄 stafziplocalfileheadercommon.cpp
字号:
stream.next_out = (Bytef*)outbuf; stream.avail_out = (uInt)Z_BUFSIZE; // set compressed bytes to 0 compressed_bytes = 0; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP19") + " stream.avail_out [" + stream.avail_out + "] stream.avail_in [" + stream.avail_in + "]"); // loop to zip data while (stream.avail_out > 0 && err == Z_OK) { if (stream.avail_in == 0) { // read Z_BUFSIZE of bytes from file stream.avail_in = fread(inbuf, 1, Z_BUFSIZE, fin); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP20") + " stream.avail_in [" + stream.avail_in + "]"); // if size read < Z_BUFSIZE and not end of file, return err if (stream.avail_in < Z_BUFSIZE) { if (feof(fin) == 0) { *result = STAFString("STAFZipLocalFileHeader::flush: ") + "Error in reading file [" + fullFileName + "].\n"; rc = kSTAFFileReadError; break; } } // if size read > 0 if (stream.avail_in > 0) { // set next in stream.next_in = (Bytef*)inbuf; // calculate crc crc = crc32(crc, stream.next_in, stream.avail_in); } } // read in raw data from file // save the total out before delfating uLong uTotalOutBefore = stream.total_out; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP22") + " stream.total_out [" + stream.total_out + "] stream.avail_in [" + stream.avail_in + "]"); if (stream.avail_in == 0) { // do delate with Z_FINISH err = deflate(&stream, Z_FINISH); } else { // do delate with Z_NO_FLUSH err = deflate(&stream, Z_NO_FLUSH); } // calculate compressed size compressed_bytes += (uInt)(stream.total_out - uTotalOutBefore); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP23") + " stream.total_out [" + stream.total_out + "] uTotalOutBefore [" + uTotalOutBefore + "] compressed_bytes [" + compressed_bytes + "]"); } // loop to zip data // if error, break if (rc != kSTAFOk || (err != Z_STREAM_END && err != Z_OK)) { if ((*result).length() == 0) { *result = STAFString("STAFZipLocalFileHeader::flush: ") + "Error in deflating [" + err + "].\n"; } rc = kZIPGeneralZipError; break; } // write the unzipped buffer to file STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP25") + " compressed_bytes [" + compressed_bytes + "]"); if (compressed_bytes > 0) { if (fwrite(outbuf, compressed_bytes, 1, zf)!=1) { *result = STAFString("STAFZipLocalFileHeader::flush: ") + "Error writting compressed bytes [" + compressed_bytes + "].\n"; rc = kSTAFFileWriteError; break; } } } // loop to read data from original file fclose(fin); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP27") + " err [" + err + "] rc [" + rc + "]"); // end the deflate if (err == Z_STREAM_END && rc == kSTAFOk) { err = deflateEnd(&stream); } free(inbuf); free(outbuf); if (err != Z_OK) { if ((*result).length() == 0) { *result = STAFString("STAFZipLocalFileHeader::flush: ") + "Error finishing deflating [" + err + "].\n"; } rc = kZIPGeneralZipError; } if (rc != kSTAFOk) { return rc; } compressedSize = stream.total_out; uncompressedSize = stream.total_in; // save current end of local file header position uLong curpos = ftell(zf); STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP30") + " curpos [" + curpos + "]"); // update crc, compressed size, uncompressed size if (fseek(zf, offset + 14, SEEK_SET) != 0) { *result = STAFString("STAFZipLocalFileHeader::flush: ") + "Error fseek crc, compressed uncompressed size [" + (offset + 14) + "].\n"; rc = kZIPGeneralZipError; } // save crc32 if (rc == kSTAFOk) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP32") + " crc [" + crc + "]"); rc = util.putValue(zf, (uLong)crc, 4); } // save compressed size if (rc == kSTAFOk) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP33") + " compressedSize [" + compressedSize + "]"); rc = util.putValue(zf, (uLong)compressedSize, 4); } // save uncompressed size if (rc == kSTAFOk) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP34") + " uncompressedSize [" + uncompressedSize + "]"); rc = util.putValue(zf, (uLong)uncompressedSize, 4); } // restore the previous end of local file header position if (fseek(zf, curpos, SEEK_SET) != 0 && rc == kSTAFOk) { *result = STAFString("STAFZipLocalFileHeader::flush: ") + "Error restore end of local header pos [" + curpos + "].\n"; rc = kZIPGeneralZipError; } } // zip the file size = ftell(zf) - offset; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::flush_CP36") + " size [" + size + "]"); return rc;}// extract one file from zip archive by using zlib utilitySTAFRC_t STAFZipLocalFileHeader::doExtract(FILE *zf, uLong startPos, FILE *outfile, STAFString *result){ z_stream stream; stream.total_out = 0; int err = Z_OK; STAFRC_t rc = kSTAFOk; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP1") + " rc [" + rc + "]"); if (compressionMethod != 0) { stream.zalloc = (alloc_func)0; stream.zfree = (free_func)0; stream.opaque = (voidpf)0; err = inflateInit2(&stream, -MAX_WBITS); /* windowBits is passed < 0 to tell that there is no zlib header. * Note that in this case inflate *requires* an extra "dummy" byte * after the compressed stream in order to complete decompression and * return Z_STREAM_END. * In unzip, i don't wait absolutely Z_STREAM_END because I known the * size of both compressed and uncompressed data */ if (err != Z_OK) { *result = STAFString("STAFZipLocalFileHeader::doExtract: ") + "Error init zlib inflate [" + err + "].\n"; return kZIPGeneralZipError; } } // allocate input buffer void *inbuf = (void*)malloc(Z_BUFSIZE); if(inbuf == NULL) { *result = STAFString("STAFZipLocalFileHeader::doExtract: ") + "Error allocating memory for input buffer [" + Z_BUFSIZE + "].\n"; return kZIPNotEnoughMemory; } // allocate output buffer void *outbuf = (void*)malloc(Z_BUFSIZE); if(outbuf == NULL) { *result = STAFString("STAFZipLocalFileHeader::doExtract: ") + "Error allocating memory for output buffer [" + Z_BUFSIZE + "].\n"; return kZIPNotEnoughMemory; } uLong pos = offset + size - compressedSize; uLong rest_uncompressed, rest_compressed; rest_uncompressed = uncompressedSize; rest_compressed = compressedSize; uInt uncompressed_bytes; uLong newcrc32 = 0; stream.avail_in = (uInt)0; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP4.5") + " pos [" + pos + "] stream.avail_in [" + stream.avail_in + "] rest_uncompressed [" + rest_uncompressed + "] rest_compressed [" + rest_compressed + "]"); // loop to read compressed data from zip file while (rest_uncompressed > 0) { // unzip buffer stream.next_out = (Bytef*)outbuf; stream.avail_out = (uInt)Z_BUFSIZE; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP5") + " stream.avail_out [" + stream.avail_out + "] rest_uncompressed [" + rest_uncompressed + "]"); if (stream.avail_out > rest_uncompressed) { stream.avail_out = (uInt)rest_uncompressed; } uncompressed_bytes = 0; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP6") + " stream.avail_out [" + stream.avail_out + "]"); // loop to unzip compressed data while (stream.avail_out > 0) { STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP7") + " stream.avail_in [" + stream.avail_in + "] rest_compressed [" + rest_compressed + "]"); if (stream.avail_in == 0 && rest_compressed != 0) { // set available in stream.avail_in = Z_BUFSIZE; STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipLocalFileHeader::doExtract_CP8") + " stream.avail_in [" + stream.avail_in + "] rest_compressed [" + rest_compressed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -