`
hzy3774
  • 浏览: 984805 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

Android 使用NDK (JNI)简单解压7z压缩文件

阅读更多

    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 __cplusplus
extern "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中输出。



 文件成功解压



 压缩文件中的路径和输出路径不能存在中文,不然文件输出不了。

 

  • 大小: 55.5 KB
  • 大小: 39.7 KB
  • 大小: 18.9 KB
分享到:
评论
6 楼 hzy3774 2014-09-01  
cx2003198 写道
hzy3774 写道
cx2003198 写道
hzy3774 写道
后缀没多大关系吧,只要压缩方式是lzma

恩,
我看你用的库是920版本之前的,版本选择上有什么特别的地方么?求传授经验

使用的是465版本,缺点是不支持压缩文件里存在中文路径和文件,920应该能避免编码问题,等我再研究一下


我试了下 920可以移植到android正常解压


github上换成920版了,可以解压中文的了
5 楼 cx2003198 2014-09-01  
hzy3774 写道
cx2003198 写道
hzy3774 写道
后缀没多大关系吧,只要压缩方式是lzma

恩,
我看你用的库是920版本之前的,版本选择上有什么特别的地方么?求传授经验

使用的是465版本,缺点是不支持压缩文件里存在中文路径和文件,920应该能避免编码问题,等我再研究一下


我试了下 920可以移植到android正常解压
4 楼 hzy3774 2014-08-29  
cx2003198 写道
hzy3774 写道
后缀没多大关系吧,只要压缩方式是lzma

恩,
我看你用的库是920版本之前的,版本选择上有什么特别的地方么?求传授经验

使用的是465版本,缺点是不支持压缩文件里存在中文路径和文件,920应该能避免编码问题,等我再研究一下
3 楼 cx2003198 2014-08-28  
hzy3774 写道
后缀没多大关系吧,只要压缩方式是lzma

恩,
我看你用的库是920版本之前的,版本选择上有什么特别的地方么?求传授经验
2 楼 hzy3774 2014-08-28  
后缀没多大关系吧,只要压缩方式是lzma
1 楼 cx2003198 2014-08-28  
下载参考一下~顺便问一下楼主能解压lzma后缀的文件么?

相关推荐

Global site tag (gtag.js) - Google Analytics