Java 从 ZipEntry 对象中读取指定文件

/**
     * 从zip文件中,读取指定的压缩内容
     * @param zipFile zip压缩文件
     * @param fileName 需要读取的文件名
     * @param outTxtFile 输出文件
     */
    private static void readFileFromZipEntry(File zipFile, String fileName, File outTxtFile) {
        try (ZipInputStream zipIs = new ZipInputStream(Files.newInputStream(zipFile.toPath()), 
                Charset.forName("GBK")); FileOutputStream fw = new FileOutputStream(outTxtFile)) {
            ZipEntry zipEntry = null;
            while ((zipEntry = zipIs.getNextEntry()) != null) {
                if (zipEntry.getName().contains(fileName)) {
                    byte[] bytes = new byte[1024];
                    int len = -1;
                    while ((len = zipIs.read(bytes, 0, bytes.length)) != -1) {
                        fw.write(bytes,0,len);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
posted @ 2023-05-20 17:44  log996  阅读(1310)  评论(0)    收藏  举报