简体   繁体   English

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

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

I read quite a few articles, but I did not find a similar problem and its solution.我阅读了很多文章,但我没有找到类似的问题及其解决方案。

I'm try to read all files and some skipped with method zis.getNextEntry我正在尝试使用 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());
            }
        }
}

But if you unzip with WinRar, for example, everything will be unzipped correctly但是如果你用 WinRar 解压,例如,一切都会正确解压

Archive files存档文件

After running the program运行程序后

Or how i can see why some files doesn't read?或者我怎么能看到为什么有些文件无法读取?

Can the archive be broken?存档可以破坏吗?

After I unzipped and re-zipped the files by using winrar, the program worked correctly.在我使用 winrar 解压缩并重新压缩文件后,程序运行正常。 Why was winrar able to do this, but the java code was not?为什么 winrar 能够做到这一点,而 java 代码却不行?

zipArchive zip存档

jdk1.8.0_161 jdk1.8.0_161

Based on the test i did i able to print each directory and file name correctly.根据我所做的测试,我能够正确打印每个目录和文件名。

There 2 scenario came to my mind: i) the filename length or the complete length is more what the platform can handle.我想到了两种情况:i)文件名长度或完整长度更多的是平台可以处理的。 But this also should be same case while do unzip from winrar ii) Was there any permission issue, but again it won't be selective manner.但这也应该是相同的情况,同时从 winrar 解压缩 ii) 是否有任何权限问题,但同样不会是选择性的方式。

can you please let me which jdk version?你能告诉我哪个jdk版本吗?

Will u be able to sent me the zip file, I can try to simulate.你能把 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