android 汉字编码,Android解压中文乱码

在Android中内置有解压的工具,一般可以使用下面的方法解压:

�注意import的包:

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

/**

* 解压缩一个文件

*

* @param zipFile 压缩文件

* @param folderPath 解压缩的目标目录

* @throws IOException 当解压缩过程出错时抛出

*/

public static 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 in = zf.getInputStream(entry);

if(entry.getName().indexOf("__MACOSX")>=0){

continue;

}

String str = folderPath + File.separator + entry.getName();

str = new String(str.getBytes("8859_1"), "UTF-8");

File desFile = new File(str);

if (!desFile.exists()) {

File fileParentDir = desFile.getParentFile();

if (!fileParentDir.exists()) {

fileParentDir.mkdirs();

}

desFile.createNewFile();

}

OutputStream out = new FileOutputStream(desFile);

byte buffer[] = new byte[BUFF_SIZE];

int realLength;

while ((realLength = in.read(buffer)) > 0) {

out.write(buffer, 0, realLength);

}

in.close();

out.close();

}

}

但是在解压遇到中文的时候,解压出来中文会变成乱码,把上面的编码改成啥都没用。

这时候可以使用apache-ant-zip的解压包来解决:

然后使用这个包中的引用:

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;

public static void upZipFile(File zipFile, String folderPath) throws IOException {

OutputStream os = null;

InputStream is = null;

ZipFile zf = null;

try {

zf = new ZipFile(zipFile,"UTF-8");

String directoryPath = "";

directoryPath = folderPath;

Enumeration entryEnum = zf.getEntries();

if (null != entryEnum) {

ZipEntry zipEntry = null;

while (entryEnum.hasMoreElements()) {

zipEntry = (ZipEntry) entryEnum.nextElement();

if (zipEntry.isDirectory()) {

//不处理文件夹

directoryPath = directoryPath + File.separator

+ zipEntry.getName();

System.out.println(directoryPath);

continue;

}

if (zipEntry.getSize() > 0) {

File targetFile = new File(directoryPath+ File.separator + zipEntry.getName());

if (!targetFile.exists()) {

//如果不存在就创建

File fileParentDir = targetFile.getParentFile();

if (!fileParentDir.exists()) {

fileParentDir.mkdirs();

}

targetFile.createNewFile();

}

os = new BufferedOutputStream(new FileOutputStream(targetFile));

is = zf.getInputStream(zipEntry);

byte[] buffer = new byte[4096];

int readLen = 0;

while ((readLen = is.read(buffer, 0, 1024)) >= 0) {

os.write(buffer, 0, readLen);

}

os.flush();

os.close();

}

}

}

} catch (IOException ex) {

throw ex;

} finally {

if (null != is) {

is.close();

}

if (null != os) {

os.close();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值