美文网首页
day08_文件的拆分合并

day08_文件的拆分合并

作者: 闪客飞飞 | 来源:发表于2021-03-03 11:14 被阅读0次

    android 端java代码:
    public class MainActivity extends AppCompatActivity {
    private String SD_CARD_PATH;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestPermission();
    SD_CARD_PATH= Environment.getExternalStorageDirectory().getAbsolutePath();
    }
    public void requestPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
    ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            Toast.makeText(this, "申请权限", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        }
    }
    public void diff(View view) {
         String path=SD_CARD_PATH+ File.separatorChar+"video.mp4";
         String path_pattern=SD_CARD_PATH+File.separatorChar+"video_%d.mp4";
         NDKFileUtils.diff(path,path_pattern,3);
    }
    public void patch(View view) {
        String path=SD_CARD_PATH +File.separatorChar+ "video_%d.mp4";
        String merge_path = SD_CARD_PATH +File.separatorChar+ "video_merge.mp4";
        NDKFileUtils.patch(path,3,merge_path);
    }
    

    }
    native 方法:
    /**

    • @Author: CHENGYUE
    • @CreateDate: 2021/3/29:50
    • @Email: chengyue@yibaishun.cn
    • @Description: 类作用描述
      /
      public class NDKFileUtils {
      static {
      System.loadLibrary("native-lib");
      }
      /
      *
      • 拆分
      • @param path
      • @param path_pattern
      • @param count
        /
        public native static void diff(String path,String path_pattern,int count);
        /
        *
      • 合并
      • @param path
      • @param count
      • @param merge_path
        */
        public native static void patch(String path,int count,String merge_path);
        }

    C++ 代码:

    include <jni.h>

    include <string>

    include <stdio.h>

    include <stdlib.h>

    include <android/log.h>

    define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"chengyue",FORMAT,VA_ARGS)

    define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,"chengyue",FORMAT,VA_ARGS)

    extern "C" JNIEXPORT jstring JNICALL
    Java_com_example_ndkcrypt_MainActivity_stringFromJNI(
    JNIEnv* env,
    jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
    }

    long get_file_size(const char path){
    FILE fp=fopen(path,"rb");
    fseek(fp,0,SEEK_END);
    return ftell(fp);
    }
    //拆分
    extern "C" JNIEXPORT void JNICALL
    Java_com_example_ndkcrypt_NDKFileUtils_diff(JNIEnv env, jclass clazz, jstring path_str,
    jstring path_pattern_str, jint file_num) {
    //需要分割的路径
    const char
    path=env->GetStringUTFChars(path_str,NULL);
    const char
    path_pattern =env->GetStringUTFChars(path_pattern_str,NULL);
    //得到分割之后的子文件的路径列表
    char patches = (char)malloc(sizeof(char
    ) * file_num);
    int i = 0;
    for(;i<file_num;i++){
    patches[i]=(char)malloc(sizeof(char) *100);
    sprintf(patches[i],path_pattern,(i+1));
    LOGI("patch path:%s",patches[i]);
    }
    //不断读取path文件,循环写入file_num个文件中
    // 整除
    // 文件大小:90,分成9个文件,每个文件10
    // 不整除
    // 文件大小:110,分成9个文件,
    // 前(9-1)个文件为(110/(9-1))=13
    // 最后一个文件(110%(9-1))=6
    LOGI("path:%s",path);
    int filesize=get_file_size(path);
    LOGI("filesize:%d",filesize);
    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
    FILE *fpw = fopen(patches[file_num - 1], "wb");
    i = 0;
    for(; i < filesize % (file_num - 1); i++){
    fputc(fgetc(fpr),fpw);
    }
    fclose(fpw);
    }
    //关闭被分割的文件
    fclose(fpr);
    //释放
    i = 0;
    for(; i < file_num; i++){
    free(patches[i]);
    }
    free(patches);
    env->ReleaseStringUTFChars(path_str,path);
    env->ReleaseStringUTFChars(path_pattern_str,path_pattern);
    }
    extern "C" JNIEXPORT void JNICALL
    Java_com_example_ndkcrypt_NDKFileUtils_patch(JNIEnv *env, jclass clazz, jstring path_str, jint file_num,
    jstring merge_path_str) {
    //合并之后的文件
    const char *merge_path=env->GetStringUTFChars(merge_path_str,NULL);
    //分割的子文件
    const char *path_pattern=env->GetStringUTFChars(path_str,NULL);

    char **patches = (char**)malloc(sizeof(char*) * file_num);
    int i = 0;
    for (; i < file_num; i++) {
        patches[i] = (char*)malloc(sizeof(char) * 100);
        //元素赋值
        //需要分割的文件:C://jason/liuyan.png
        //子文件:C://jason/liuyan_%d.png
        sprintf(patches[i], path_pattern, (i+1));
        LOGI("patch path:%s",patches[i]);
    }
    
    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);
    LOGI("merge path:%s",merge_path);
    //释放
    i = 0;
    for(; i < file_num; i++){
        free(patches[i]);
    }
    free(patches);
    env->ReleaseStringUTFChars(path_str,path_pattern);
    env->ReleaseStringUTFChars(merge_path_str,merge_path);
    

    }

    相关文章

      网友评论

          本文标题:day08_文件的拆分合并

          本文链接:https://www.haomeiwen.com/subject/abkqqltx.html