Android程序解压缩zip文件并加载显示解压后的文件内容


刚做了个demo用于解压缩本地zip文件,并用webview显示其中的一个html文件,直接上代码,需要的朋友可以看看

  1. public class ZipActivity extends Activity {  
  2.     private static final String TAG = "HelloXmlActivity";  
  3.     private WebView mWebView;    
  4.   
  5.     private static LinkedHashMap<String, String> widgetInfoMap = new LinkedHashMap<String, String>();  
  6.   
  7.     //http://blog.csdn.net/com360/article/details/6618086   
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.         String zipfile = "/sdcard/abc.zip";  
  14.         try {  
  15.             unzip(zipfile, "/sdcard/");//yangguangfu/wujiali/   
  16.               
  17.               
  18.         } catch (Exception e) {  
  19.             // TODO Auto-generated catch block   
  20.             e.printStackTrace();  
  21.               
  22.         }  
  23.           
  24.         mWebView=(WebView)findViewById(R.id.web);  
  25.         mWebView.loadUrl("file:///sdcard/abc/aaa.html");//此处加载解压后的html内容  
  26.   
  27.     }  
  28.   
  29.     /* 
  30.      * 这个是解压ZIP格式文件的方法 
  31.      *  
  32.      * @zipFileName:是传进来你要解压的文件路径,包括文件的名字; 
  33.      *  
  34.      * @outputDirectory:选择你要保存的路劲; 
  35.      *  
  36.      */  
  37.     private void unzip(String zipFileName, String outputDirectory)  
  38.             throws Exception {  
  39.         ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));  
  40.         ZipEntry z;  
  41.         String name = "";  
  42.         String extractedFile = "";  
  43.         int counter = 0;  
  44.   
  45.         while ((z = in.getNextEntry()) != null) {  
  46.             name = z.getName();  
  47.             Log.d(TAG, "unzipping file: " + name);  
  48.             if (z.isDirectory()) {  
  49.                 Log.d(TAG, name + "is a folder");  
  50.                 // get the folder name of the widget   
  51.                 name = name.substring(0, name.length() - 1);  
  52.                 File folder = new File(outputDirectory + File.separator + name);  
  53.                 folder.mkdirs();  
  54.                 if (counter == 0) {  
  55.                     extractedFile = folder.toString();  
  56.                 }  
  57.                 counter++;  
  58.                 Log.d(TAG, "mkdir " + outputDirectory + File.separator + name);  
  59.             } else {  
  60.                 Log.d(TAG, name + "is a normal file");  
  61.                 File file = new File(outputDirectory + File.separator + name);  
  62.                 file.createNewFile();  
  63.                 // get the output stream of the file   
  64.                 FileOutputStream out = new FileOutputStream(file);  
  65.                 int ch;  
  66.                 byte[] buffer = new byte[1024];  
  67.                 // read (ch) bytes into buffer   
  68.                 while ((ch = in.read(buffer)) != -1) {  
  69.                     // write (ch) byte from buffer at the position 0   
  70.                     out.write(buffer, 0, ch);  
  71.                     out.flush();  
  72.                 }  
  73.                 out.close();  
  74.             }  
  75.         }  
  76.   
  77.         in.close();  
  78.   
  79.     }  
  80.   
  81.       
  82. }  

其中我的abc.zip文件是放在sdcard中的,里面有2个文件,解压后生成一个abc文件夹,文件夹下是解压缩后的2个文件,我用一个webview直接指定加载了解压后的一个html文件,做的比较粗糙,省去了文件存在判断,扫描文件名、文件类型,main.xml文件也很简单,通过上面代码应该可以看出其中的控件,这里不再写xml布局文件了。

更多信息可参考下面文章:

加载html与js:

解压缩zip文件:

相关内容