点击此处跳转至jar包的下载链接,无需积分,解压后把前面的wode_去掉
最近做一个功能,下载下来的文件是一个压缩包,需要程序解压后才能使用,但是文件夹里有中文,使用java自带的api解压出来中文后出现乱码问题。百度一下,有一种方案是使用apache的jar包解压,发现到处下载都要积分,有点难受。下面贴出解压代码以及使用方法。
/**
* 解压文件
*/
public void upZipFile(String archive, String decompressDir) {
BufferedInputStream bi = null;
ZipFile zf = null;// 支持中文
try {
zf = new ZipFile(archive, "GBK");
} catch (IOException e) {
e.printStackTrace();
}
Enumeration e = zf.getEntries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
String entryName = ze2.getName();
String path = decompressDir + "/" + entryName;
if (ze2.isDirectory()) {
Log.d("readByApacheZipFile", "正在创建解压目录 - " + entryName);
File decompressDirFile = new File(path);
if (!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
} else {
Log.d("readByApacheZipFile", "正在创建解压文件 - " + entryName);
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(
new FileOutputStream(decompressDir + "/" + entryName));
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
try {
bi = new BufferedInputStream(zf.getInputStream(ze2));
} catch (IOException ioException) {
ioException.printStackTrace();
}
byte[] readContent = new byte[1024];
int readCount = 0;
try {
readCount = bi.read(readContent);
} catch (IOException ioException) {
ioException.printStackTrace();
}
while (readCount != -1) {
try {
bos.write(readContent, 0, readCount);
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
readCount = bi.read(readContent);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
try {
bos.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
try {
zf.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
第一个参数是解压的目标文件路径,第二个参数是解压后的文件放在哪里。
jar包的下载地址在文章开始前的超链接上。