2016年02月04日 代码异常 浏览(576) 收藏

java解压zip文件报异常:at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:293)

解压文件时报异常如下:

java.lang.IllegalArgumentException
    at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:293)
    at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:247)
    at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:74)


最佳回答
使用JDK的解压zip的API会报这个异常,推荐你使用ant-1.7.0.jar。使用方式如下:

public static List<String> browseZipFiles(String zipfilePath) throws IOException {
        ZipFile zipFile = new ZipFile(zipfilePath,"gbk"); 
        Enumeration<?> emu = zipFile.getEntries(); 
        ZipEntry entry = null; 
        List<String> zList = new ArrayList<String>();
        while (emu.hasMoreElements()) { 
            entry = (org.apache.tools.zip.ZipEntry) emu.nextElement();
            entry.setUnixMode(644);
            if (entry.getName().startsWith(".") || entry.isDirectory()) { 
                continue; 
            } 
            zList.add(entry.getName());
        } 
        zipFile.close(); 
        return zList;
    }
感谢散钓风光的贡献!
赞一下 0 人赞了本文