JNIEXPORT jint JNICALL Java_java_util_zip_ZipFile_openZipImpl (JNIEnv * env, jobject recv, jbyteArray zipName) { VMI_ACCESS_FROM_ENV (env); PORT_ACCESS_FROM_ENV (env); I_32 retval; JCLZipFile *jclZipFile; JCLZipFileLink *zipfileHandles; jsize length; char pathCopy[HyMaxPath]; HyZipCachePool *zipCachePool; jclZipFile = jclmem_allocate_memory (env, sizeof (*jclZipFile)); if (!jclZipFile) return 3; length = (*env)->GetArrayLength (env, zipName); length = length < HyMaxPath - 1 ? length : HyMaxPath - 1; ((*env)->GetByteArrayRegion (env, zipName, 0, length, pathCopy)); pathCopy[length++] = '\0'; ioh_convertToPlatform (pathCopy); /* Open the zip file (caching will be managed automatically by zipsup) */ zipCachePool = (*VMI)->GetZipCachePool (VMI); retval = zip_openZipFile (privatePortLibrary, pathCopy, &(jclZipFile->hyZipFile), zipCachePool); if (retval) { jclmem_free_memory (env, jclZipFile); /* free on fail */ if (retval == ZIP_ERR_FILE_OPEN_ERROR) return 1; else return 2; } /* Add the zipFile we just allocated to the list of zip files -- we will * free this on UnLoad if its not already free'd. */ zipfileHandles = JCL_CACHE_GET (env, zipfile_handles); jclZipFile->last = (JCLZipFile *) zipfileHandles; jclZipFile->next = zipfileHandles->next; if (zipfileHandles->next != NULL) zipfileHandles->next->last = jclZipFile; zipfileHandles->next = jclZipFile; (*env)->SetLongField (env, recv, JCL_CACHE_GET (env, FID_java_util_zip_ZipFile_descriptor), ((IDATA) jclZipFile)); return 0; }
/** * This DLL is being unloaded, do any clean up required. * This may be called more than once!! */ JNIEXPORT void JNICALL JNI_OnUnload (JavaVM * vm, void *reserved) { JNIEnv *env; void *keyInitCountPtr = GLOBAL_DATA (keyInitCount); void **jclIdCache = GLOBAL_DATA (JCL_ID_CACHE); if ((*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_2) == JNI_OK) { JniIDCache *idCache = (JniIDCache *) HY_VMLS_GET (env, *jclIdCache); if (idCache) { JCLZipFileLink *zipfileHandles; JCLZipFile *jclZipFile; PORT_ACCESS_FROM_ENV (env); #ifdef HY_ZIP_API VMI_ACCESS_FROM_ENV(env); VMIZipFunctionTable *zipFuncs = (*VMI)->GetZipFunctions(VMI); #endif /* HY_ZIP_API */ /* Detach from the common library */ ClearLibDetach (env); /* Close and free the HyZipFile handles */ zipfileHandles = JCL_CACHE_GET (env, zipfile_handles); if (zipfileHandles != NULL) { jclZipFile = zipfileHandles->next; while (jclZipFile != NULL) { JCLZipFile *next = jclZipFile->next; #ifndef HY_ZIP_API zip_closeZipFile (PORTLIB, &jclZipFile->hyZipFile); #else /* HY_ZIP_API */ zipFuncs->zip_closeZipFile (VMI, &jclZipFile->hyZipFile); #endif /* HY_ZIP_API */ jclmem_free_memory (env, jclZipFile); jclZipFile = next; } MUTEX_DESTROY (zipfileHandles->mutex); jclmem_free_memory (env, zipfileHandles); } /* Free any global references */ freeReferences (env); /* Free VMLS keys */ idCache = (JniIDCache *) HY_VMLS_GET (env, *jclIdCache); HY_VMLS_FNTBL (env)->HYVMLSFreeKeys (env, keyInitCountPtr, jclIdCache, NULL); hymem_free_memory (idCache); } } }
/* Create a new stream . This stream cannot be used until it has been properly initialized. */ JNIEXPORT jlong JNICALL Java_java_util_zip_Deflater_createStream (JNIEnv * env, jobject recv, jint level, jint strategy, jboolean noHeader) { PORT_ACCESS_FROM_ENV (env); JCLZipStream *jstream; z_stream *stream; int err = 0; int wbits = 15; /*Use MAX for fastest */ #ifdef HY_ZIP_API VMI_ACCESS_FROM_ENV (env); VMIZipFunctionTable *zipFuncs; zipFuncs = (*VMI)->GetZipFunctions(VMI); #endif /*Allocate mem for wrapped struct */ jstream = jclmem_allocate_memory (env, sizeof (JCLZipStream)); if (jstream == NULL) { throwNewOutOfMemoryError (env, ""); return -1; } /*Allocate the z_stream */ stream = jclmem_allocate_memory (env, sizeof (z_stream)); if (stream == NULL) { jclmem_free_memory (env, jstream); throwNewOutOfMemoryError (env, ""); return -1; } stream->opaque = (void *) privatePortLibrary; stream->zalloc = zalloc; stream->zfree = zfree; jstream->stream = stream; jstream->dict = NULL; jstream->inaddr = NULL; /*Unable to find official doc that this is the way to avoid zlib header use. However doc in zipsup.c claims it is so */ if (noHeader) wbits = wbits / -1; err = deflateInit2 (stream, level, Z_DEFLATED, /*Only supported ZLIB method */ wbits, /*Window bits to use. 15 is fastest but consumes the most memory */ 9, /*Memory allocation for internal compression state. 9 uses the most. */ strategy); if (err != Z_OK) { THROW_ZIP_EXCEPTION(env, err, IllegalArgumentException); return -1; } return (jlong) ((IDATA) jstream); }
createZipEntry (JNIEnv * env, VMIZipFile * zipFile, VMIZipEntry * zipEntry) #endif { PORT_ACCESS_FROM_ENV (env); #ifdef HY_ZIP_API VMI_ACCESS_FROM_ENV(env); #endif /* HY_ZIP_API */ jclass javaClass; jobject java_ZipEntry, extra, entryName; jmethodID mid; #ifdef HY_ZIP_API VMIZipFunctionTable *zipFuncs = (*VMI)->GetZipFunctions(VMI); #endif /* HY_ZIP_API */ /* Build a new ZipEntry from the C struct */ entryName = ((*env)->NewStringUTF (env, (const char*)zipEntry->filename)); if (((*env)->ExceptionCheck (env))) return NULL; extra = NULL; if (zipEntry->extraFieldLength > 0) { #ifndef HY_ZIP_API zip_getZipEntryExtraField (PORTLIB, zipFile, zipEntry, NULL, #else /* HY_ZIP_API */ zipFuncs->zip_getZipEntryExtraField (VMI, zipFile, zipEntry, NULL, #endif /* HY_ZIP_API */ zipEntry->extraFieldLength); if (zipEntry->extraField == NULL) return NULL; extra = ((*env)->NewByteArray (env, zipEntry->extraFieldLength)); if (((*env)->ExceptionCheck (env))) return NULL; ((*env)-> SetByteArrayRegion (env, extra, 0, zipEntry->extraFieldLength, (jbyte*)zipEntry->extraField)); jclmem_free_memory (env, zipEntry->extraField); zipEntry->extraField = NULL; }