? ? C语言使用LZMA SDK解压非加密7z文件介绍:http://hzy3774.iteye.com/blog/2104510
?github项目地址:https://github.com/hzy3774/AndroidUn7zip
把前文中相应源文件和头文件复制到jni目录下:
?
写java接口源文件:
package com.hu.andun7z;import java.io.File;public class AndUn7z { public static boolean extract7z(String filePath, String outPath) { File outDir = new File(outPath); if(!outDir.exists() || !outDir.isDirectory()) { outDir.mkdirs(); } return (AndUn7z.un7zip(filePath, outPath) == 1); } //JNI interface private static native int un7zip(String filePath, String outPath); static { System.loadLibrary("un7z"); }}
?
在项目中只要调用extract7z(String filePath, String outPath)就可以实现解压了
javah -jni 生成对应的c++接口文件,填充函数:
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_hu_andun7z_AndUn7z */#ifndef _Included_com_hu_andun7z_AndUn7z#define _Included_com_hu_andun7z_AndUn7z#ifdef __cplusplusextern "C" {#endif#include "JniWrapper.h"/* * Class: com_hu_andun7z_AndUn7z * Method: un7zip * Signature: (Ljava/lang/String;Ljava/lang/String;)I */JNIEXPORT jint JNICALL Java_com_hu_andun7z_AndUn7z_un7zip(JNIEnv *env, jclass thiz, jstring filePath, jstring outPath){ const char* cfilePath = (const char*)env->GetStringUTFChars(filePath, NULL); const char* coutPath = (const char*)env->GetStringUTFChars(outPath, NULL); LOGD("start extract filePath[%s], outPath[%s]", cfilePath, coutPath); jint ret = extract7z(cfilePath, coutPath); LOGD("end extract"); env->ReleaseStringUTFChars(filePath, cfilePath); env->ReleaseStringUTFChars(outPath, coutPath); return ret;}#ifdef __cplusplus}#endif#endif
?
写NDK相关的头文件JniWrapper.h
/* * JniWrapper.h * * Created on: 2014-8-12 * Author: HZY */#ifndef JNIWRAPPER_H_#define JNIWRAPPER_H_#include <jni.h>#include <android/log.h>#include "src/Types.h"#define LOG_TAG "jniLog"#undef LOG#ifdef DEBUG#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)#else#define LOGD(...) do{}while(0)#endif#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,LOG_TAG,__VA_ARGS__)int extract7z(const char* srcFile, const char* dstPath);#endif /* JNIWRAPPER_H_ */
?
接口最终调用上文中介绍的函数:int MY_CDECL extract7z(const char* srcFile, const char* dstPath);
打印的输出改到Logcat中输出。
?文件成功解压
?压缩文件中的路径和输出路径不能存在中文,不然文件输出不了。
?