【Android】解决File中文名字问题
16lz
2021-01-26
Apache jar 包下载
/** * */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;}}}
更多相关文章
- http通信,Android(安卓)Gzip压缩解压
- Android倒计时控件 CountdownView
- 通过SOCKET实现文件下载
- Android(安卓)中英文切换工具类
- Android中关于路径
- Volley框架网络访问String,JsonObject,JsonArray
- Android(安卓)Java 与 C++ 调用过程中的常量,路径名、文件名、后
- Android(安卓)zip文件压缩解压缩
- android 从Assets中读取中文文本