Android无需解压直接读取Zip文件和文件内容

package com.example.readzip;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class ReadFileContent extends Activity implements OnClickListener{


private Button mButton;
private static String TAG = "ReadFileContent";
public static Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.filecontent);
mButton = (Button)findViewById(R.id.startRead);
mButton.setOnClickListener(this);
mContext = getApplicationContext();
}

public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.startRead:
try {
readZipFile("/storage/sdcard0/test.zip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;


default:
break;
}
}


/**
* 无需解压直接读取Zip文件和文件内容
* @param file
* @throws Exception
*/
public static void readZipFile(String file) throws Exception {  
        ZipFile zf = new ZipFile(file);
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {  
            if (ze.isDirectory()) {
            //Do nothing
            } else {
            Log.e(TAG,"file - " + ze.getName() + " : " + ze.getSize() + " bytes");
                if (ze.getName().equals("sbl1.mbn")) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(zf.getInputStream(ze)));
                    String line;
                    while ((line = br.readLine()) != null) {  
                    Log.e(TAG, line);
                    if (line.contains("OK")) {
                    Toast.makeText(mContext, "OK", Toast.LENGTH_SHORT).show();
                    }
                    }  
                    br.close();
                }  
                System.out.println();
            }  
        }  
        zin.closeEntry();  

    }

}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LOVE000520/article/details/52993782
文章标签: 读取zip
个人分类: Android-Java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭