android文件复制及解压

提供两个自己写的方法,目标路径可以是SD卡,也可以是data/data私有文件夹。

/**
     * 解压Zip文件到目标路径
     * 
     * @param zipPath
     *            Zip文件路径
     * @param destdir
     *            目标路径
     * @throws IOException
     */
    public static void unZipFile(String zipPath, File destdir) throws IOException
    {
        if (destdir == null || !destdir.isDirectory())
        {
            throw new IOException("destdir is not valid");
        }
        if (!destdir.exists())
        {
            destdir.mkdirs();
        }
        ZipFile zipFile = new ZipFile(zipPath);
        Enumeration<?> e = zipFile.entries();
        while (e.hasMoreElements())
        {
            ZipEntry entry = (ZipEntry) e.nextElement();
            File file = new File(destdir, entry.getName());
            if (entry.isDirectory())
            {
                if (!file.exists())
                {
                    file.mkdirs();
                }
            } else
            {
                FileHelper.writeInputStreamToFile(zipFile.getInputStream(entry), file);
            }
        }
        zipFile.close();
    }
    /**
     * 复制assets内的指定文件/文件夹到目标路径
     * 
     * @param assetManager
     * @param path
     * @param destdir
     * @throws IOException
     */
    public static void copyAssetsFile(AssetManager assetManager, String path, File destdir) throws IOException
    {
        if (path == null)
        {
            throw new IOException("path is not valid");
        }
        if (!destdir.exists())
        {
            destdir.mkdirs();
        }
        if (path.indexOf('.') >= 0)
        {// 文件
            File file = new File(destdir, path);
            FileHelper.writeInputStreamToFile(assetManager.open(path), file);
            return;
        }
        String[] fileList = assetManager.list(path);
        for (String fileName : fileList)
        {
            String subPath = path + File.separator + fileName;
            if (fileName.indexOf('.') < 0)
            {// 文件夹,递归复制
                copyAssetsFile(assetManager, subPath, new File(destdir, fileName));
            } else
            {// 文件
                FileHelper.writeInputStreamToFile(assetManager.open(subPath), new File(destdir, fileName));
            }
        }
    }
    /**
     * write inputStream to a file
     * 
     * @param inputStream
     * @param file
     * @throws IOException
     */
    public static void writeInputStreamToFile(InputStream inputStream, File file) throws IOException
    {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        BufferedInputStream bi = new BufferedInputStream(inputStream);
        byte[] bytes = new byte[1024];
        int readCount = bi.read(bytes);
        while (readCount != -1)
        {
            bos.write(bytes, 0, readCount);
            readCount = bi.read(bytes);
        }
        bos.close();
    }

 

转载于:https://www.cnblogs.com/toronto/p/4227920.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值