2011年4月14日星期四

使用 java.util.zip 解壓 ZIP 壓縮檔案

Android API 提供 java.util.zip 包, 可以用來解壓 ZIP 壓縮檔案.

範例:
本程序解壓縮指定的 ZIP 壓縮檔案(/sdcard/test/ziptest.zip), 並儲存於指定的文件夾(/sdcard/test/).

package com.AndroidUnzip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidUnzip extends Activity {

TextView textMsg;
static final int BUFFER_SIZE = 4096;
String srcPath = "/sdcard/test/ziptest.zip";
String destPath = "/sdcard/test/";

String msg = "";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textMsg = (TextView)findViewById(R.id.msg);
unZip();
}

private void appendMsg(String tMsg){
msg += tMsg + "\n";
textMsg.setText(msg);
}

private void unZip(){
BufferedOutputStream bufferedOutputStream = null;
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(srcPath);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;


while ((zipEntry = zipInputStream.getNextEntry()) != null){

appendMsg(" ----- ");
appendMsg("Zip Entry: " + zipEntry.toString());

String zipEntryName = zipEntry.getName();
appendMsg(destPath + zipEntryName);

File file = new File(destPath + zipEntryName);

if (file.exists()){
appendMsg("Already exist! Skip");
} else {
if(zipEntry.isDirectory()){
appendMsg("make dir: " + file.getPath());
file.mkdirs();
}else{
appendMsg("unzip: " + file.getPath());

byte buffer[] = new byte[BUFFER_SIZE];
FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);

int count;
while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
bufferedOutputStream.write(buffer, 0, count);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
}

zipInputStream.close();
appendMsg(" ------------ ");
appendMsg(" - Finished - ");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
appendMsg(e.toString());
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
appendMsg(e.toString());
}
}
}


main.xml 佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


因本程序需要把解壓後的文件寫回到 SD Card, 所以須修改 AndroidManifest.xml 授予"android.permission.WRITE_EXTERNAL_STORAGE"權限.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidUnzip"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidUnzip"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

1 則留言:

  1. 請問一下 我解壓縮出來是資料夾 到ddms從sdcard把檔案取出到電腦不是資料夾 要如何解壓出來是資料夾呢??
    android資料夾有副檔名嗎??

    回覆刪除