jzfile * ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, jboolean usemmap) { static char errbuf[256]; jlong len; jzfile *zip; if ((zip = allocZip(name)) == NULL) { return NULL; } #ifdef USE_MMAP zip->usemmap = usemmap; #endif zip->refs = 1; zip->lastModified = lastModified; if (zfd == -1) { if (pmsg && JVM_GetLastErrorString(errbuf, sizeof(errbuf)) > 0) *pmsg = errbuf; freeZip(zip); return NULL; } len = zip->len = IO_Lseek(zfd, 0, SEEK_END); //获取当前jar/zip文件的长度 if (len <= 0) { if (len == 0) { /* zip file is empty */ if (pmsg) { *pmsg = "zip file is empty"; } } else { /* error */ if (pmsg && JVM_GetLastErrorString(errbuf, sizeof(errbuf)) > 0) *pmsg = errbuf; } ZFILE_Close(zfd); freeZip(zip); return NULL; } zip->zfd = zfd; //当前jar/zip文件的文件描述符 if (readCEN(zip, -1) < 0) { /* An error occurred while trying to read the zip file */ if (pmsg != 0) { /* Set the zip error message */ *pmsg = zip->msg; } freeZip(zip); return NULL; } MLOCK(zfiles_lock); zip->next = zfiles; zfiles = zip; MUNLOCK(zfiles_lock); return zip; }
/* * Closes the specified zip file object. */ void JNICALL ZIP_Close(jzfile *zip) { MLOCK(zfiles_lock); if (--zip->refs > 0) { /* Still more references so just return */ MUNLOCK(zfiles_lock); return; } /* No other references so close the file and remove from list */ if (zfiles == zip) { zfiles = zfiles->next; } else { jzfile *zp; for (zp = zfiles; zp->next != 0; zp = zp->next) { if (zp->next == zip) { zp->next = zip->next; break; } } } MUNLOCK(zfiles_lock); freeZip(zip); return; }
jzfile * ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, jboolean usemmap) { char errbuf[256]; jlong len; jzfile *zip; if ((zip = allocZip(name)) == NULL) { return NULL; } #ifdef USE_MMAP zip->usemmap = usemmap; #endif zip->refs = 1; zip->lastModified = lastModified; if (zfd == -1) { if (pmsg && getLastErrorString(errbuf, sizeof(errbuf)) > 0) *pmsg = strdup(errbuf); freeZip(zip); return NULL; } // Assumption, zfd refers to start of file. Trivially, reuse errbuf. if (readFully(zfd, errbuf, 4) != -1) { // errors will be handled later zip->locsig = LOCSIG_AT(errbuf) ? JNI_TRUE : JNI_FALSE; } len = zip->len = IO_Lseek(zfd, 0, SEEK_END); if (len <= 0) { if (len == 0) { /* zip file is empty */ if (pmsg) { *pmsg = strdup("zip file is empty"); } } else { /* error */ if (pmsg && getLastErrorString(errbuf, sizeof(errbuf)) > 0) *pmsg = strdup(errbuf); } ZFILE_Close(zfd); freeZip(zip); return NULL; } zip->zfd = zfd; if (readCEN(zip, -1) < 0) { /* An error occurred while trying to read the zip file */ if (pmsg != 0) { /* Set the zip error message */ if (zip->msg != NULL) *pmsg = strdup(zip->msg); } freeZip(zip); return NULL; } MLOCK(zfiles_lock); zip->next = zfiles; zfiles = zip; MUNLOCK(zfiles_lock); return zip; }