android zip解压进度_android从zip文件解压缩文件夹并从该文件夹读取内容

in one of my app i need to extract a zip file that have folder inside and that folder contains images it mean abc.zip=>adb(folder)=>abc.png

i want to extract image file

i used below method

private boolean extractFolder(File destination, File zipFile) throws ZipException, IOException

{

int BUFFER = 8192;

File file = zipFile;

//This can throw ZipException if file is not valid zip archive

ZipFile zip = new ZipFile(file);

// String newPath = destination.getAbsolutePath() + File.separator + FilenameUtils.removeExtension(zipFile.getName());

String newPath = destination.getAbsolutePath() + File.separator + zipFile.getName();

//Create destination directory

new File(newPath).mkdir();

Enumeration zipFileEntries = zip.entries();

//Iterate overall zip file entries

while (zipFileEntries.hasMoreElements())

{

ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

String currentEntry = entry.getName();

File destFile = new File(newPath, currentEntry);

File destinationParent = destFile.getParentFile();

//If entry is directory create sub directory on file system

destinationParent.mkdirs();

if (!entry.isDirectory())

{

//Copy over data into destination file

BufferedInputStream is = new BufferedInputStream(zip

.getInputStream(entry));

int currentByte;

byte data[] = new byte[BUFFER];

//orthodox way of copying file data using streams

FileOutputStream fos = new FileOutputStream(destFile);

BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

while ((currentByte = is.read(data, 0, BUFFER)) != -1) {

dest.write(data, 0, currentByte);

}

dest.flush();

dest.close();

is.close();

}

}

return true;//some error codes etc.

}

but getting zipfolder/foldername/ (Is a directory)

解决方案private void unzip(String src, String dest){

final int BUFFER_SIZE = 4096;

BufferedOutputStream bufferedOutputStream = null;

FileInputStream fileInputStream;

try {

fileInputStream = new FileInputStream(src);

ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));

ZipEntry zipEntry;

while ((zipEntry = zipInputStream.getNextEntry()) != null){

String zipEntryName = zipEntry.getName();

String name = dest.substring(dest.lastIndexOf("/")-1);

File FileName = new File(FolderName);

if (!FileName.isDirectory()) {

try {

if (FileName.mkdir()) {

} else {

}

} catch (Exception e) {

e.printStackTrace();

}

}

File file = new File(FolderName+"/" +zipEntryName);

if (file.exists()){

} else {

if(zipEntry.isDirectory()){

file.mkdirs();

}else{

byte buffer[] = new byte[BUFFER_SIZE];

FileOutputStream fileOutputStream = new FileOutputStream(file);

bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);

int count;

while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {

bufferedOutputStream.write(buffer, 0, count);

}

bufferedOutputStream.flush();

bufferedOutputStream.close();

}

}

}

zipInputStream.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

Try This code it's working for me.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值