package com.base.superfilemanager.helps.file

import java.io.File
import java.util.Stack

object FileEx {
    /**
     *删除文件夹
     */
    fun deleteDirectory(dir: File): Boolean {
        val stackFiles = Stack<File>()
        stackFiles.push(dir)
        while (stackFiles.size > 0) {
            val currentFile = stackFiles.peek()
            val subFiles = currentFile.listFiles()

            if (subFiles != null) {
                for (i in subFiles.indices) {
                    if (subFiles[i].isFile) {
                        if (!subFiles[i].delete()) {
                            return false
                        }
                    } else {
                        stackFiles.push(subFiles[i])
                    }
                }
            }
            if (currentFile === stackFiles.peek()) {
                if (!currentFile.delete()) {
                    return false
                }
                stackFiles.pop()
            }
        }
        return true
    }

}