JAVA ZipFile 解压zip 避免文件名中文乱码

String zipFilePath="C:\\Users\\Joue\\Desktop\\unzip.zip";

String destDir ="C:\\Users\\Joue\\Desktop\\unzip";

 ZipFile zipFile = new ZipFile(zipFilePath);   //此处可自动识别编码
        Enumeration<?> emu = zipFile.getEntries();
        BufferedInputStream bis;
        FileOutputStream fos;
        BufferedOutputStream bos;
        File file, parentFile;
        ZipEntry entry;
        byte[] cache = new byte[CACHE_SIZE];
        while (emu.hasMoreElements()) {
            entry = (ZipEntry) emu.nextElement();
            if (entry.isDirectory()) {
                File entryFile = new File(destDir + entry.getName());
                if (!entryFile.exists() && !entryFile.mkdirs()) {
                    throw new Exception("创建文件夹失败");
                }
                continue;
            }
            bis = new BufferedInputStream(zipFile.getInputStream(entry));
            file = new File(destDir + entry.getName());
            parentFile = file.getParentFile();
            if (parentFile != null && !parentFile.exists() && !parentFile.mkdirs()) {
                throw new Exception("文件夹创建失败");
            }
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos,1024);
            int nRead;
            while ((nRead = bis.read(cache, 0, 1024)) != -1) {
                fos.write(cache, 0, nRead);
            }
            bos.flush();
            IOUtils.closeQuietly(bos);
            IOUtils.closeQuietly(fos);
            IOUtils.closeQuietly(bis);
        }
        zipFile.close();

猜你喜欢

转载自blog.csdn.net/bingguang1993/article/details/86575343
今日推荐