简体   繁体   中英

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

        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

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. Why was winrar able to do this, but the java code was not?

zipArchive

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. 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.

can you please let me which jdk version?

Will u be able to sent me the zip file, I can try to simulate.

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());
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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