package com.ad.zip;

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.os.Environment;

public class FileUtil {
public static final String SDCARD_PATH = Environment
.getExternalStorageDirectory() + "/";
public static final String CACHE_PATH_ROOT = SDCARD_PATH + "MBACache/images.zip";
public static final String SHUCHU_ROOT = SDCARD_PATH + "MBACache";


public static void zipMethod() {
long startTime=System.currentTimeMillis();
try {
ZipInputStream Zin=new ZipInputStream(new FileInputStream(CACHE_PATH_ROOT));//輸入源zip路徑
BufferedInputStream Bin=new BufferedInputStream(Zin);
String Parent=SHUCHU_ROOT; 輸出路徑(資料夾目錄)
File Fout=null;
ZipEntry entry;
try {
entry:images/menu_center.png entry.isDirectory():false
fout:/mnt/sdcard/MBACache/images/menu_center.png
Fout.getParent():/mnt/sdcard/MBACache/images

while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){
Fout=new File(Parent,entry.getName());
if(!Fout.exists()){
(new File(Fout.getParent())).mkdirs();
}
FileOutputStream out=new FileOutputStream(Fout);
BufferedOutputStream Bout=new BufferedOutputStream(out);
int b;
while((b=Bin.read())!=-1){
Bout.write(b);
}
Bout.close();
out.close();
System.out.println(Fout+"解壓成功");
}
Bin.close();
Zin.close();
} catch (IOException e) {
System.out.println("e1---->"+e);
}
} catch (FileNotFoundException e) {
System.out.println("e2---->"+e);
}
long endTime=System.currentTimeMillis();
System.out.println("耗費時間: "+(endTime-startTime)+" ms");
}
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()