Android 解压Zip文件,中文乱码
生活随笔
收集整理的這篇文章主要介紹了
Android 解压Zip文件,中文乱码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考自:中文亂碼
直接上代碼:
?
package com.xxx.utils;import android.util.Log;import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.BitSet;//import org.slf4j.Logger; //import org.slf4j.LoggerFactory;/*** @author 自動識別文件編碼格式*/ public class EncodeUtil {public static String TAG = "EncodeUtil";private static int BYTE_SIZE = 8;public static String CODE_UTF8 = "UTF-8";public static String CODE_UTF8_BOM = "UTF-8_BOM";public static String CODE_GBK = "GBK";/*** 通過文件全名稱獲取編碼集名稱** @param fullFileName* @param ignoreBom* @return* @throws Exception*/public static String getEncode(String fullFileName, boolean ignoreBom) throws Exception {Log.d(TAG, fullFileName);BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fullFileName));return getEncode(bis, ignoreBom);}/*** 通過文件緩存流獲取編碼集名稱,文件流必須為未曾** @param bis* @param ignoreBom 是否忽略utf-8 bom* @return* @throws Exception*/public static String getEncode(BufferedInputStream bis, boolean ignoreBom) throws Exception {bis.mark(0);String encodeType = "未識別";byte[] head = new byte[3];bis.read(head);if (head[0] == -1 && head[1] == -2) {encodeType = "UTF-16";} else if (head[0] == -2 && head[1] == -1) {encodeType = "Unicode";} else if (head[0] == -17 && head[1] == -69 && head[2] == -65) { //帶BOMif (ignoreBom) {encodeType = CODE_UTF8;} else {encodeType = CODE_UTF8_BOM;}} else if ("Unicode".equals(encodeType)) {encodeType = "UTF-16";} else if (isUTF8(bis)) {encodeType = CODE_UTF8;} else {encodeType = CODE_GBK;}Log.d(TAG, "result encode type : " + encodeType);return encodeType;}/*** 是否是無BOM的UTF8格式,不判斷常規場景,只區分無BOM UTF8和GBK** @param bis* @return*/private static boolean isUTF8(BufferedInputStream bis) throws Exception {bis.reset();//讀取第一個字節int code = bis.read();do {BitSet bitSet = convert2BitSet(code);//判斷是否為單字節if (bitSet.get(0)) {//多字節時,再讀取N個字節if (!checkMultiByte(bis, bitSet)) {//未檢測通過,直接返回return false;}} else {//單字節時什么都不用做,再次讀取字節}code = bis.read();} while (code != -1);return true;}/*** 檢測多字節,判斷是否為utf8,已經讀取了一個字節** @param bis* @param bitSet* @return*/private static boolean checkMultiByte(BufferedInputStream bis, BitSet bitSet) throws Exception {int count = getCountOfSequential(bitSet);byte[] bytes = new byte[count - 1];//已經讀取了一個字節,不能再讀取bis.read(bytes);for (byte b : bytes) {if (!checkUtf8Byte(b)) {return false;}}return true;}/*** 檢測單字節,判斷是否為utf8** @param b* @return*/private static boolean checkUtf8Byte(byte b) throws Exception {BitSet bitSet = convert2BitSet(b);return bitSet.get(0) && !bitSet.get(1);}/*** 檢測bitSet中從開始有多少個連續的1** @param bitSet* @return*/private static int getCountOfSequential(BitSet bitSet) {int count = 0;for (int i = 0; i < BYTE_SIZE; i++) {if (bitSet.get(i)) {count++;} else {break;}}return count;}/*** 將整形轉為BitSet** @param code* @return*/private static BitSet convert2BitSet(int code) {BitSet bitSet = new BitSet(BYTE_SIZE);for (int i = 0; i < BYTE_SIZE; i++) {int tmp3 = code >> (BYTE_SIZE - i - 1);int tmp2 = 0x1 & tmp3;if (tmp2 == 1) {bitSet.set(i);}}return bitSet;}/*** 將一指定編碼的文件轉換為另一編碼的文件** @param oldFullFileName* @param oldCharsetName* @param newFullFileName* @param newCharsetName*/public static void convert(String oldFullFileName, String oldCharsetName, String newFullFileName, String newCharsetName) throws Exception {Log.d(TAG, oldFullFileName + oldCharsetName);Log.d(TAG, newFullFileName + newCharsetName);StringBuffer content = new StringBuffer();BufferedReader bin = new BufferedReader(new InputStreamReader(new FileInputStream(oldFullFileName), oldCharsetName));String line;while ((line = bin.readLine()) != null) {content.append(line);content.append(System.getProperty("line.separator"));}newFullFileName = newFullFileName.replace("\\", "/");File dir = new File(newFullFileName.substring(0, newFullFileName.lastIndexOf("/")));if (!dir.exists()) {dir.mkdirs();}Writer out = new OutputStreamWriter(new FileOutputStream(newFullFileName), newCharsetName);out.write(content.toString());}} package com.xxx.utils;import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.nio.charset.Charset; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile;public class MyUtils {/*** 使用GBK編碼可以避免壓縮中文文件名亂碼*/private static final String CHINESE_CHARSET = "GBK";/*** 文件讀取緩沖區大小*/private static final int CACHE_SIZE = 1024;/*** zip文件解壓** @param inputFile 待解壓文件夾/文件* @param destDirPath 解壓路徑*/ // @RequiresApi(api = Build.VERSION_CODES.N)public static void zipUncompress(String inputFile, String destDirPath) throws Exception {File srcFile = new File(inputFile);//獲取當前壓縮文件// 判斷源文件是否存在if (!srcFile.exists()) {throw new Exception(srcFile.getPath() + "所指文件不存在");}String fileEncode = EncodeUtil.getEncode(inputFile, true);ZipFile zipFile = new ZipFile(srcFile, Charset.forName(fileEncode));//創建壓縮文件對象//開始解壓Enumeration<?> entries = zipFile.entries();while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();// 如果是文件夾,就創建個文件夾if (entry.isDirectory()) {String dirPath = destDirPath + "/" + entry.getName();srcFile.mkdirs();} else {// 如果是文件,就先創建一個文件,然后用io流把內容copy過去File targetFile = new File(destDirPath + "/" + entry.getName());// 保證這個文件的父文件夾必須要存在if (!targetFile.getParentFile().exists()) {targetFile.getParentFile().mkdirs();}targetFile.createNewFile();// 將壓縮文件內容寫入到這個文件中InputStream is = zipFile.getInputStream(entry);FileOutputStream fos = new FileOutputStream(targetFile);int len;byte[] buf = new byte[1024];while ((len = is.read(buf)) != -1) {fos.write(buf, 0, len);}// 關流順序,先打開的后關閉fos.close();is.close();}}} }使用方式如下:
private static final String ImageZIPName= "xxxlib.zip";private static final String destPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();private static final String srcPath = destPath+File.separator+ImageZIPName;private void doActionFunc(){try {zipUncompress(srcPath, destPath);} catch (Exception e) {e.printStackTrace();} }總結
以上是生活随笔為你收集整理的Android 解压Zip文件,中文乱码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器系统安装oracle数据库,Ora
- 下一篇: android sina oauth2.