开发者

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


     开发者_如何学Go           } 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜