Android文件工具类(复制文件、解压文件等功能)

public class FileUtilss {
    private static final String TAG = "FileUtils";

    private FileUtilss(){}

    static void copyFiles(String fromPath, String toPath) {
        File fromPathFile = new File(fromPath);
        if (fromPathFile.exists() && fromPathFile.isDirectory()) {
            File[] files = fromPathFile.listFiles();
            for (File file : files) {
                String filename = file.getName();
                String targetPath = getPaths(fromPath, filename);
                String destPath = getPaths(toPath, filename);

                File targetFile = new File(targetPath);
                File destFile = new File(destPath);
                if (file.isDirectory()) {
                    boolean result = destFile.mkdirs();
                    if (result) {
                        copyFiles(targetPath, destPath);
                    }
                } else {
                    try {
                        if (!destFile.getParentFile().exists()) {
                            destFile.getParentFile().mkdirs();
                        }
                        org.apache.commons.io.FileUtils.copyFile(targetFile, destFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    static void removeFiles(String targetPath) {

    }

    public static void copyAssetsFile(String filename, Context context, File destination) throws Exception{
        if (!destination.exists()) {
            if (!destination.getParentFile().exists()) {
                boolean dirsResult = destination.getParentFile().mkdirs();
                Log.d(TAG, "创建文件夹是否成功:"+dirsResult);
            }
            boolean result = destination.createNewFile();
            Log.d(TAG, "创建文件是否成功:"+result);
        }
        AssetManager assetManager = context.getResources().getAssets();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            out = new FileOutputStream(destination);
            copyFileStream(in, out);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void copyFileStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }

    public static void unZipFolder(String zipFileString, String outPathString) throws Exception {
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
        ZipEntry zipEntry;
        String  szName;
        while ((zipEntry = inZip.getNextEntry()) != null) {
            szName = zipEntry.getName();
            if (zipEntry.isDirectory()) {
                //获取部件的文件夹名
                szName = szName.substring(0, szName.length() - 1);
                File folder = new File(outPathString + File.separator + szName);
                folder.mkdirs();
            } else {
                File file = new File(outPathString + File.separator + szName);
                if (!file.exists()){
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }
                // 获取文件的输出流
                FileOutputStream out = new FileOutputStream(file);
                int len;
                byte[] buffer = new byte[1024];
                // 读取(字节)字节到缓冲区
                while ((len = inZip.read(buffer)) != -1) {
                    // 从缓冲区(0)位置写入(字节)字节
                    out.write(buffer, 0, len);
                    out.flush();
                }
                out.close();
            }
        }
        inZip.close();
    }

    /**
     * 拼接文件路径
     * @param first 第一个路径
     * @param more 多个路径的字符串数组
     * @return 拼接好的路径 a/b/c
     */
    static String getPaths(String first, String... more) {
        StringBuilder stringBuilder = new StringBuilder(first);
        int i = 0;
        while (i < more.length) {
            String path = more[i];
            stringBuilder
                    .append(File.separator)
                    .append(path);
            i++;
        }
        return stringBuilder.toString();
    }

    /**
     * 从 SharedPreferences 中获取激活的版本号
     * @param context 上下文
     * @return 当前激活版本号
     */
    static String getActivePackageVersion(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PackageManager.SHARED_PREFERENCES_NAME,
                Context.MODE_PRIVATE);
        return sharedPreferences.getString(PackageManager.ACTIVE_PACKAGE_VERSION_SP_KEY, PackageManager.NONE);
    }

    /**
     * 在 SharedPreferences 中设置激活的版本号
     * @param context 上下文
     * @param version 要设置的版本号
     */
    static void setActivePackageVersion(Context context, String version) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PackageManager.SHARED_PREFERENCES_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(PackageManager.ACTIVE_PACKAGE_VERSION_SP_KEY, version);
        editor.apply();
    }

    static String getWebPath(Context context) {
        File appInsideFile = context.getFilesDir();
        return FileUtilss.getPaths(appInsideFile.getAbsolutePath(), PackageManager.WWW_COMPOSE_PATH);
    }

    static String getPatchPath(Context context) {
        File appInsideFile = context.getFilesDir();
        return FileUtilss.getPaths(appInsideFile.getAbsolutePath(), PackageManager.PATCH_COMPOSE_PATH);
    }

    static String getVersionWebPath(Context context, String version) {
        return FileUtilss.getPaths(getWebPath(context), version);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值