Java Uzip文件夹递归-android

Java Uzip文件夹递归-android,java,android,zip,unzip,Java,Android,Zip,Unzip,我尝试解压缩一个压缩文件(这个文件包含许多子文件夹和文件) 每次我都会收到一个错误,说: No such file or directory. 关于这一点,我搜索了很多,比如: 但是,什么都帮不了我 以下是我尝试过的: public class UnZipper { private static final String TAG = "UnZip"; private String mFileName, mDestinationPath; public UnZ

我尝试解压缩一个压缩文件(这个文件包含许多子文件夹和文件)

每次我都会收到一个错误,说:

No such file or directory.
关于这一点,我搜索了很多,比如:

  • 但是,什么都帮不了我

    以下是我尝试过的:

    public class UnZipper {
    
        private static final String TAG = "UnZip";
        private String mFileName, mDestinationPath;
    
        public UnZipper(String fileName, String destinationPath) {
            mFileName = fileName;
            mDestinationPath = destinationPath;
        }
    
        public String getFileName() {
            return mFileName;
        }
    
        public String getDestinationPath() {
            return mDestinationPath;
        }
    
        // shrikant
        public void unzip() {
            String fullPath = mFileName;
            Log.d(TAG, "unzipping " + mFileName + " to " + mDestinationPath);
            doInBackground(fullPath, mDestinationPath);
        }
    
        // shrikant: I have changed return type from Boolean to boolean.
        protected boolean doInBackground(String filePath, String destinationPath) {
    
            File archive = new File(filePath);
            boolean returnValue = false;
            try {
                ZipFile zipfile = new ZipFile(archive);
    
                for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
    
                    ZipEntry entry = (ZipEntry) e.nextElement();
                    try {
                        unzipEntry(zipfile, entry, destinationPath);
                        Log.d("Unzipped", entry.getName());
                        returnValue = true;
                    } catch (Exception ex) {
                        Log.e(TAG,
                                "Error while extracting file: " + entry
                                        + ex.getMessage());
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, "Error while extracting file " + archive, e);
                // return false;
            }
            return returnValue;
        }
    
        // shrikant: I have changed return type from void to boolean.
        /**
         * Unzips the zipped file into outputDir path.
         * 
         * @param zipfile
         * @param entry
         * @param outputDir
         * @throws IOException
         */
        private void unzipEntry(ZipFile zipfile, ZipEntry entry, String outputDir)
                throws IOException {
            Log.d("CURRENT ZIP", entry.getName());
            String _dir = null, fileName = null;
            if (entry.getName().contains("\\")) {
    
                _dir = entry.getName().substring(0, entry.getName().indexOf('\\'));
                createDir(new File(outputDir, _dir));
                fileName = entry.getName().substring(entry.getName().indexOf('\\'));
            }
    
            // Change by Prashant : To Remove "/" from file Name Date : 5/01/2011
            if (fileName.toString().startsWith("\\")) {
                fileName = fileName.substring(1); // End
            }
    
            if (_dir != "")
                outputDir = outputDir + "/" + _dir;
    
            File outputFile = new File(outputDir, fileName);
            if (!outputFile.getParentFile().exists()) {
                createDir(outputFile.getParentFile());
            }
            Log.d("OUTPUT FILE", outputDir + fileName);
            Log.v(TAG, "Extracting: " + entry);
    
            Log.d("FOUND inside unzipEntry()", entry.getName());
    
            BufferedInputStream inputStream = new BufferedInputStream(
                    zipfile.getInputStream(entry));
    
    // **here I am getting error.**
    
            BufferedOutputStream outputStream = new BufferedOutputStream(
                    new FileOutputStream(outputFile));
    
    // **above line.**
    
            try {
                copy(inputStream, outputStream);
            } finally {
                outputStream.close();
                inputStream.close();
            }
    
        }
    
        private void createDir(File dir) {
            if (dir.exists()) {
                return;
            }
            Log.v(TAG, "Creating dir " + dir.getName());
            if (!dir.mkdirs()) {
                throw new RuntimeException("Cannot create dir " + dir);
            }
        }
    
        private void copy(BufferedInputStream input, BufferedOutputStream output)
                throws IOException {
            byte[] buffer = new byte[4096];
            int size;
            while ((size = input.read(buffer)) != -1)
                output.write(buffer, 0, size);
        }
    
    }
    
    我的问题是:(请看代码)

    当我调用
    unzipEntry()
    时,当它遇到子文件夹时,它会传递类似“
    /data/abc.ext
    ”的内容,但我的文件系统不包含任何名为“data”的文件夹,我甚至尝试创建它,但失败了

    那么,如何创建从压缩文件到目标路径的子文件夹

    我甚至尝试过以下方法:

    if(entry.isDirectory) {
    // create directory
    }
    
    但这不会被调用,因为
    unzipEntry()
    (请在
    中查找()循环
    )直接传递子文件夹下的文件

    请帮我做这个


    谢谢。

    哦,是的!我已经解决了……)

    我已经编写了以下函数,如果需要,它将递归地创建子文件夹

    这是一个经过尝试和测试的函数,可以成功地解压缩任何文件

        /**
         * Unzips the file (recursively creates sub-folder if exists.)
         * 
         * @param tempFileName
         *            The zip file.
         * @param destinationPath
         *            The destination path where unzipped file will be saved.
         */
        public void unzipFile(String tempFileName, String destinationPath) {
            try {
    
                int index = destinationPath.lastIndexOf("\\");
                String fileString = destinationPath.substring(index);
    
                File extFile = new File("/mnt/sdcard/courses1", fileString);
                if(!extFile.exists()) {
                    createDir(extFile);
                }
    
                byte[] buffer = new byte[1024];
    
                FileInputStream fin = new FileInputStream(tempFileName);
                ZipInputStream zin = new ZipInputStream(fin);
                ZipEntry zipentry = null;
                if (!(zin.available() == 0)) {
                    byte[] startBuffer = new byte[8];
    
                    while ((zipentry = zin.getNextEntry()) != null) {
                        String zipName = zipentry.getName();
                        if (zipName.startsWith("/")) {
                            zipName = zipentry.getName();
                        } else if (zipName.startsWith("\\")) {
                            zipName = zipentry.getName();
                        } else {
                            zipName = "/" + zipentry.getName();
                        }
    
                        String fileName = destinationPath + zipName;
                        fileName = fileName.replace("\\", "/");
                        fileName = fileName.replace("//", "/");
    
                        if (zipentry.isDirectory()) {
                            createDir(new File(fileName));
                            continue;
                        }
    
                        String name = zipentry.getName();
                        int start, end = 0;
                        while (true) {
    
                            start = name.indexOf('\\', end);
                            end = name.indexOf('\\', start + 1);
                            if (start > 0)
                                "check".toString();
                            if (end > start && end > -1 && start > -1) {
                                String dir = name.substring(1, end);
    
                                createDir(new File(destinationPath + '/' + dir));
                                // name = name.substring(end);
                            } else
                                break;
                        }
    
                        File file = new File(fileName);
    
                        FileOutputStream tempDexOut = new FileOutputStream(file);
                        int BytesRead = 0;
    
                        if (zipentry != null) {
                            if (zin != null) {
                                while ((BytesRead = zin.read(buffer)) != -1) {
                                    tempDexOut.write(buffer, 0, BytesRead);
                                }
                                tempDexOut.close();
                            }
                        }
                    }
                }
    
            } catch (Exception e) {
                Log.e("Exception", e.getMessage());
            }
        }
    
    我希望它能帮助别人


    谢谢。

    您是否有权限写入清单中设置的SD卡?MDESTIONPATH的值是多少?MDESTIONPATH包含将保存解压缩文件的文件路径。我已经找到了解决方案。我已经更新了我的答案。非常感谢你的关心。
        /**
         * Unzips the file (recursively creates sub-folder if exists.)
         * 
         * @param tempFileName
         *            The zip file.
         * @param destinationPath
         *            The destination path where unzipped file will be saved.
         */
        public void unzipFile(String tempFileName, String destinationPath) {
            try {
    
                int index = destinationPath.lastIndexOf("\\");
                String fileString = destinationPath.substring(index);
    
                File extFile = new File("/mnt/sdcard/courses1", fileString);
                if(!extFile.exists()) {
                    createDir(extFile);
                }
    
                byte[] buffer = new byte[1024];
    
                FileInputStream fin = new FileInputStream(tempFileName);
                ZipInputStream zin = new ZipInputStream(fin);
                ZipEntry zipentry = null;
                if (!(zin.available() == 0)) {
                    byte[] startBuffer = new byte[8];
    
                    while ((zipentry = zin.getNextEntry()) != null) {
                        String zipName = zipentry.getName();
                        if (zipName.startsWith("/")) {
                            zipName = zipentry.getName();
                        } else if (zipName.startsWith("\\")) {
                            zipName = zipentry.getName();
                        } else {
                            zipName = "/" + zipentry.getName();
                        }
    
                        String fileName = destinationPath + zipName;
                        fileName = fileName.replace("\\", "/");
                        fileName = fileName.replace("//", "/");
    
                        if (zipentry.isDirectory()) {
                            createDir(new File(fileName));
                            continue;
                        }
    
                        String name = zipentry.getName();
                        int start, end = 0;
                        while (true) {
    
                            start = name.indexOf('\\', end);
                            end = name.indexOf('\\', start + 1);
                            if (start > 0)
                                "check".toString();
                            if (end > start && end > -1 && start > -1) {
                                String dir = name.substring(1, end);
    
                                createDir(new File(destinationPath + '/' + dir));
                                // name = name.substring(end);
                            } else
                                break;
                        }
    
                        File file = new File(fileName);
    
                        FileOutputStream tempDexOut = new FileOutputStream(file);
                        int BytesRead = 0;
    
                        if (zipentry != null) {
                            if (zin != null) {
                                while ((BytesRead = zin.read(buffer)) != -1) {
                                    tempDexOut.write(buffer, 0, BytesRead);
                                }
                                tempDexOut.close();
                            }
                        }
                    }
                }
    
            } catch (Exception e) {
                Log.e("Exception", e.getMessage());
            }
        }