首页 > 代码库 > Android 下载zip压缩文件并解压

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();        }

 

Android 下载zip压缩文件并解压