zip.c
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 678 行 · 第 1/2 页
C
678 行
if(err != Z_OK) { handle_zerror(err, zstream.msg); exit(ERRX); } while( (zstream.avail_in = PR_Read(readfp, inbuf, BUFSIZ)) > 0) { zstream.next_in = inbuf; /* Process this chunk of data */ while(zstream.avail_in > 0) { err = deflate(&zstream, Z_NO_FLUSH); if(err != Z_OK) { handle_zerror(err, zstream.msg); exit(ERRX); } if(zstream.avail_out <= 0) { if( PR_Write(zipfp, outbuf, BUFSIZ) < BUFSIZ) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Writing zip data: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } zstream.next_out = outbuf; zstream.avail_out = BUFSIZ; } } } /* Now flush everything */ while(1) { err = deflate(&zstream, Z_FINISH); if(err == Z_STREAM_END) { break; } else if(err == Z_OK) { /* output buffer full, repeat */ } else { handle_zerror(err, zstream.msg); exit(ERRX); } if( PR_Write(zipfp, outbuf, BUFSIZ) < BUFSIZ) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Writing zip data: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } zstream.avail_out = BUFSIZ; zstream.next_out = outbuf; } /* If there's any output left, write it out. */ if((char*)zstream.next_out != outbuf) { if( PR_Write(zipfp, outbuf, (char*)zstream.next_out-outbuf) < (char*)zstream.next_out-outbuf) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Writing zip data: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } zstream.avail_out = BUFSIZ; zstream.next_out = outbuf; } /* Now that we know the compressed size, write this to the headers */ longtox(zstream.total_in, entry->local.orglen); longtox(zstream.total_out, entry->local.size); if(PR_Seek(zipfp, local_size_pos, PR_SEEK_SET) == -1) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Accessing zip file: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } if( PR_Write(zipfp, entry->local.size, 8) != 8) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Writing zip data: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } if(PR_Seek(zipfp, 0L, PR_SEEK_END) == -1) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Accessing zip file: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } longtox(zstream.total_in, entry->central.orglen); longtox(zstream.total_out, entry->central.size); /* Close out the deflation operation */ err = deflateEnd(&zstream); if(err != Z_OK) { handle_zerror(err, zstream.msg); exit(ERRX); } PR_Close(readfp); if((zstream.total_in > zstream.total_out) && (zstream.total_in > 0)) { deflate_percent = (int) ( (zstream.total_in-zstream.total_out)*100 / zstream.total_in ); } else { deflate_percent = 0; } if(verbosity >= 0) { PR_fprintf(outputFD, "(deflated %d%%)\n", deflate_percent); } return 0;}/******************************************************************** * J z i p C l o s e * * Finishes the ZipFile. ALSO DELETES THE ZIPFILE STRUCTURE PASSED IN!! */intJzipClose(ZIPfile *zipfile){ ZIPentry *pe, *dead; PRFileDesc *zipfp; struct ZipEnd zipend; unsigned int entrycount = 0; if(!zipfile) { return -1; } if(!zipfile->filename) { /* bogus */ return 0; } zipfp = zipfile->fp; zipfile->central_start = PR_Seek(zipfp, 0L, PR_SEEK_CUR); /* Write out all the central directories */ pe = zipfile->list; while(pe) { entrycount++; /* Write central directory info */ if( PR_Write(zipfp, &pe->central, sizeof(struct ZipCentral)) < sizeof(struct ZipCentral)) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Writing zip data: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } /* Write filename */ if( PR_Write(zipfp, pe->filename, strlen(pe->filename)) < strlen(pe->filename)) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Writing zip data: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } /* Write file comment */ if(pe->comment) { if( PR_Write(zipfp, pe->comment, strlen(pe->comment)) < strlen(pe->comment)) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Writing zip data: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } } /* Delete the structure */ dead = pe; pe = pe->next; if(dead->filename) { PORT_Free(dead->filename); } if(dead->comment) { PORT_Free(dead->comment); } PORT_Free(dead); } zipfile->central_end = PR_Seek(zipfile->fp, 0L, PR_SEEK_CUR); /* Create the ZipEnd structure */ PORT_Memset(&zipend, 0, sizeof(zipend)); longtox(ESIG, zipend.signature); inttox(entrycount, zipend.total_entries_disk); inttox(entrycount, zipend.total_entries_archive); longtox(zipfile->central_end-zipfile->central_start, zipend.central_dir_size); longtox(zipfile->central_start, zipend.offset_central_dir); if(zipfile->comment) { inttox(strlen(zipfile->comment), zipend.commentfield_len); } /* Write out ZipEnd xtructure */ if( PR_Write(zipfp, &zipend, sizeof(zipend)) < sizeof(zipend)) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Writing zip data: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } /* Write out Zipfile comment */ if(zipfile->comment) { if( PR_Write(zipfp, zipfile->comment, strlen(zipfile->comment)) < strlen(zipfile->comment)) { char *nsprErr; if(PR_GetErrorTextLength()) { nsprErr = PR_Malloc(PR_GetErrorTextLength()); PR_GetErrorText(nsprErr); } else { nsprErr = NULL; } PR_fprintf(errorFD, "Writing zip data: %s\n", nsprErr ? nsprErr : ""); if(nsprErr) PR_Free(nsprErr); errorCount++; exit(ERRX); } } PR_Close(zipfp); /* Free the memory of the zipfile structure */ if(zipfile->filename) { PORT_Free(zipfile->filename); } if(zipfile->comment) { PORT_Free(zipfile->comment); } PORT_Free(zipfile); return 0;}/********************************************** * i n t t o x * * Converts a two byte ugly endianed integer * to our platform's integer. * */static void inttox (int in, char *out){ out [0] = (in & 0xFF); out [1] = (in & 0xFF00) >> 8;}/********************************************* * l o n g t o x * * Converts a four byte ugly endianed integer * to our platform's integer. * */static void longtox (long in, char *out){ out [0] = (in & 0xFF); out [1] = (in & 0xFF00) >> 8; out [2] = (in & 0xFF0000) >> 16; out [3] = (in & 0xFF000000) >> 24;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?