日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 汉字编码,Android解压中文乱码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在Android中內(nèi)置有解壓的工具,一般可以使用下面的方法解壓:

�注意import的包:

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

/**

* 解壓縮一個文件

*

* @param zipFile 壓縮文件

* @param folderPath 解壓縮的目標(biāo)目錄

* @throws IOException 當(dāng)解壓縮過程出錯時拋出

*/

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()) {

//如果不存在就創(chuàng)建

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();

}

}

}

總結(jié)

以上是生活随笔為你收集整理的android 汉字编码,Android解压中文乱码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。