简体   繁体   中英

Java - Unzipping files compressed differently

Creating this application has become a pain in the ass! Using Java I want to unzip .zip files created by many different applications: Using my 7-zip this works perfectly fine, Using somebodys winrar to compress the files completely messes them up! Here's my code:

 public static void ExtractModZip(File Zip, File Dest) {
        try {
            if (Zip.getName().toLowerCase().endsWith(".zip")) {
            }
            ZipFile zip = new ZipFile(Zip);
            System.out.println(zip.getName() + " opened.");
            Enumeration entries = zip.entries();
            String ModName = Zip.getName().substring(0, Zip.getName().length() - 4);
            File base = new File(Dest + File.separator + ModName);
            base.mkdirs();
            InputStream entryStream = null;
            FileOutputStream fos = null;
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                entryStream = zip.getInputStream(entry);
                String entryName = entry.getName().replace('/', File.separatorChar);
                entryName = entryName.replace('\\', File.separatorChar);


                if (!entry.isDirectory()) {
                    File file = new File(base + File.separator + entryName);
                    File Base = new File(base + File.separator);
                    if (!Base.exists()) {
                        Base.mkdirs();
                    }

                    fos = new FileOutputStream(file);
                    try {
                        // Allocate a buffer for reading the entry data.
                        byte[] buffer = new byte[1024];
                        int bytesRead;
                        // Read the entry data and write it to the output file.
                        while ((bytesRead = entryStream.read(buffer)) != -1) {
                            fos.write(buffer, 0, bytesRead);
                        }
                        System.out.println(entry.getName() + " extracted.");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                } else {
                    File file = new File(base + File.separator + entryName);
                    file.mkdir();
                }
            }
            fos.close();
            entryStream.close();
        } catch (ZipException ex) {
            Logger.getLogger(fileUtils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(fileUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Example: I unzipped the fie using this method, it completely missed out a folder and certain files inside...

Try a different unzip (decompression) implementation. TrueZIP is well known.

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