Is there a Java equivalent to GetCompressedFileSize (C ++)?

I want to get accurate data (IE not disk size / all 0 size) measurements of sparse files in Java. In C ++ one can use GetCompressedFileSize

. I haven't figured out yet how can this be done in Java?

If there is no direct equivalent, how would I measure the data in a sparse file, as opposed to a size containing all zeros?

To clarify, I'm looking for this to do spare file measurements on both Linux O / S and windows, however I don't mind coding two separate applications!

Thank,

+3


source to share


2 answers


If you only do this on Windows, you can write it with Java Native Interface

class NativeInterface{
   public static native long GetCompressedFileSize(String filename);
}

      



and in the C / C ++ file:

extern "C"
JNIEXPORT jlong JNICALL Java_NativeInterface_GetCompressedFileSize
  (JNIEnv *env, jobject obj, jstring javaString)
{
    const char *nativeString = env->GetStringUTFChars(javaString, 0);

    char buffer[512];
    strcpy(buffer, nativeString);
    env->ReleaseStringUTFChars(javaString, nativeString);
    return (jlong) GetCompressedFileSize(buffer, NULL);
}

      

+1


source


Since the answer was given for windows. I'll try to provide Linux.

I'm not sure, but I think this will do the trick (C ++):



#include <linux/fs.h>
ioctl(file, BLKGETSIZE64, &file_size_in_bytes);

      

This can be loaded in the same way as described in @ Aniket's answer (JNI)

+1


source







All Articles