IOException : Request to write 'n' bytes exceeds size in header of 0 bytes for entry 'filename' 파일/디렉토리 압축하는 동작을 구현하는 중 확인된 ...

[안드로이드] Request to write bytes exceeds size in header of 0 bytes for entry 해결방법

IOException : Request to write 'n' bytes exceeds size in header of 0 bytes for entry 'filename'

파일/디렉토리 압축하는 동작을 구현하는 중 확인된 에러입니다.

위와 같은 에러를 확인했을 때 해결 방법을 정리하려고 합니다.

 

 

 

 

 

 

 

1. 원인

tar 아카이버로 파일/디렉토리를 묶어 주는 동작을 하는 중 아래 빨간 박스 부분에서 확인된 에러입니다.

private fun tar(files: Array<File>, destPath: String): File {
try {
val tarArchiveOutputStream = TarArchiveOutputStream(FileOutputStream(destPath + EXTENSION_TAR))

for (file in files) {
if (!file.exists()) {
Log.e(TAG, "[tar] file is not exists : ${file.name}")
continue
}
Log.d(TAG, "[tar] file : ${file.name}")

if (file.isDirectory) {
file.listFiles()?.let {
tarDirectory(tarArchiveOutputStream, it, "/${file.name}/")
}
} else {
val fileInputStream = FileInputStream(file)
val entry = TarArchiveEntry(file.name)

tarArchiveOutputStream.putArchiveEntry(entry)
fileInputStream.copyTo(tarArchiveOutputStream, BUFFER_SIZE) // Error Occurred

tarArchiveOutputStream.closeArchiveEntry()
fileInputStream.close()
}
}
tarArchiveOutputStream.finish()
} catch (e: Exception) {
e.printStackTrace()
}
return File(destPath + EXTENSION_TAR)
}

 

해당 에러는 지정된 entry 파일에 쓰려고 하는데 entry의 사이즈가 정의되지 않았기 때문에 (Default : 0)

파일 쓰기 동작에서 에러가 발생한 것으로 확인되었습니다.

(zip 할 때는 따로 entry 사이즈 정해주지 않아도 문제없었는데 참.. ㅠㅠ)

 

 

 

 

 

 

2. 해결 방법

entry에 내가 쓰려고 하는 파일 크기만큼 사이즈를 할당해 줍니다.

에러도 사라지고 파일 쓰기도 잘되네요..!

val fileInputStream = FileInputStream(file)
val entry = TarArchiveEntry(file.name).apply {
size = file.length() // size 할당
}

tarArchiveOutputStream.putArchiveEntry(entry)
fileInputStream.copyTo(tarArchiveOutputStream, BUFFER_SIZE)

tarArchiveOutputStream.closeArchiveEntry()
fileInputStream.close()

 

 

 

 

 

 

 

 

[Reference]

Stackoverflow

 

 

 

0 comments: