Java - распаковка файлов, сжатых по-разному

Создание этого приложения стало занозой в заднице! Используя Java, я хочу разархивировать файлы .zip, созданные многими различными приложениями: с помощью моего 7-zip это работает отлично, использование чьего-то winrar для сжатия файлов полностью их портит! Вот мой код:

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

Пример: я разархивировал файл с помощью этого метода, он полностью пропустил папку и некоторые файлы внутри...


person Thomas Nairn    schedule 22.01.2011    source источник


Ответы (1)


Попробуйте другую реализацию распаковки (декомпрессии). TrueZIP хорошо известен.

person Uriah Carpenter    schedule 23.01.2011