Получение обратной косой черты при распаковке папки, содержащей файлы, такие как: /sdcard/folder/subfolder\file.xml в Android - PullRequest
0 голосов
/ 18 декабря 2018

Моя папка Zipped содержит подпапку с файлами, но при ее извлечении я не могу достичь той же иерархии.Я получаю распакованную структуру следующим образом: -

/ storage / emulated / 0 / unzipped_folder / sub_folder \ main.png /storage/emulated/0/unzipped_folder/sub_folder\test.xml

Таким образом, при извлечении я не могу получить sub_folder в качестве каталога.При извлечении zip-файла я использую приведенный ниже код.

 public static void unzip(String zipFile, String location) throws IOException {
        try {
            File f = new File(location);
            if (!f.isDirectory()) {
                f.mkdirs();
            }
            ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
            try {
                ZipEntry ze = null;
                while ((ze = zin.getNextEntry()) != null) {
                    String path = location + File.separator + ze.getName();
                    if (ze.isDirectory()) {
                        File unzipFile = new File(path);
                        if (!unzipFile.isDirectory()) {
                            unzipFile.mkdirs();
                        }
                    } else {
                        FileOutputStream fout = new FileOutputStream(path, false);
                        try {
                            for (int c = zin.read(); c != -1; c = zin.read()) {
                                fout.write(c);
                            }
                            zin.closeEntry();
                        } finally {
                            fout.close();
                        }
                    }
                }
            } finally {
                zin.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("ZIP STU", "Unzip exception", e);
        }
    }

Пожалуйста, помогите, я застрял в этом более чем на 2 дня.Спасибо!

1 Ответ

0 голосов
/ 19 декабря 2018

Наконец-то я смог решить эту проблему, используя приведенный ниже код.

public static void unzipEPub(File zipFile, File destinationDir){
    ZipFile zip = null;
    try {
        int DEFUALT_BUFFER = 1024;
        destinationDir.mkdirs();
        zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = zipFileEntries.nextElement();
            String entryName = entry.getName();
            entryName = entryName.replace("\\","/");
            File destFile = new File(destinationDir, entryName);
            File destinationParent = destFile.getParentFile();
            if (destinationParent != null && !destinationParent.exists()) {
                destinationParent.mkdirs();
            }
            if (!entry.isDirectory()) {
                BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
                int currentByte;
                byte data[] = new byte[DEFUALT_BUFFER];
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos, DEFUALT_BUFFER);
                while ((currentByte = is.read(data, 0, DEFUALT_BUFFER)) > 0) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                is.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (IOException ignored) {
            }
        }
    }
}
...