Android程式解壓縮zip檔案,並載入顯示解壓後的檔案內容
剛做了個demo用於解壓縮本地zip檔案,並用webview顯示其中的一個html檔案,直接上程式碼,需要的朋友可以看看
public class ZipActivity extends Activity {
private static final String TAG = "HelloXmlActivity";
private WebView mWebView;
private static LinkedHashMap<String, String> widgetInfoMap = new LinkedHashMap<String, String>();
//http://blog.csdn.net/com360/article/details/6618086
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String zipfile = "/sdcard/abc.zip";
try {
unzip(zipfile, "/sdcard/");//yangguangfu/wujiali/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mWebView=(WebView)findViewById(R.id.web);
mWebView.loadUrl("file:///sdcard/abc/aaa.html");//此處載入解壓後的html內容
}
/*
* 這個是解壓ZIP格式檔案的方法
*
* @zipFileName:是傳進來你要解壓的檔案路徑,包括檔案的名字;
*
* @outputDirectory:選擇你要儲存的路勁;
*
*/
private void unzip(String zipFileName, String outputDirectory)
throws Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z;
String name = "";
String extractedFile = "";
int counter = 0;
while ((z = in.getNextEntry()) != null) {
name = z.getName();
Log.d(TAG, "unzipping file: " + name);
if (z.isDirectory()) {
Log.d(TAG, name + "is a folder");
// get the folder name of the widget
name = name.substring(0, name.length() - 1);
File folder = new File(outputDirectory + File.separator + name);
folder.mkdirs();
if (counter == 0) {
extractedFile = folder.toString();
}
counter++;
Log.d(TAG, "mkdir " + outputDirectory + File.separator + name);
} else {
Log.d(TAG, name + "is a normal file");
File file = new File(outputDirectory + File.separator + name);
file.createNewFile();
// get the output stream of the file
FileOutputStream out = new FileOutputStream(file);
int ch;
byte[] buffer = new byte[1024];
// read (ch) bytes into buffer
while ((ch = in.read(buffer)) != -1) {
// write (ch) byte from buffer at the position 0
out.write(buffer, 0, ch);
out.flush();
}
out.close();
}
}
in.close();
}
}
其中我的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
相關文章
- ZipArchive解壓縮zip檔案Hive
- Android 下載Zip檔案,並解壓到本地Android
- 禁止Mac Safari下載zip檔案後自動解壓縮Mac
- 使用jar與zip壓縮解壓檔案的區別JAR
- Laravel 中建立 Zip 壓縮檔案並提供下載Laravel
- aix 檔案的壓縮與解壓縮AI
- node ~ zip壓縮 && 檔案加密加密
- Java實現檔案壓縮與解壓[zip格式,gzip格式]Java
- zip壓縮檔案處理方案(Zip4j壓縮和解壓)
- 用 Huffman 樹實現檔案壓縮並解壓
- WindowsApi 解壓縮檔案WindowsAPI
- cpio檔案解壓縮
- betterzip怎麼解壓檔案?如何使用BetterZip批次解壓壓縮檔案
- .Z 檔案的解壓縮
- Unix 下批量解壓zip檔案
- java 把檔案壓縮成 zipJava
- java 生成 zip格式 壓縮檔案Java
- 檢視一個歸檔或壓縮檔案的內容而無需解壓它
- 電腦怎麼壓縮檔案 檔案壓縮方法詳解
- 常見檔案解壓和壓縮
- C# 檔案流壓縮解壓C#
- FTP自動下載並解壓縮檔案指令碼FTP指令碼
- linux下解壓.gz檔案、.cpio檔案、zip檔案Linux
- 批處理 壓縮zip 並過濾部分檔案
- 將多個檔案壓縮成zip檔案進行下載
- 檔案打包與解壓縮
- Linux下檔案的壓縮與解壓Linux
- linux 下面壓縮、解壓.rar檔案Linux
- 使用zlib庫解壓zip檔案
- Java zip解壓,並遍歷zip中的配置檔案 .cfg或.propertiesJava
- 關於解壓zip檔案的問題
- 檔案的壓縮與解壓縮zz--linuxLinux
- 分卷壓縮怎麼解壓 快速解壓電腦分卷壓縮檔案方法
- aix下解壓縮cpio檔案AI
- 壓縮檔案格式rar和zip有什麼區別 壓縮檔案格式rar和zip哪個好
- Linux中檔案的壓縮與解壓縮(轉貼)Linux
- zip解壓縮
- java 壓縮包 遍歷解壓 zip 和 7z 指定格式檔案Java