【Android】Zipファイル解凍方法

1795 ワード

Androidでzipファイルを解凍
public class ZipUtil {
    /**
     *        
     *
     * @param inputStream
     * @param outPathString
     * @throws Exception
     */
    public static void UnZipFolder(InputStream inputStream, String outPathString) throws Exception {
        ZipInputStream inZip = new ZipInputStream(inputStream);
        ZipEntry zipEntry;
        String szName = "";
        File root = new File(outPathString);
        if (!root.exists()) {
            root.mkdir();
        }
        while ((zipEntry = inZip.getNextEntry()) != null) {
            szName = zipEntry.getName();
            if (zipEntry.isDirectory()) {
                //         
                szName = szName.substring(0, szName.length() - 1);
                File folder = new File(outPathString + File.separator + szName);
                folder.mkdirs();
            } else {
                Log.i("tag", outPathString + File.separator + szName);
                File file = new File(outPathString + File.separator + szName);
                if (!file.exists()) {
                    Log.i("tag", "Create the file:" + outPathString + File.separator + szName);
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }
                //         
                FileOutputStream out = new FileOutputStream(file);
                int len;
                byte[] buffer = new byte[2048];
                //   (  )      
                while ((len = inZip.read(buffer)) != -1) {
                    //     (0)    (  )  
                    out.write(buffer, 0, len);
                    out.flush();
                }
                out.close();
            }
        }
        inZip.close();
    }
}