JNIEXPORT jlong JNICALL Java_java_util_zip_ZipFile_open(JNIEnv *env, jclass cls, jstring name, jint mode) { const char *path = JNU_GetStringPlatformChars(env, name, 0); jlong result = 0; int flag = 0; if (mode & OPEN_READ) flag |= O_RDONLY; if (mode & OPEN_DELETE) flag |= JVM_O_DELETE; if (path != 0) { char *msg; jzfile *zip = ZIP_Open_Generic(path, &msg, flag); JNU_ReleaseStringPlatformChars(env, name, path); if (zip != 0) { result = ptr_to_jlong(zip); } else if (msg != 0) { ThrowZipException(env, msg); } else if (errno == ENOMEM) { JNU_ThrowOutOfMemoryError(env, 0); } else { ThrowZipException(env, "error in opening zip file"); } } return result; }
JNIEXPORT jlong JNICALL Java_java_util_zip_ZipFile_open(JNIEnv *env, jclass cls, jstring name, jint mode, jlong lastModified, jboolean usemmap) { const char *path = JNU_GetStringPlatformChars(env, name, 0); char *msg = 0; jlong result = 0; int flag = 0; jzfile *zip = 0; if (mode & OPEN_READ) flag |= O_RDONLY; if (mode & OPEN_DELETE) flag |= JVM_O_DELETE; if (path != 0) { zip = ZIP_Get_From_Cache(path, &msg, lastModified); if (zip == 0 && msg == 0) { ZFILE zfd = 0; #ifdef WIN32 zfd = winFileHandleOpen(env, name, flag); if (zfd == -1) { /* Exception already pending. */ goto finally; } #else zfd = JVM_Open(path, flag, 0); if (zfd < 0) { throwFileNotFoundException(env, name); goto finally; } #endif zip = ZIP_Put_In_Cache0(path, zfd, &msg, lastModified, usemmap); } if (zip != 0) { result = ptr_to_jlong(zip); } else if (msg != 0) { ThrowZipException(env, msg); free(msg); } else if (errno == ENOMEM) { JNU_ThrowOutOfMemoryError(env, 0); } else { ThrowZipException(env, "error in opening zip file"); } finally: JNU_ReleaseStringPlatformChars(env, name, path); } return result; }
JNIEXPORT jint JNICALL Java_java_util_zip_ZipFile_read(JNIEnv *env, jclass cls, jlong zfile, jlong zentry, jint pos, jbyteArray bytes, jint off, jint len) { #define BUFSIZE 8192 jzfile *zip = jlong_to_ptr(zfile); jbyte buf[BUFSIZE]; char *msg; if (len > BUFSIZE) { len = BUFSIZE; } ZIP_Lock(zip); len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf, len); msg = zip->msg; ZIP_Unlock(zip); if (len == -1) { if (msg != 0) { ThrowZipException(env, msg); } else { JNU_ThrowIOException(env, strerror(errno)); } } else { (*env)->SetByteArrayRegion(env, bytes, off, len, buf); } return len; }
JNIEXPORT jint JNICALL Java_java_util_zip_ZipFile_read(JNIEnv *env, jclass cls, jlong zfile, jlong zentry, jlong pos, jbyteArray bytes, jint off, jint len) { jzfile *zip = jlong_to_ptr(zfile); char *msg; #define BUFSIZE 8192 /* copy via tmp stack buffer: */ jbyte buf[BUFSIZE]; if (len > BUFSIZE) { len = BUFSIZE; } ZIP_Lock(zip); len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf, len); msg = zip->msg; ZIP_Unlock(zip); if (len != -1) { (*env)->SetByteArrayRegion(env, bytes, off, len, buf); } if (len == -1) { if (msg != 0) { ThrowZipException(env, msg); } else { char errmsg[128]; sprintf(errmsg, "errno: %d, error: %s\n", errno, "Error reading ZIP file"); JNU_ThrowIOExceptionWithLastError(env, errmsg); } } return len; }