android 实现zip文件的解压

来源:互联网 发布:手机网络攻击软件 编辑:程序博客网 时间:2024/05/18 01:18



最近遇到一个项目,需要再android里面存放比较大的数据,数据量大概有20+w条,通过sqlite来存放数据,并且存放再raw文件夹中,第一次加载程序的时候,将文件拷贝出来使用,有因为raw里面只能存放1m的文件,所以需要把数据先进行压缩。闲话少说,贴上实现方式。


先将数据导入sqlite中,然后再sqlite数据库进行打包,注意打包成zip格式。

然后将文件存放在raw中,程序第一次加载时,将文件进行解包。我是用异步来实现。代码如下。

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;


import android.app.ProgressDialog;

import android.content.Context;

import android.os.AsyncTask;


/**

 * 解压数据库文件操作类

 * 

 * @author administrator

 * 

 */

publicclass unZipFile extends AsyncTask<String, Integer, Integer> {


private Context_conContext = null;

private ProgressDialog_progress = null;

privateint _StreamLength = 0;

private String_SavePath = "";// 数据库存放路径

privateint _ResourcesIndex = 0;// 资源文件索引


/**

* 解压zip数据库文件

*@param context

*            activity

*@param SavePath

*            保存路径

*@param ResourcesIndex

*            资源文件索引

*/

public unZipFile(Context context, String SavePath,int ResourcesIndex) {

_conContext = context;

_SavePath = SavePath;

_ResourcesIndex = ResourcesIndex;

}


@Override

protected Integer doInBackground(String... params) {


int byteLength = 4096;

String EntryFileName;// zip原文件名

try {

BufferedOutputStream bufferStream =null; // 缓冲输出流

InputStream ZipIs =_conContext.getResources().openRawResource(

_ResourcesIndex);// 获取文件流

ZipInputStream zis =new ZipInputStream(new BufferedInputStream(

ZipIs));

_StreamLength = zis.available();// 获取文件大小

ZipEntry entry;// 每个zip条目的实例

while ((entry = zis.getNextEntry()) !=null) {

try {

int count;

byte data[] =new byte[byteLength];

EntryFileName = entry.getName();

File SaveFile =new File(_SavePath +"/" + EntryFileName);

File SaveDir =new File(SaveFile.getParent());

if (!SaveDir.exists()) {

SaveDir.mkdirs();

}

FileOutputStream fos =new FileOutputStream(SaveFile);

bufferStream =new BufferedOutputStream(fos, byteLength);

int progress = 0;

while ((count = zis.read(data, 0, byteLength)) != -1) {

bufferStream.write(data, 0, count);

progress += byteLength;

publishProgress(progress);// 更新进度条

}

bufferStream.flush();

bufferStream.close();

}catch (Exception ex) {

ex.printStackTrace();

}

}

zis.close();

}catch (Exception ex) {

ex.printStackTrace();

}

return_StreamLength;

}


@Override

protectedvoid onPostExecute(Integer result) {

super.onPostExecute(result);

_progress.setProgress(result);

if (_progress !=null)

_progress.dismiss();

_progress.cancel();

}


@Override

protectedvoid onPreExecute() {

super.onPreExecute();

_progress =new ProgressDialog(_conContext);

_progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

_progress.setTitle("文件解压");

_progress.setMessage("正在文件解压,请稍后...");

_progress.show();

}


@Override

protectedvoid onProgressUpdate(Integer... values) {

super.onProgressUpdate(values);

/**

* 更改进度条状态

*/

if (_StreamLength != _progress.getMax())

_progress.setMax(_StreamLength);

_progress.setProgress(values[0]);

}


}