| 14 | import java.util.zip.ZipFile; |
| 15 | |
| 16 | public class FileUtil { |
| 17 | |
| 18 | public static void openFile(File file) { |
| 19 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 20 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 21 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 22 | intent.setDataAndType(getShareUri(file), FileUtil.getMimeType(file.getName())); |
| 23 | Init.context().startActivity(intent); |
| 24 | } |
| 25 | |
| 26 | public static void unzip(File target, File path) { |
| 27 | try (ZipFile zip = new ZipFile(target.getAbsolutePath())) { |
| 28 | Enumeration<?> entries = zip.entries(); |
| 29 | while (entries.hasMoreElements()) { |
| 30 | ZipEntry entry = (ZipEntry) entries.nextElement(); |
| 31 | File out = new File(path, entry.getName()); |
| 32 | if (entry.isDirectory()) out.mkdirs(); |
| 33 | else Path.copy(zip.getInputStream(entry), out); |
| 34 | } |
| 35 | } catch (Exception e) { |
| 36 | e.printStackTrace(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | private static Uri getShareUri(File file) { |
| 41 | return Build.VERSION.SDK_INT < Build.VERSION_CODES.N ? Uri.fromFile(file) : FileProvider.getUriForFile(Init.context(), Init.context().getPackageName() + ".provider", file); |
| 42 | } |
| 43 | |
| 44 | private static String getMimeType(String fileName) { |
| 45 | String mimeType = URLConnection.guessContentTypeFromName(fileName); |
| 46 | return TextUtils.isEmpty(mimeType) ? "*/*" : mimeType; |
| 47 | } |
| 48 | } |
nothing calls this directly
no outgoing calls
no test coverage detected