Skip to content

Instantly share code, notes, and snippets.

@xxnjdlys
Created May 4, 2017 06:37
Show Gist options
  • Save xxnjdlys/d5878b756d0df4439cbc0d10192c460d to your computer and use it in GitHub Desktop.
Save xxnjdlys/d5878b756d0df4439cbc0d10192c460d to your computer and use it in GitHub Desktop.
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import java.io.File;
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.security.MessageDigest;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Created by 十三姐家的小钻风
* Date: 2014/6/26.
* Time: 下午9:35
* Goodluck and enjoy it.
*/
public class OmniStorage {
public static final String FORDER_DOWNLOAD = "Download";
/*
* I want a place to save my file and I need other apps can access it
* to get FileOutputStream, please use StorageManager.get...Stream
* @param desiredDirName: relative dir name
*/
public static File getPublicStorage(String desiredDirName, Context context) {
File file = Environment.getExternalStoragePublicDirectory(FORDER_DOWNLOAD);
File destDir = new File(file, desiredDirName);
if (checkFileOkForStorage(destDir)) {
return destDir;
}
try{
file = context.getExternalFilesDir(null);
destDir = new File(file, desiredDirName);
}catch (Exception ignore){
// @See getPublicStorageFile()
}
if (file != null && checkFileOkForStorage(destDir)) {
return destDir;
}
// public locations not available, use private location
return context.getFilesDir();
}
public static File getPublicStorageFile(String desiredDirName, String fileName, Context context) {
File file = Environment.getExternalStoragePublicDirectory("Download");
File destDir = new File(file, desiredDirName);
if (checkFileOkForStorage(destDir)) {
File destFile = new File(destDir, fileName);
return destFile;
}
try {
file = context.getExternalFilesDir(null);
destDir = new File(file, desiredDirName);
fileChmod(destDir,true);
} catch (Exception ignore) {
// https://bugly.qq.com/v2/crash/apps/900020804/issues/33012?pid=1
// context.getExternalFilesDir throws securityException
}
if (file != null && checkFileOkForStorage(destDir)) {
File destFile = new File(destDir, fileName);
return destFile;
}
File innerDir = context.getFilesDir();
File destFileDir = new File(innerDir, desiredDirName);
makeFileChmod(destFileDir, true);
File destFile = new File(destFileDir, fileName);
makeFileChmod(destFile, false);
if (!destFile.exists() || destFile.length() == 0) {
try {
@SuppressLint({"WorldWriteableFiles", "WorldReadableFiles"})
FileOutputStream fos = context.openFileOutput(fileName,
Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
fos.close();
} catch (IOException ignore) {
}
}
return destFile;
}
public static void makeFileChmod(File file, boolean isDir) {
if (file != null) {
// file not exist
if (!file.exists()) {
boolean make = false;
try {
make = isDir ? file.mkdirs() : file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Log.v("SYCN", "make file " + file.getPath() + " " + make);
}
// chmod +w
@SuppressLint("SetWorldWritable") boolean a = file.setWritable(true, false);
// chmod +r
@SuppressLint("SetWorldReadable") boolean b = file.setReadable(true, false);
boolean c = file.setExecutable(true, false);
Log.v("SYCN", file.getPath() + " chmod w:" + a + " r:" + b + " e:" + c);
}
}
@SuppressLint("WorldWriteableFiles")
public static FileOutputStream getPublicFileOutputStream(File dir, String filename, Context context)
throws FileNotFoundException {
File filesDir = context.getFilesDir();
if (dir.equals(filesDir)) {
return context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE);
}
return new FileOutputStream(new File(dir, filename));
}
public static boolean checkFileOkForStorage(File file) {
if (file == null) {
return false;
} else if (file.exists()) {
if (!file.isDirectory()) {
return false;
}
} else {
if (!file.mkdirs()) {
return false;
}
}
return file.canWrite();
}
public static String md5File(File file) throws Exception {
if (!file.exists()) {
throw new Exception("file not found");
}
return md5InputStream(new FileInputStream(file));
}
public static String md5InputStream(InputStream inputStream) throws Exception {
MessageDigest md5Util = MessageDigest.getInstance("MD5");
md5Util.reset();
if (inputStream == null) {
return "";
}
byte[] buffer = new byte[2048];
int read;
while ((read = inputStream.read(buffer)) > 0) {
md5Util.update(buffer, 0, read);
}
inputStream.close();
byte[] digestBytes = md5Util.digest();
StringBuilder stringBuilder = new StringBuilder("");
if (digestBytes == null || digestBytes.length <= 0) {
return "";
}
int i = 0;
while (i < digestBytes.length) {
String hv;
int v = (digestBytes[i] >> 4) & 0x0F;
hv = Integer.toHexString(v);
stringBuilder.append(hv);
v = digestBytes[i] & 0x0F;
hv = Integer.toHexString(v);
stringBuilder.append(hv);
i++;
}
return stringBuilder.toString();
}
public static boolean upZipFile(File zipFile, String folderPath) throws IOException {
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdirs();
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry)entries.nextElement());
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[2048];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment