小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Android程序解壓縮zip文件,并加載顯示解壓后的文件內(nèi)容

 WindySky 2016-03-07

剛做了個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內(nèi)容  
  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:

http://blog.csdn.net/com360/article/details/6618086

解壓縮zip文件

http://www.oschina.net/code/snippet_4873_4142





    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約