Fix duplicate zip entry error
This commit is contained in:
@@ -4,6 +4,7 @@ import androidx.annotation.WorkerThread
|
|||||||
import androidx.collection.ArraySet
|
import androidx.collection.ArraySet
|
||||||
import okio.Closeable
|
import okio.Closeable
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.FileInputStream
|
||||||
import java.util.zip.Deflater
|
import java.util.zip.Deflater
|
||||||
import java.util.zip.ZipEntry
|
import java.util.zip.ZipEntry
|
||||||
import java.util.zip.ZipFile
|
import java.util.zip.ZipFile
|
||||||
@@ -20,24 +21,30 @@ class ZipOutput(
|
|||||||
setLevel(compressionLevel)
|
setLevel(compressionLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(name: String, file: File) {
|
@WorkerThread
|
||||||
entryNames.add(name)
|
fun put(name: String, file: File): Boolean {
|
||||||
output.appendFile(file, name)
|
return output.appendFile(file, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(name: String, content: String) {
|
@WorkerThread
|
||||||
entryNames.add(name)
|
fun put(name: String, content: String): Boolean {
|
||||||
output.appendText(content, name)
|
return output.appendText(content, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addDirectory(name: String) {
|
@WorkerThread
|
||||||
entryNames.add(name)
|
fun addDirectory(name: String): Boolean {
|
||||||
val entry = if (name.endsWith("/")) {
|
val entry = if (name.endsWith("/")) {
|
||||||
ZipEntry(name)
|
ZipEntry(name)
|
||||||
} else {
|
} else {
|
||||||
ZipEntry("$name/")
|
ZipEntry("$name/")
|
||||||
}
|
}
|
||||||
output.putNextEntry(entry)
|
return if (entryNames.add(entry.name)) {
|
||||||
|
output.putNextEntry(entry)
|
||||||
|
output.closeEntry()
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
@@ -66,4 +73,46 @@ class ZipOutput(
|
|||||||
isClosed = true
|
isClosed = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@WorkerThread
|
||||||
|
private fun ZipOutputStream.appendFile(fileToZip: File, name: String): Boolean {
|
||||||
|
if (fileToZip.isDirectory) {
|
||||||
|
val entry = if (name.endsWith("/")) {
|
||||||
|
ZipEntry(name)
|
||||||
|
} else {
|
||||||
|
ZipEntry("$name/")
|
||||||
|
}
|
||||||
|
if (!entryNames.add(entry.name)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
putNextEntry(entry)
|
||||||
|
closeEntry()
|
||||||
|
fileToZip.listFiles()?.forEach { childFile ->
|
||||||
|
appendFile(childFile, "$name/${childFile.name}")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
FileInputStream(fileToZip).use { fis ->
|
||||||
|
if (!entryNames.add(name)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val zipEntry = ZipEntry(name)
|
||||||
|
putNextEntry(zipEntry)
|
||||||
|
fis.copyTo(this)
|
||||||
|
closeEntry()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
@WorkerThread
|
||||||
|
private fun ZipOutputStream.appendText(content: String, name: String): Boolean {
|
||||||
|
if (!entryNames.add(name)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val zipEntry = ZipEntry(name)
|
||||||
|
putNextEntry(zipEntry)
|
||||||
|
content.byteInputStream().copyTo(this)
|
||||||
|
closeEntry()
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.core.zip
|
|
||||||
|
|
||||||
import androidx.annotation.WorkerThread
|
|
||||||
import java.io.File
|
|
||||||
import java.io.FileInputStream
|
|
||||||
import java.util.zip.ZipEntry
|
|
||||||
import java.util.zip.ZipOutputStream
|
|
||||||
|
|
||||||
@WorkerThread
|
|
||||||
fun ZipOutputStream.appendFile(fileToZip: File, name: String) {
|
|
||||||
if (fileToZip.isDirectory) {
|
|
||||||
if (name.endsWith("/")) {
|
|
||||||
putNextEntry(ZipEntry(name))
|
|
||||||
} else {
|
|
||||||
putNextEntry(ZipEntry("$name/"))
|
|
||||||
}
|
|
||||||
closeEntry()
|
|
||||||
fileToZip.listFiles()?.forEach { childFile ->
|
|
||||||
appendFile(childFile, "$name/${childFile.name}")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
FileInputStream(fileToZip).use { fis ->
|
|
||||||
val zipEntry = ZipEntry(name)
|
|
||||||
putNextEntry(zipEntry)
|
|
||||||
fis.copyTo(this)
|
|
||||||
closeEntry()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@WorkerThread
|
|
||||||
fun ZipOutputStream.appendText(content: String, name: String) {
|
|
||||||
val zipEntry = ZipEntry(name)
|
|
||||||
putNextEntry(zipEntry)
|
|
||||||
content.byteInputStream().copyTo(this)
|
|
||||||
closeEntry()
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user