Android解壓文件包:java
import android.content.Context; import android.os.Environment; import org.json.JSONArray; import org.json.JSONObject; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; /** * 此工具類可提供壓縮與解壓縮功能 * */ public class ZipTool { @SuppressWarnings("unchecked") public static void unzip(String zipFileName, String outputDirectory) throws IOException { ZipFile zipFile = null; try { zipFile = new ZipFile(zipFileName); Enumeration e = zipFile.entries(); ZipEntry zipEntry = null; File dest = new File(outputDirectory); dest.mkdirs(); while (e.hasMoreElements()) { zipEntry = (ZipEntry) e.nextElement(); String entryName = zipEntry.getName(); InputStream in = null; FileOutputStream out = null; try { if (zipEntry.isDirectory()) { String name = zipEntry.getName(); name = name.substring(0, name.length() - 1); File f = new File(outputDirectory + File.separator + name); f.mkdirs(); } else { int index = entryName.lastIndexOf("\\"); if (index != -1) { File df = new File(outputDirectory + File.separator + entryName.substring(0, index)); df.mkdirs(); } index = entryName.lastIndexOf("/"); if (index != -1) { File df = new File(outputDirectory + File.separator + entryName.substring(0, index)); df.mkdirs(); } File f = new File(outputDirectory + File.separator + zipEntry.getName()); // f.createNewFile(); in = zipFile.getInputStream(zipEntry); out = new FileOutputStream(f); int c; byte[] by = new byte[1024]; while ((c = in.read(by)) != -1) { out.write(by, 0, c); } out.flush(); } } catch (IOException ex) { ex.printStackTrace(); throw new IOException("解壓失敗:" + ex.toString()); } finally { if (in != null) { try { in.close(); } catch (IOException ex) { } } if (out != null) { try { out.close(); } catch (IOException ex) { } } } } } catch (IOException ex) { ex.printStackTrace(); throw new IOException("解壓失敗:" + ex.toString()); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException ex) { } } } } public static String getPath(Context context) { boolean hasSD = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); String rootPath = hasSD ? (Environment.getExternalStorageDirectory().getPath() + File.separator +"Android" + File.separator + "data" + File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+ "download" + File.separator) : (context.getFilesDir().getPath() + File.separator +"Android" + File.separator + "data" + File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator + "download" + File.separator); File download = new File(rootPath); if (!download.exists()) { download.mkdir(); } return rootPath; } public static String getPath(Context context, String str) { boolean hasSD = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); String rootPath = hasSD ? (Environment.getExternalStorageDirectory().getPath() + File.separator + "Android" + File.separator + "data" + File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator + str + File.separator) : (context.getFilesDir().getPath() + File.separator + "Android" + File.separator + "data" + File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+ str + File.separator); File download = new File(rootPath); if (!download.exists()) { download.mkdir(); } return rootPath; } public static String getSrcPath(Context context, String park) { boolean hasSD = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); String rootPath = hasSD ? (Environment.getExternalStorageDirectory().getPath() + File.separator +"Android" + File.separator + "data" + File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+ "download" + File.separator + "park" + park + File.separator) : (context.getFilesDir().getPath() + File.separator + "Android" + File.separator + "data" + File.separator+SwitchAppTool.APPLICATION_ID+File.separator+"cache"+File.separator+ "download" + File.separator + "park" + park + File.separator); File download = new File(rootPath); if (!download.exists()) { download.mkdir(); } return rootPath; } public static boolean isFileExist(String fileName) { File file = new File(fileName); file.isFile(); return file.exists(); } /** * 清空壓縮包資源列表 */ public static boolean clearZip(Context context, String park) { String path = getSrcPath(context, park); File srcDir = new File(path); String[] children = srcDir.list(); for (int i = 0; i < children.length; i++) { if (!new File(srcDir, children[i]).delete()) { return false; } } return true; } public static JSONArray getSrcInfo(Context context) { String path = getPath(context); File srcDir = new File(path); JSONArray jary = new JSONArray(); if (srcDir.list().length!=0){ String[] arg = srcDir.list(); for (String t : arg) { try { JSONObject jo = new JSONObject(); File file = new File(path + t + File.separator); double size = getFileSizes(file) ; if (t.startsWith("park")){ jo.put("name", t); jo.put("size", size); jary.put(jo); } } catch (Exception e) { } } } return jary; } public static List<String> getSrcList(Context context) { String path = getPath(context); File srcDir = new File(path); String[] arg = srcDir.list(); List<String> list = new ArrayList<String>(); for (String t : arg) { list.add(t); } return list; } public static void delZip(Context context) { String path = getPath(context) + "temp" + File.separator; File srcDir = new File(path); srcDir.delete(); } /** * 遞歸刪除掉文件夾及旗下子目錄 * * @param file */ public static void delSrc(File file) { if (file.isFile()) { file.delete(); return; } if (file.isDirectory()) { File[] childFile = file.listFiles(); if (childFile == null || childFile.length == 0) { file.delete(); return; } for (File f : childFile) { delSrc(f); } file.delete(); } } public static long getFileSizes(File f) throws Exception { long size = 0; File flist[] = f.listFiles(); for (int i = 0; i < flist.length; i++) { if (flist[i].isDirectory()) { size = size + getFileSizes(flist[i]); } else { size = size + getFileSize(flist[i]); } } return size; } public static long getFileSize(File file) throws Exception { long size = 0; if (file.exists()) { FileInputStream fis = null; fis = new FileInputStream(file); size = fis.available(); } else { file.createNewFile(); } return size; } }