1. 程式人生 > >【Android】解決File中文名字問題

【Android】解決File中文名字問題

/**
 * 
 */
package com.ztb.sebook.utils;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;

import com.ztb.sebook.activitys.R;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;

/**
 * 
 * 
 * @author zhao.tb
 */
public class FileUtils {

	/**
	 * unZip
	 * 	UnZip the *****.zip in Assets folder.
	 * 
	 * @param c
	 * @param zipFile
	 * 		<br> Just the Name
	 * @param outFilePath
	 * 		<br> eg.Environment.getExternalStorageDirectory().getAbsolutePath() + "/ebook/data/";
	 * @return
	 */
	public static boolean unZip(Context c,String zipFileName, String outFilePath) {
		boolean flag = false;
		try {
			//ZipFile zf = new ZipFile(zipFile, "GBK"); // 此處設定編碼格式 支援中文檔名解壓
			ZipArchiveInputStream zin = new ZipArchiveInputStream(c.getAssets().open(zipFileName), "GBK", true);
			ArchiveEntry entry = zin.getNextEntry();
			
			byte[] buf = new byte[409600];
			FileOutputStream out;
			
			while (entry != null) {
				if (entry.isDirectory()) {
					File newDir = new File(outFilePath + entry.getName());
					newDir.mkdir();
				} else {
					String name = entry.getName();
					File outputFile = new File(outFilePath + name);
					String outputPath = outputFile.getCanonicalPath();
					name = outputPath.substring(outputPath.lastIndexOf("/") + 1);
					outputPath = outputPath.substring(0, outputPath.lastIndexOf("/"));
					File outputDir = new File(outputPath);
					outputDir.mkdirs();
					outputFile = new File(outputPath, name);
					outputFile.createNewFile();
					out = new FileOutputStream(outputFile);

					int numread = 0;
					do {
						numread = zin.read(buf);
						if (numread <= 0) {
							break;
						} else {
							out.write(buf, 0, numread);
						}
					} while (true);
					out.close();
				}
				entry = zin.getNextEntry();
			}
			
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	

	/**
	 * unZip
	 * 	UnZip file in Raw folder.
	 * @param c
	 * @param rawId
	 * @param outPath
	 * 		<br>eg.Environment.getExternalStorageDirectory().getAbsolutePath() + "/ebook/data/";
	 * @return
	 */
	public static boolean unZip(Context c, int rawId, String outPath) {
		FileOutputStream out;
		byte buf[] = new byte[102400];
		try {
			Resources res = c.getResources();
			AssetFileDescriptor fd = res.openRawResourceFd(rawId);
			InputStream stream = fd.createInputStream();
			ZipInputStream zis = new ZipInputStream(stream);
			ZipEntry entry = zis.getNextEntry();
			while (entry != null) {
				if (entry.isDirectory()) {
					File newDir = new File(outPath + entry.getName());
					newDir.mkdir();
				} else {
					String name = entry.getName();
					File outputFile = new File(outPath + name);
					String outputPath = outputFile.getCanonicalPath();
					name = outputPath.substring(outputPath.lastIndexOf("/") + 1);
					outputPath = outputPath.substring(0, outputPath.lastIndexOf("/"));
					File outputDir = new File(outputPath);
					outputDir.mkdirs();
					outputFile = new File(outputPath, name);
					outputFile.createNewFile();
					out = new FileOutputStream(outputFile);

					int numread = 0;
					do {
						numread = zis.read(buf);
						if (numread <= 0) {
							break;
						} else {
							out.write(buf, 0, numread);
						}
					} while (true);
					out.close();
				}
				entry = zis.getNextEntry();
			}
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
}