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();
}
}