[kotlin] 압축된 파일을 풀어서 읽는 방법

Kotlin에서 압축 파일을 다루기 위해 먼저 java.util.zip 패키지의 ZipInputStream 클래스를 사용해야 합니다. ZipInputStream 클래스는 압축 파일을 읽고 압축을 해제하는 기능을 제공합니다.

아래는 압축 파일을 풀어서 읽는 Kotlin 코드의 예시입니다:

import java.io.File
import java.util.zip.ZipInputStream

fun extractZipFile(zipFilePath: String) {
    try {
        val zipFile = File(zipFilePath)
        val zipInputStream = ZipInputStream(zipFile.inputStream())
        var zipEntry = zipInputStream.nextEntry
        
        while (zipEntry != null) {
            val entryName = zipEntry.name
            
            // 압축 해제된 파일의 내용을 읽는 작업을 수행합니다.
            // 예시로 파일의 내용을 콘솔에 출력합니다.
            val buffer = ByteArray(1024)
            var bytesRead = zipInputStream.read(buffer)
            while (bytesRead != -1) {
                // 파일의 내용을 콘솔에 출력합니다.
                System.out.print(String(buffer, 0, bytesRead))
                bytesRead = zipInputStream.read(buffer)
            }
            
            zipEntry = zipInputStream.nextEntry
        }
        
        zipInputStream.closeEntry()
        zipInputStream.close()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun main() {
    val zipFilePath = "path/to/your/zip/file.zip"
    extractZipFile(zipFilePath)
}

위의 코드는 extractZipFile 함수를 정의하고, 해당 함수를 main 함수에서 호출하는 예시입니다. zipFilePath 변수에는 압축 파일의 경로를 지정하십시오.

실행 시, 해당 코드는 압축 파일을 열고 그 내용을 콘솔에 출력합니다. 실제로는 파일 내용을 읽어서 원하는 작업을 수행하면 됩니다.

이와 같은 방법을 사용하여 Kotlin에서 압축 파일을 풀어서 읽을 수 있습니다. 추가로 필요한 작업이 있을 경우, java.util.zip 패키지의 다른 클래스와 메서드를 참고하십시오.