当前位置:操作系统 > 安卓/Android >>

android解压缩的方法

/**
* 解压缩含有文件夹的压缩文件
*
* @param zipFile
* @param folderPath
* @throws ZipException
* @throws IOException
*/
public void upZipFile(File zipFile, String folderPath) throws ZipException,
IOException {
File desDir = new File(folderPath);
if (!desDir.exists()) {
// 创建目标目录
desDir.mkdirs();
}


ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
if (entry.isDirectory()) {
String tmpStr = folderPath + File.separator + entry.getName();
tmpStr = new String(tmpStr.getBytes("8859_1"), "GB2312");
File folder = new File(tmpStr);
folder.mkdirs();
} else {
InputStream is = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
// 转换编码,避免中文时乱码
str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str);
if (!desFile.exists()) {
// 创建目标文件
desFile.createNewFile();
}
OutputStream os = new FileOutputStream(desFile);
byte[] buffer = new byte[1024];
int realLength;
while ((realLength = is.read(buffer)) > 0) {
os.write(buffer, 0, realLength);
os.flush();
}
is.close();
os.close();
}


}
zf.close();

}

 

 

 

/**
* 解压缩不含文件夹的压缩包
*
* @param zipFile
* @param folderPath
* @throws ZipException
* @throws IOException
*/
public void upZipFile(File zipFile, String folderPath) throws ZipException,
IOException {
File desDir = new File(folderPath);
if (!desDir.exists()) {
// 创建目标目录
desDir.mkdirs();
}


ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
InputStream is = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
// 转换编码,避免中文时乱码
str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
// 创建目标文件的父目录
fileParentDir.mkdirs();
}
// 创建目标文件
desFile.createNewFile();
}
OutputStream os = new FileOutputStream(desFile);
byte[] buffer = new byte[1024];
int realLength;
while ((realLength = is.read(buffer)) > 0) {
os.write(buffer, 0, realLength);
os.flush();
}
is.close();
os.close();
}
zf.close();
}

 

 

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,