Ver Fonte

feat: unzip

tongmengxiao há 3 meses atrás
pai
commit
6ef61d92a1

+ 45 - 0
baseswago/src/main/java/com/swago/baseswago/util/FileUtil.kt

@@ -6,6 +6,7 @@ import java.lang.Exception
 import java.util.*
 import java.util.zip.ZipEntry
 import java.util.zip.ZipFile
+import java.util.zip.ZipInputStream
 
 /**
  *@date 2022/2/12 11:44
@@ -80,7 +81,51 @@ object FileUtil {
 
 
     }
+    fun upZipFile(zipInputStream: InputStream, zipUpPath: String) {
+        val buf = ByteArray(1024)
+        val zis = ZipInputStream(BufferedInputStream(zipInputStream))
 
+        try {
+            val zipUpFile = File(zipUpPath)
+            if (!zipUpFile.exists()) {
+                zipUpFile.mkdirs()
+            }
+
+            var ze: ZipEntry? = zis.nextEntry
+            while (ze != null) {
+                if (ze.isDirectory) {
+                    val tempPath = ze.name
+                    var dirstr = zipUpPath + tempPath
+                    dirstr = String(dirstr.toByteArray(charset("8859_1")))
+                    val f = File(dirstr)
+                    f.mkdir()
+                } else {
+                    val tempPath = ze.name
+                    val os: OutputStream = BufferedOutputStream(FileOutputStream(getRealFileName(zipUpPath, tempPath)))
+                    var readLen: Int
+                    try {
+                        while (zis.read(buf).also { readLen = it } != -1) {
+                            os.write(buf, 0, readLen)
+                        }
+                    } catch (e: Exception) {
+                        e.printStackTrace()
+                    } finally {
+                        try {
+                            os.close()
+                        } catch (e: IOException) {
+                            e.printStackTrace()
+                        }
+                    }
+                }
+                ze = zis.nextEntry
+            }
+            zis.closeEntry()
+            zis.close()
+            zipInputStream.close()
+        } catch (e: Exception) {
+            e.printStackTrace()
+        }
+    }
 
     private fun getRealFileName(baseDir: String, absFileName: String): File {
         val dirs = absFileName.split("/").toTypedArray()