Example #1
0
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;
}
Example #2
0
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;
}