您的位置:首页 > 移动开发 > Android开发

Android:文件按类型打开和压缩解压

2016-11-18 08:45 288 查看
文件管理类:

public class FileUtil {

/**
* 转换文件大小单位
*/
public static String convertFileSize(long fileSize) {
String newSize = "";
if (fileSize < 1024) {
newSize = fileSize + "B";
} else if (fileSize >= 1024 && fileSize < 1024 * 1024) {
newSize = String.valueOf(fileSize / 1024) + "KB";
} else {
DecimalFormat format = new DecimalFormat("0.00");
String result = format.format((double) fileSize / (1024 * 1024));
newSize = result + "MB";
}
return newSize;
}

/**
* 根据文件扩展名获取打开类型
*/
private static String getMIMEType(File f) {
String type = "";
String fileName = f.getName();
String end = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
if (end.equals("doc") || end.equals("docx")) {
type = "application/msword";
} else if (end.equals("xls") || end.equals("xlsx")) {
type = "application/vnd.ms-excel";
} else if (end.equals("ppt") || end.equals("pptx")) {
type = "application/vnd.ms-powerpoint";
} else if (end.equals("txt") || end.equals("log") || end.equals("xml")) {
type = "text/plain";
} else if (end.equals("pdf")) {
type = "application/pdf";
} else if (end.equals("rtf")) {
type = "application/rtf";
} else if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
type = "audio/*";
} else if (end.equals("3gp") || end.equals("mp4") || end.equals("avi")) {
type = "video/*";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp") || end.equals("ico")) {
type = "image/*";
} else if (end.equals("apk")) {
type = "application/vnd.android.package-archive";
} else if (end.equals("chm")) {
type = "application/x-chm";
} else if (end.equals("htm") || end.equals("html")) {
type = "text/html";
}
return type;
}

/**
* 按文件扩展名设置文件图标
*/
public static Bitmap getFileIcon(Context context, File file) {
if (file.isDirectory()) {
return BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_folder);
}
String fileName = file.getName();
Bitmap bm = null;
String end = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_audio);
} else if (end.equals("apk")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_apk);
} else if (end.equals("text") || end.equals("txt")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_txt);
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp") || end.equals("ico")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_image);
} else if (end.equals("rar") || end.equals("zip")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_rar);
} else if (end.equals("jar")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_unknown);
} else if (end.equals("rmvb") || end.equals("avi") || end.equals("mp4")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_video);
} else if (end.equals("doc") || end.equals("docx")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_word);
} else if (end.equals("xls") || end.equals("xlsx")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_excel);
} else if (end.equals("ppt") || end.equals("pptx")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_ppt);
} else if (end.equals("pdf")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_pdf);
} else if (end.equals("rtf")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_rtf);
} else if (end.equals("log")) {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_log);
} else {
bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.filetype_unknown);
}
return bm;
}

/**
* 按类型打开文件
**/
public static void openFile(Context context, File f) {
if (f.length() > 0) {
try {
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory("android.intent.category.DEFAULT");
String type = getMIMEType(f);
intent.setDataAndType(Uri.fromFile(f), type);
context.startActivity(intent);
} catch (Exception e) {
Toast.makeText(context, context.getString(R.string.filemanager_nosupportfile), Toast.LENGTH_SHORT);
e.printStackTrace();
}
} else {
Toast.makeText(context, context.getString(R.string.filemanager_nosupportfile), Toast.LENGTH_SHORT);
}
}

/**
* SD卡是否就绪
*/
public static boolean sdCardIsExsit() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

}


文件压缩、解压类:

public class ZipUtil {
private static boolean isCancel;

public static void setCancel(boolean isCancel1) {
isCancel = isCancel1;
}

/**
* 压缩
*
* @param srcFilePath 待压缩的目录或文件路径
* @param tarFilePath 压缩后的文件路径
*/
public static void compress(String srcFilePath, String tarFilePath, ThreadCallBack threadCallBack) throws IOException {
isCancel = false;
File folder = new File(srcFilePath);
if (folder.exists()) {
List<File> fileList = getSubFiles(new File(srcFilePath));
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tarFilePath));
ZipEntry ze = null;
File file = null;
byte[] buf = new byte[1024];
int len = 0;
String fileName = null;
for (int i = 0; i < fileList.size() && !isCancel; i++) {
file = fileList.get(i);
//回调外部,用于更新进度条
threadCallBack.callBack(file.getName());
if (file.getPath().length() > srcFilePath.length()) {
fileName = file.getPath().substring(srcFilePath.length() + 1);
} else {
fileName = file.getName();
}
//文件实体
ze = new ZipEntry(fileName);
ze.setSize(file.length());
ze.setTime(file.lastModified());
//将实体加入压缩包中
zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(file));
while ((len = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, len);
}
is.close();
}
zos.close();
if (!isCancel) {
threadCallBack.callBack("end");
}
} else {
throw new IOException("this folder isnot exist!");
}
}

/**
* 取得指定目录下的所有文件列表,包括子目录.
*
* @param baseDir 指定的目录或文件,如果是文件就加入列表,目录就继续取下级子文件
* @return 文件列表
*/
private static List<File> getSubFiles(File baseDir) {
List<File> files = new ArrayList<File>();
if (!baseDir.isDirectory()) {
files.add(baseDir);
} else {
File[] tmp = baseDir.listFiles();
for (int i = 0; i < tmp.length; i++) {
if (tmp[i].isFile()) {
files.add(tmp[i]);
}
if (tmp[i].isDirectory()) {
files.addAll(getSubFiles(tmp[i]));
}
}
}
return files;
}

/**
* 解压缩
*
* @param srcFilePath 待解压的文件路径
* @param tarFilePath 解压后的目录或文件路径
*/
public static void uncompress(String srcFilePath, String tarFilePath, ThreadCallBack threadCallBack) throws IOException {
isCancel = false;
ZipFile zipFile = new ZipFile(srcFilePath);
Enumeration zipList = zipFile.getEntries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zipList.hasMoreElements() && !isCancel) {
ze = (ZipEntry) zipList.nextElement();
if (ze.isDirectory()) {
continue;
}
//回调外部更新进度条
threadCallBack.callBack(ze.getName());
OutputStream out = new BufferedOutputStream(new FileOutputStream(mkdirs(tarFilePath, ze.getName())));
InputStream in = new BufferedInputStream(zipFile.getInputStream(ze));
int readLen = 0;
while ((readLen = in.read(buf, 0, 1024)) != -1) {
out.write(buf, 0, readLen);
}
in.close();
out.close();
}
zipFile.close();
if (!isCancel) {
threadCallBack.callBack("end");
}
}

/**
* 创建目录或文件
*
* @param baseDir 解压后的目录或文件路径
* @param itemName ZipEntry中的name
* @return 文件
*/
private static File mkdirs(String baseDir, String itemName) {
File file = new File(baseDir, itemName);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
return file;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: