如何将一个zip文件原本的目录结构完整的解压出来呢,我用的这个解压方法目前只能把文件解压出来,原本的目录结构没了
/**
* 解压Zip文件
* @param srcPath zip文件路径
* @param targePath 解压路径
* @throws IOException
*/
public static void unZipFile(String srcPath, String targePath) throws IOException {
BufferedInputStream bi;
ZipFile zf = new ZipFile(srcPath);
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
String entryName = ze2.getName();
String path = targePath + "/" + entryName;
if (ze2.isDirectory()) {
Log.d(TAG,"正在创建解压目录 - " + entryName);
File decompressDirFile = new File(path);
if (!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
} else {
Log.d(TAG,"正在创建解压文件 - " + entryName);
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targePath + "/" + entryName));
bi = new BufferedInputStream(zf.getInputStream(ze2));
byte[] readContent = new byte[1024];
int readCount = bi.read(readContent);
while (readCount != -1) {
bos.write(readContent, 0, readCount);
readCount = bi.read(readContent);
}
bos.close();
}
}
zf.close();
}
zip文件的entry可以判定是目录还是文件的啊,根据这个就可以做到解压时与zip文件里的目录树保持一致