//UnZip.java package com.hl; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnZip { private boolean staus=false; public UnZip(String file){ String unzipfile = file; //解壓縮的檔案名包含路徑 try { File olddirec = new File(unzipfile); //解壓縮的檔路徑(為了獲取路徑) ZipInputStream zin = new ZipInputStream(new FileInputStream(unzipfile)); ZipEntry entry; //創建文件夾 while ( (entry = zin.getNextEntry()) != null){ if (entry.isDirectory()) { File directory = new File(olddirec.getParent(), entry.getName()); if (!directory.exists()) if (!directory.mkdirs()) System.exit(0); zin.closeEntry(); } if (!entry.isDirectory()) { File myFile = new File(entry.getName()); //輸出路徑 String ofile=file.replace(".zip", ""); File fo=new File(ofile); if(!fo.exists()){ fo.mkdir(); } FileOutputStream fout = new FileOutputStream(ofile+"/"+myFile.getPath()); DataOutputStream dout = new DataOutputStream(fout); byte[] b = new byte[1024]; int len = 0; while ( (len = zin.read(b)) != -1) { dout.write(b, 0, len); } dout.close(); fout.close(); zin.closeEntry(); staus=true; } } } catch (IOException e) { e.printStackTrace(); } } public boolean getStaus(){ return staus; } }
2013年1月2日 星期三
Android Unzip code
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言