Android 下载zip压缩文件并解压

网上有很多介绍下载文件或者解压zip文件的文章,但是两者结合的不多,在此记录一下下载zip文件并直接解压的方法。

其实也很简单,就是把下载文件和解压zip文件结合到一起。下面即代码:

        URLConnection connection;  
        ZipInputStream zipIn = null;
        FileOutputStream fileOut = null;
        ZipEntry zipEntry = null;
        int readedBytes = 0;
        try {
            connection = modelUrl.openConnection();
            Log.v(TAG, "model file length:"+connection.getContentLength());
            zipIn = new ZipInputStream(connection.getInputStream());
            while ((zipEntry = zipIn.getNextEntry()) != null) {
                String entryName = zipEntry.getName();
                if (zipEntry.isDirectory()) {
                    entryName = entryName.substring(0, entryName.length() - 1);
                    File folder = new File(folderPath + File.separator+ entryName);
                    folder.mkdirs();
                } else {
                    String fileName=folderPath + File.separator + entryName;
                    File file = new File(fileName);
                    file.createNewFile();
                    fileOut = new FileOutputStream(file);
                    while ((readedBytes = zipIn.read(downLoadBuffer)) > 0) {
                        fileOut.write(downLoadBuffer, 0, readedBytes);
                        total+=readedBytes;
                    }
                    fileOut.close();
                }
                zipIn.closeEntry();
            }
            zipIn.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。