安卓解压文件乱码

文章讲述了在Java中遇到解压包含中文文件名的zip包时出现乱码的问题,作者尝试使用Apache的jar包来解决,并提供了相应的解压代码示例。代码示例展示了如何使用Apache的ZipFile类以GBK编码解压文件,避免中文乱码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击此处跳转至jar包的下载链接,无需积分,解压后把前面的wode_去掉
最近做一个功能,下载下来的文件是一个压缩包,需要程序解压后才能使用,但是文件夹里有中文,使用java自带的api解压出来中文后出现乱码问题。百度一下,有一种方案是使用apache的jar包解压,发现到处下载都要积分,有点难受。下面贴出解压代码以及使用方法。

    /**
     * 解压文件
     */
    public void upZipFile(String archive, String decompressDir) {
        BufferedInputStream bi = null;
        ZipFile zf = null;// 支持中文
        try {
            zf = new ZipFile(archive, "GBK");
        } catch (IOException e) {
            e.printStackTrace();
        }

        Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            ZipEntry ze2 = (ZipEntry) e.nextElement();
            String entryName = ze2.getName();
            String path = decompressDir + "/" + entryName;
            if (ze2.isDirectory()) {
                Log.d("readByApacheZipFile", "正在创建解压目录 - " + entryName);
                File decompressDirFile = new File(path);
                if (!decompressDirFile.exists()) {
                    decompressDirFile.mkdirs();
                }
            } else {
                Log.d("readByApacheZipFile", "正在创建解压文件 - " + entryName);
                String fileDir = path.substring(0, path.lastIndexOf("/"));
                File fileDirFile = new File(fileDir);
                if (!fileDirFile.exists()) {
                    fileDirFile.mkdirs();
                }
                BufferedOutputStream bos = null;
                try {
                    bos = new BufferedOutputStream(
                            new FileOutputStream(decompressDir + "/" + entryName));
                } catch (FileNotFoundException fileNotFoundException) {
                    fileNotFoundException.printStackTrace();
                }
                try {
                    bi = new BufferedInputStream(zf.getInputStream(ze2));
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                byte[] readContent = new byte[1024];
                int readCount = 0;
                try {
                    readCount = bi.read(readContent);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                while (readCount != -1) {
                    try {
                        bos.write(readContent, 0, readCount);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    try {
                        readCount = bi.read(readContent);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
                try {
                    bos.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
        try {
            zf.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

第一个参数是解压的目标文件路径,第二个参数是解压后的文件放在哪里。

jar包的下载地址在文章开始前的超链接上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值