1、声明本地方法
public native void diff(String path,String path_pattern,int count);
public native void patch(String path,int count,String path_pattern);
2、生成头文件
JNIEXPORT void JNICALL Java_com_dongnaoedu_jni_JniTest_diff
(JNIEnv *, jclass, jstring, jstring,jint);
JNIEXPORT void JNICALL Java_com_dongnaoedu_jni_JniTest_patch
(JNIEnv *, jclass, jstring, jint, jstring);
3、实现声明的方法
JNIEXPORT void JNICALL Java_com_dongnaoedu_jni_JniTest_diff
(JNIEnv *env, jclass jcls, jstring path_jstr, jstring path_pattern_jstr, jint file_num)
{
//需要分割的文件路径
const char *path = (*env)->GetStringUTFChars(env, path_jstr,NULL);
const char *path_pattern = (*env)->GetStringUTFChars(env, path_pattern_jstr, NULL);
//得到分割之后的文件的路径列表
char **patches = malloc(sizeof(char*) *file_num);
int i = 0;
for (; i < file_num; i++){
patches[i] = malloc(sizeof(char)* 100);
//元素赋值
//C://jason/ly.png
//子文件 C://jason/ly_%d.png
sprintf(patches[i], path_pattern, (i + 1));
printf("patch path:%s \n",patches[i]);
}
//不断读取path文件,循环写入file_num个文件中
//整除
//文件大小:90,分成9个文件,每个文件10
//不整除
//文件大小:110,分成9个文件
//前(9-1)个文件为(110/(9-1))=13
//最后一个文件(110%(9-1))=6
int filesize = get_file_size(path);
FILE *fpr = fopen(path, "rb");
//整除
if (filesize%file_num == 0){
//单个文件大小
int part = filesize / file_num;
i = 0;
//逐一写入不同分割子文件中
for (; i < file_num; i++){
FILE *fpw = fopen(patches[i], "wb");
int j = 0;
for (; j < part; j++){
//边读边写
fputc(fgetc(fpr), fpw);
}
fclose(fpw);
}
}
else
{
//单个文件大小
int part = filesize / (file_num -1);
i = 0;
//逐一写入不同分割子文件中
for (; i < file_num-1; i++){
FILE *fpw = fopen(patches[i], "wb");
int j = 0;
for (; j < part; j++){
//边读边写
fputc(fgetc(fpr), fpw);
}
fclose(fpw);
}
//the last one
int last_part = filesize - (file_num - 1)*part;
FILE *fpw = fopen(patches[file_num-1], "wb");
i = 0;
for (; i < last_part; i++){
fputc(fgetc(fpr), fpw);
fclose(fpw);
}
}
//关闭被分割的总文件
fclose(fpr);
//释放内存
int j = 0;
for (; j < file_num; j++){
free(patches[j]);
}
free(patches);
(*env)->ReleaseStringUTFChars(env, path_jstr, path);
(*env)->ReleaseStringUTFChars(env, path_pattern_jstr, path_pattern);
}
//合并
JNIEXPORT void JNICALL Java_com_dongnaoedu_jni_JniTest_patch
(JNIEnv *env, jclass jcls, jstring path_pattern_jstr, jint file_num, jstring merge_path_jstr)
{
//合并之后的文件
const char* merge_path = (*env)->GetStringUTFChars(env, merge_path_jstr, NULL);
const char* path_pattern = (*env)->GetStringUTFChars(env, path_pattern_jstr, NULL);
//得到分割之后的文件的路径列表
char **patches = malloc(sizeof(char*)*file_num);
int i = 0;
for (; i < file_num; i++){
printf("diff:%d \n", 2);
patches[i] = malloc(sizeof(char)* 100);
//元素赋值
//C://jason/ly.png
//子文件 C://jason/ly_%d.png
sprintf(patches[i], path_pattern, (i + 1));
}
FILE *fpw = fopen(merge_path, "wb");
//把所有的分割文件读取以便,写入一个总的文件中
i = 0;
for (; i < file_num; i++){
//每个子文件的大小
int filesize = get_file_size(patches[i]);
FILE *fpr = fopen(patches[i], "rb");
int j = 0;
for (; j < filesize; j++){
fputc(fgetc(fpr), fpw);
}
fclose(fpr);
}
fclose(fpw);
(*env)->ReleaseStringUTFChars(env, merge_path_jstr, merge_path);
(*env)->ReleaseStringUTFChars(env, path_pattern_jstr, path_pattern);
}
4、加载生成的.dll文件,并调用
(1)调用文件拆分的方法
String path = "E:\\message.mp4";
String path_pattern = "E:\\message%d.mp4";
t.diff(path, path_pattern,3);
运行后将message拆分成了message1、message2、message3三个文件
(2)合并后生成了目标文件dest_message
String path_pattern = "E:\\message%d.mp4";
String dest_path = "E:\\dest_message.mp4";
t.patch(path_pattern,3,dest_path);