zip, unzip

2019. 12. 20. 15:40Kotlin

반응형
fun unZip(zipFile: File, targetPath: String) {
    val zip = ZipFile(zipFile, "euc-kr") // UTF-8
    zip.entries.forEach {
        if (!it.isDirectory)
            BufferedInputStream(zip.getInputStream(it)).use { bis ->
                File(targetPath, it.name).outputStream().buffered(1024).use { bis.copyTo(it) }
            }
    }
}
fun zip(path: String){
        val folderToZip = File(path)
        var out: ZipOutputStream? = null
        try {
            out = ZipOutputStream(
                     BufferedOutputStream(
                        FileOutputStream(
                            path.plus(File.separator).plus(ZIP_FILE_NAME)
                        )
                     )
                  )
            val files = folderToZip.listFiles()
            for (file in files) {
                if(!file.name.endsWith("zip")){
                    val origin = BufferedInputStream(FileInputStream(file))
                    origin.use {
                        val entryName = file.path.substring(folderToZip.absolutePath.length)
                        out.putNextEntry(ZipEntry(entryName))
                        origin.copyTo(out, 1024)
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            out?.close()
        }
}
반응형

'Kotlin' 카테고리의 다른 글

file.copyTo  (0) 2019.12.20
Bitmap to File  (0) 2019.12.20
Bitmap in putExtra  (0) 2019.12.20
BitmapFromAsset  (0) 2019.12.20
Snackbar  (0) 2019.12.20