1.选择某个zip文件后,在zip文件的目录创建一个与zip文件同名的文件夹,并在该文件夹下解压缩
因为在windows和在mac下压缩所采用的编码不一样,有可能导致中文名解压缩出来出现乱码。解压缩windows zip包需要设置编码为GBK,解压缩mac zip包需要设置编码为UTF-8。一开始时默认解压缩编码为GBK,若zip包为mac压缩,则转换成UTF-8重新解压缩。能解决大部分的中文名乱码问题,但若是在windows下压缩的mac的文件,或者是在mac下压缩的windows的文件,则无法解决额。
public void unZipFile(Context context, String filePath) {
String absFilePath = filePath.substring(0, filePath.lastIndexOf("/"));
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
String folderPath = absFilePath + File.separator + fileName;
// LogUtil.i("yangqiuyan", absFilePath + " : " + fileName + " : " + folderPath + " ??");
File zipDir = new File(folderPath);
if (!zipDir.exists()) {
zipDir.mkdir();
}
if (!unZipFilebyEcode(filePath, folderPath, "GBK")) {
unZipFilebyEcode(filePath, folderPath, "UTF-8");
}
Intent intent = new Intent(context, ShowUnZipFile.class);
intent.putExtra("filepath",folderPath);
context.startActivity(intent);
}
若编码不同,则不会进入while循环,以此来判断是否选择了正确的解压缩编码。
private boolean unZipFilebyEcode(String filePath, String folderPath, String ecode) {
boolean isUnZipSuccess = false;
try {
// ZipFile zf = new ZipFile(filePath);
ZipFile zf = new ZipFile(filePath, ecode);
Enumeration e = zf.getEntries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
String entryName = ze2.getName();
String path = folderPath + "/" + entryName;;
if (ze2.isDirectory()) {
File decompressDirFile = new File(path);
if (!decompressDirFile.exists()) {
// LogUtil.i("yangqiuyan", path + " mkdir");
decompressDirFile.mkdirs();
}
} else {
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
BufferedInputStream 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);
}
bi.close();
bos.close();
}
isUnZipSuccess = true;
}
zf.close();
} catch (Exception e) {
e.printStackTrace();
}
return isUnZipSuccess;
}
2.解压完后,根据路径,取得该文件夹下所有文件
public List<FileItem> getAllFileByPath(String folderPath) {
<span style="white-space:pre"> </span>// FileItem为自己定义的文件类
List<FileItem> tempList = new ArrayList<FileItem>();
if (TextUtils.isEmpty(folderPath)) {
return tempList;
}
File file = new File(folderPath);
if (!file.exists()) {
return tempList;
}
File[] files = file.listFiles(); //取出该路径下所有文件
if (files != null && files.length > 0) {
Arrays.sort(files, new FileComparatorByTimeDes()); // 文件排序
for (int i = 0; i < files.length; i++) {
FileItem fileItem = new FileItem();
fileItem.filePath = files[i].getAbsolutePath();
fileItem.fileName = files[i].getName();
if (files[i].isDirectory()) {
fileItem.isDirectory = true;
} else {
fileItem.isDirectory = false;
FILE_TYPE type = FileTypeUtil.getFileType(FileUtil.getFileType(fileItem.filePath));
fileItem.fileType = type;
try {
fileItem.fileSize = FileUtil.getFileSize(fileItem.filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
tempList.add(fileItem);
}
}
return tempList;
}