繁体   English   中英

为什么我的程序在java.util.zip解压时会跳过文件?

[英]Why does my program skip files when unzipping by java.util.zip?

我阅读了很多文章,但我没有找到类似的问题及其解决方案。

我正在尝试使用 zis.getNextEntry 方法读取所有文件并跳过一些文件

        public static void main(String[] args) throws Exception {
            String fileZip = "src/main/resources/unzipTest/fias_xml.zip";
            ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                System.out.println(entry.getName());
            }
        }
}

但是如果你用 WinRar 解压,例如,一切都会正确解压

存档文件

运行程序后

或者我怎么能看到为什么有些文件无法读取?

存档可以破坏吗?

在我使用 winrar 解压缩并重新压缩文件后,程序运行正常。 为什么 winrar 能够做到这一点,而 java 代码却不行?

zip存档

jdk1.8.0_161

根据我所做的测试,我能够正确打印每个目录和文件名。

我想到了两种情况:i)文件名长度或完整长度更多的是平台可以处理的。 但这也应该是相同的情况,同时从 winrar 解压缩 ii) 是否有任何权限问题,但同样不会是选择性的方式。

你能告诉我哪个jdk版本吗?

你能把 zip 文件发给我吗,我可以试着模拟一下。

public void unzip(String zipFile, String destDir)
    {
        try
        {
            int BUFFER = 8*1024;
            File file = new File(zipFile);
            ZipFile zip = new ZipFile(file);
            String newPath = destDir;
            new File(newPath).mkdir();
            Enumeration zipFileEntries = zip.entries();
            while (zipFileEntries.hasMoreElements())
            {
                ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
                String currentEntry = entry.getName();
                File destFile = new File(newPath, currentEntry);
                File destinationParent = destFile.getParentFile();
                destinationParent.mkdirs();

                if (!entry.isDirectory())
                {
                    BufferedInputStream is = new BufferedInputStream(zip
                            .getInputStream(entry));
                    int currentByte;
                    byte[] data = new byte[BUFFER];
                    FileOutputStream fos = new FileOutputStream(destFile);
                    BufferedOutputStream dest = new BufferedOutputStream(fos,
                            BUFFER);
                    while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, currentByte);
                    }
                    dest.flush();
                    dest.close();
                    is.close();
                }
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM