在解压缩包含以下文件的文件夹时获取反斜杠:android 中的 /sdcard/folder/subfolder\file.xml

Getting backward slash while unzip folder containing files like: /sdcard/folder/subfolder\file.xml in android

我的 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 天了。 谢谢!

最后我可以使用下面的代码解决这个问题。

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