Android Zip文件解压缩代码
生活随笔
收集整理的這篇文章主要介紹了
Android Zip文件解压缩代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在Android平臺(tái)中如何實(shí)現(xiàn)Zip文件的解壓縮功能呢? 因?yàn)锳ndroid內(nèi)部已經(jīng)集成了zlib庫(kù),對(duì)于英文和非密碼的Zip文件解壓縮還是比較簡(jiǎn)單的,下面Android123給大家一個(gè)解壓縮zip的java代碼,可以在Android上任何版本中使用,Unzip這個(gè)靜態(tài)方法比較簡(jiǎn)單,參數(shù)一為源zip文件的完整路徑,參數(shù)二為解壓縮后存放的文件夾。
private static void Unzip(String zipFile, String targetDir) {
? ?int BUFFER = 4096; //這里緩沖區(qū)我們使用4KB,
? ?String strEntry; //保存每個(gè)zip的條目名稱
? ?try {
? ? BufferedOutputStream dest = null; //緩沖輸出流
? ? FileInputStream fis = new FileInputStream(zipFile);
? ? ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
? ? ZipEntry entry; //每個(gè)zip條目的實(shí)例
? ? while ((entry = zis.getNextEntry()) != null) {
? ? ?try {
? ? ? ?Log.i("Unzip: ","="+ entry);
? ? ? int count;?
? ? ? byte data[] = new byte[BUFFER];
? ? ? strEntry = entry.getName();
? ? ? File entryFile = new File(targetDir + strEntry);
? ? ? File entryDir = new File(entryFile.getParent());
? ? ? if (!entryDir.exists()) {
? ? ? ?entryDir.mkdirs();
? ? ? }
? ? ? FileOutputStream fos = new FileOutputStream(entryFile);
? ? ? dest = new BufferedOutputStream(fos, BUFFER);
? ? ? while ((count = zis.read(data, 0, BUFFER)) != -1) {
? ? ? ?dest.write(data, 0, count);
? ? ? }
? ? ? dest.flush();
? ? ? dest.close();
? ? ?} catch (Exception ex) {
? ? ? ex.printStackTrace();
? ? ?}
? ? }
? ? zis.close();
? ?} catch (Exception cwj) {
? ? cwj.printStackTrace();
? ?}
? }
? 上面是Android開發(fā)網(wǎng)總結(jié)的zip文件解壓縮代碼,希望你大家有用,需要注意的是參數(shù)均填寫完整的路徑,比如/mnt/sdcard/xxx.zip這樣的類型。
private static void Unzip(String zipFile, String targetDir) {
? ?int BUFFER = 4096; //這里緩沖區(qū)我們使用4KB,
? ?String strEntry; //保存每個(gè)zip的條目名稱
? ?try {
? ? BufferedOutputStream dest = null; //緩沖輸出流
? ? FileInputStream fis = new FileInputStream(zipFile);
? ? ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
? ? ZipEntry entry; //每個(gè)zip條目的實(shí)例
? ? while ((entry = zis.getNextEntry()) != null) {
? ? ?try {
? ? ? ?Log.i("Unzip: ","="+ entry);
? ? ? int count;?
? ? ? byte data[] = new byte[BUFFER];
? ? ? strEntry = entry.getName();
? ? ? File entryFile = new File(targetDir + strEntry);
? ? ? File entryDir = new File(entryFile.getParent());
? ? ? if (!entryDir.exists()) {
? ? ? ?entryDir.mkdirs();
? ? ? }
? ? ? FileOutputStream fos = new FileOutputStream(entryFile);
? ? ? dest = new BufferedOutputStream(fos, BUFFER);
? ? ? while ((count = zis.read(data, 0, BUFFER)) != -1) {
? ? ? ?dest.write(data, 0, count);
? ? ? }
? ? ? dest.flush();
? ? ? dest.close();
? ? ?} catch (Exception ex) {
? ? ? ex.printStackTrace();
? ? ?}
? ? }
? ? zis.close();
? ?} catch (Exception cwj) {
? ? cwj.printStackTrace();
? ?}
? }
? 上面是Android開發(fā)網(wǎng)總結(jié)的zip文件解壓縮代碼,希望你大家有用,需要注意的是參數(shù)均填寫完整的路徑,比如/mnt/sdcard/xxx.zip這樣的類型。
轉(zhuǎn)自:http://blog.163.com/itsmallbird@126/blog/static/16913910920113155852688/
轉(zhuǎn)載于:https://www.cnblogs.com/sesexxoo/archive/2012/08/06/6189992.html
總結(jié)
以上是生活随笔為你收集整理的Android Zip文件解压缩代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决 IE8下 vs2008 无法调试
- 下一篇: Android 更改签名