Back to project page markj.
The source code is released under:
GNU Lesser General Public License
If you think the Android project markj listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.markjmind.mobile.api.android.util; /* www . j a v a2 s.c o m*/ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.ImageView; public class JwFile { Context context; public JwFile(Context context){ this.context=context; } public static int BUFFER_SIZE = 1024; public static class Dir{ /** * ???? ????? ????. */ public static File storage=Environment.getExternalStorageDirectory(); /** * Data ????? ????. */ public static File datd=Environment.getDataDirectory().getAbsoluteFile(); /** * DownloadCacheDirectory ????? ????. */ public static File downloadCache=Environment.getDownloadCacheDirectory().getAbsoluteFile(); /** * Root ????? ????. */ public static File root = Environment.getRootDirectory().getAbsoluteFile(); /** * Pictures ????? ????. */ public static File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile(); /** * DCIM ????? ????. */ public static File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsoluteFile(); /** * Downloads ????? ????. */ public static File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsoluteFile(); /** * movies ????? ????. */ public static File movies = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsoluteFile(); /** * music ????? ????. */ public static File music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsoluteFile(); /** * notifications ????? ????. */ public static File notifications = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS).getAbsoluteFile(); /** * podcasts ????? ????. */ public static File podcasts = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS).getAbsoluteFile(); /** * ringtones ????? ????. */ public static File ringtones = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getAbsoluteFile(); /** * CacheDir??? ????. * @param context * @return */ public static File getCacheDir(Context context){ return context.getCacheDir(); } } /** * ????,???? ???? ????. * @param dir * @return File[] */ public static File[] getList(File dir) { if(dir.isDirectory() && dir.exists()){ return dir.listFiles(); }else{ return null; } } /** * ????,???? ???? ????. * @param dirPath * @return File[] */ public static File[] getList(String dirPath){ File dir = new File(dirPath); return getList(dir); } /** * ???? ???? ????. * @param dir * @return File[] */ public static File[] getDirList(File dir){ if(dir.isDirectory() && dir.exists()){ return dir.listFiles(new IsDirFilter(true)); }else{ return null; } } /** * ???? ???? ????. * @param dirPath * @return File[] */ public static File[] getDirList(String dirPath){ File dir = new File(dirPath); return getDirList(dir); } /** * ???????? ????. * @param dir * @return */ public static File[] getFileList(File dir){ if(dir.isDirectory() && dir.exists()){ return dir.listFiles(new IsDirFilter(false)); }else{ return null; } } /** * ???? ?? ????? * @author codemasta * */ private static class IsDirFilter implements FileFilter{ private boolean isDir; public IsDirFilter(boolean isDir){ this.isDir = isDir; } @Override public boolean accept(File pathname) { if(isDir){ return pathname.isDirectory(); }else{ return !pathname.isDirectory(); } } } /** * ??????? ?????. * @param file * @return */ public static boolean mkFile(File file){ boolean result=true; if(!file.getParentFile().exists()){ result = file.getParentFile().mkdirs(); } if(result){ try { return file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } return false; } /** * ??????? ?????. * @param path * @return */ public static boolean mkFile(String path){ File dir = new File(path); return mkFile(dir); } /** * Stream?? ???? ??????? ?????. * @param path * @param in * @param append * @throws IOException */ public static void mkFile(Context context, String path, InputStream in) throws IOException{ OutputStream out = new FileOutputStream(path,false); // FileOutputStream out = context.openFileOutput(path,Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE); byte[] buffer = new byte[BUFFER_SIZE]; int readCount=0; while((readCount=in.read(buffer))!=-1){ out.write(buffer, 0, readCount); out.flush(); } out.close(); in.close(); } /** * Stream?? ???? ??????? ????. * @param file * @param in * @param append * @return * @throws IOException */ public static void mkFile(Context context, File file, InputStream in) throws IOException{ mkFile(context, file.getPath(), in); } /** * byte?? ???? ??????? ?????. * @param file * @param bytes * @param append * @throws IOException */ public static void mkFile(Context context, File file, byte[] bytes) throws IOException{ mkFile(context, file.getPath(), bytes); } /** * byte?? ???? ??????? ?????. * @param path * @param bytes * @param append * @throws IOException */ public static void mkFile(Context context, String path, byte[] bytes) throws IOException{ OutputStream out = new FileOutputStream(path,false); // FileOutputStream out = context.openFileOutput(path,Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE); out.write(bytes); out.flush(); out.close(); } /** * Web?? ?? ??????? ????? * @param context * @param urlName * @param savefile * @throws IOException */ public static void mkFileWeb(Context context, String urlName,File savefile) throws IOException{ URL url = new URL(urlName) ; HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(1000 * 15); urlConnection.setReadTimeout(1000 * 15); InputStream in = urlConnection.getInputStream(); mkFile(context,savefile, in); } /** * Web?? ?? ??????? ????? * @param context * @param urlName * @param fileName * @throws IOException */ public void saveWebFile(Context context,String urlName,String fileName) throws IOException{ mkFileWeb(context, urlName, new File(fileName)); } /** * ??????? ????. * @param taget * @param clone * @throws IOException */ public static void copy(Context context, File target, File clone) throws IOException{ InputStream in = new FileInputStream(target); mkFile(context,clone, in); } /** * ??????? ????. * @param targetPath * @param clone * @throws IOException */ public static void copy(Context context, String targetPath, File clone) throws IOException{ InputStream in = new FileInputStream(targetPath); mkFile(context,clone, in); } /** * ??????? ????. * @param targetPath * @param clonePath * @throws IOException */ public static void copy(Context context, String targetPath, String clonePath) throws IOException{ InputStream in = new FileInputStream(targetPath); mkFile(context,clonePath, in); } /** * ??????? ????. * @param target * @param clonePath * @throws IOException */ public static void copy(Context context, File target, String clonePath) throws IOException{ InputStream in = new FileInputStream(target); mkFile(context, clonePath, in); } /** * ????? ?????. * @param dir * @return */ public static boolean mkDir(File dir){ return dir.mkdirs(); } /** * ????? ?????. * @param path * @return */ public static boolean mkDir(String path){ File dir = new File(path); return dir.mkdirs(); } /** * ???? ?? ????? ????. * @param file * @return */ public static boolean del(File file){ boolean result = true; if(file.exists()){ if(file.isDirectory()){ File[] files = getList(file); for(int i=0;i<files.length;i++){ result = del(files[i]); } } if(!result){ file.delete(); return false; } return file.delete(); }else{ return false; } } /** * ??????? ????. * @param files * @param zipFile * @throws IOException */ public static void zip(String[] filePath, File zipFile) throws IOException { File[] files = new File[filePath.length]; for(int i=0;i<files.length;i++){ files[i] = new File(filePath[i]); } zip(files, zipFile); } public static void zip(File[] files, File zipFile) throws IOException { BufferedInputStream origin = null; ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile))); try { byte data[] = new byte[BUFFER_SIZE]; for (int i = 0; i < files.length; i++) { FileInputStream fi = new FileInputStream(files[i]); origin = new BufferedInputStream(fi, BUFFER_SIZE); try { // ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1)); ZipEntry entry = new ZipEntry(files[i].getName()); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) { out.write(data, 0, count); } } finally { origin.close(); } } } finally { out.close(); } } public static void zipFolder(File srcFolder, String destZipFile) throws Exception { ZipOutputStream zip = null; FileOutputStream fileWriter = null; fileWriter = new FileOutputStream(destZipFile); zip = new ZipOutputStream(fileWriter); if(srcFolder.isDirectory()){ addFolderToZip(null, srcFolder.getPath(), zip); }else{ addFileToZip(null, srcFolder.getPath(),zip); } zip.flush(); zip.close(); } public static void zipFolder(String srcFolder, String destZipFile) throws Exception { zipFolder(new File(srcFolder), destZipFile); } private static void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else { byte[] buf = new byte[1024]; int len; FileInputStream in = new FileInputStream(srcFile); if(path==null){ zip.putNextEntry(new ZipEntry(folder.getName())); }else{ zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); } while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } } private static void addFolderToZip(String path, String srcFolder,ZipOutputStream zip) throws Exception { File folder = new File(srcFolder); for (String fileName : folder.list()) { if (path==null) { addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip); } else { addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip); } } } public static void zip(File[] files, String zipFile) throws IOException { zip(files, new File(zipFile)); } public static void zip(String[] filePath, String zipFile) throws IOException { zip(filePath, new File(zipFile)); } /** * ????? ????. * @param zipFile * @param location * @throws IOException */ public static void unZip(File zipFile, File location) throws IOException { if(!location.isDirectory()){ System.out.println("????? ???? location??? ????? ????."); return; } if(!location.exists()) { location.mkdirs(); } ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile)); try { ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { File unzipFile = new File(location,ze.getName()); if(!unzipFile.getParentFile().exists()) { unzipFile.getParentFile().mkdirs(); } if (ze.isDirectory()) { if(!unzipFile.isDirectory()) { unzipFile.mkdirs(); } } else { FileOutputStream fout = new FileOutputStream(unzipFile, false); try { for (int c = zin.read(); c != -1; c = zin.read()) { fout.write(c); fout.flush(); } zin.closeEntry(); } finally { fout.close(); } } } } finally { zin.close(); } } public static void unZip(File zipFile, String location) throws IOException { unZip(zipFile, new File(location)); } public static void unZip(String zipFile, File location) throws IOException { unZip(new File(zipFile), location); } public static void unZip(String zipFile, String location) throws IOException { unZip(new File(zipFile), new File(location)); } /** * ???? ?? ????? ????. * @param file * @return */ public static boolean del(String fileName){ File file = new File(fileName); return del(file); } /** * ??????? ?????? ????. * @param filePath * @return */ public static String getParentPath(String filePath){ File file = new File(filePath); return file.getParentFile().getPath(); } /** * ?????? ????. * @param filePath * @return */ public static String getFileName(String filePath){ File file = new File(filePath); return file.getName(); } /** * ?????? ???????? ????. * @return */ public static String getTempFileName(){ String path = ""+new Date().getTime(); return path; } ///////////////////////////////////////////////////////////////////////////////////////////////// public void saveWebFile(String urlName,String fileName) throws MalformedURLException, ProtocolException, FileNotFoundException,IOException { WebConnection wc = new WebConnection(); InputStream in = wc.getInputStreamGet(urlName); saveFile(in,fileName); } public void saveFile(InputStream in, String fileName) throws FileNotFoundException,IOException { int readCount = 0; byte[] buf = new byte[1024]; FileOutputStream out = context.openFileOutput(fileName,Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE); while((readCount=in.read(buf))!=-1){ out.write(buf,0,readCount); } in.close(); out.close(); } public Drawable getDrawable(String fileName) throws FileNotFoundException,IOException{ if(fileName==null || fileName.equals("")){ return null; } FileInputStream in = getFileInputStream(fileName); Drawable d = Drawable.createFromStream(in, "none"); in.close(); return d; } public FileInputStream getFileInputStream(String fileName) throws FileNotFoundException{ if(fileName==null || fileName.equals("")){ return null; } FileInputStream in = context.openFileInput(fileName); return in; } public View drawView(String fileName,View iv){ try { if(fileName!=null && !fileName.equals("")){ Drawable d = getDrawable(fileName); if(d==null){ return null; } iv.setBackgroundDrawable(d); return iv; }else{ return null; } } catch (Exception e) { Log.d("??? ",e.getMessage()); e.printStackTrace(); return null; } } public View setImgDrawView(String fileName,ImageView iv){ try { Drawable d = getDrawable(fileName); if(d==null){ return null; } iv.setImageDrawable(d); return iv; } catch (Exception e) { Log.d("??? ",e.getMessage()); e.printStackTrace(); return null; } } }