forked from openrs2/openrs2
parent
29b63b613a
commit
f5ae71932d
@ -0,0 +1,31 @@ |
|||||||
|
package dev.openrs2.compress.cli.lzma |
||||||
|
|
||||||
|
import com.github.ajalt.clikt.core.CliktCommand |
||||||
|
import com.github.ajalt.clikt.parameters.options.default |
||||||
|
import com.github.ajalt.clikt.parameters.options.option |
||||||
|
import com.github.ajalt.clikt.parameters.options.validate |
||||||
|
import com.github.ajalt.clikt.parameters.types.defaultStdin |
||||||
|
import com.github.ajalt.clikt.parameters.types.defaultStdout |
||||||
|
import com.github.ajalt.clikt.parameters.types.inputStream |
||||||
|
import com.github.ajalt.clikt.parameters.types.int |
||||||
|
import com.github.ajalt.clikt.parameters.types.outputStream |
||||||
|
import dev.openrs2.compress.lzma.Lzma |
||||||
|
import org.tukaani.xz.LZMA2Options |
||||||
|
|
||||||
|
class LzmaCommand : CliktCommand(name = "lzma") { |
||||||
|
private val input by option().inputStream().defaultStdin() |
||||||
|
private val output by option().outputStream().defaultStdout() |
||||||
|
private val level by option().int().default(LZMA2Options.PRESET_MAX).validate { |
||||||
|
require(it >= LZMA2Options.PRESET_MIN && it <= LZMA2Options.PRESET_MAX) { |
||||||
|
"--level must be between ${LZMA2Options.PRESET_MIN} and ${LZMA2Options.PRESET_MAX} inclusive" |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun run() { |
||||||
|
input.use { input -> |
||||||
|
Lzma.createHeaderlessOutputStream(output, LZMA2Options(level)).use { output -> |
||||||
|
System.err.println(input.copyTo(output)) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,25 @@ |
|||||||
|
package dev.openrs2.compress.cli.lzma |
||||||
|
|
||||||
|
import com.github.ajalt.clikt.core.CliktCommand |
||||||
|
import com.github.ajalt.clikt.parameters.options.option |
||||||
|
import com.github.ajalt.clikt.parameters.options.required |
||||||
|
import com.github.ajalt.clikt.parameters.types.defaultStdin |
||||||
|
import com.github.ajalt.clikt.parameters.types.defaultStdout |
||||||
|
import com.github.ajalt.clikt.parameters.types.inputStream |
||||||
|
import com.github.ajalt.clikt.parameters.types.long |
||||||
|
import com.github.ajalt.clikt.parameters.types.outputStream |
||||||
|
import dev.openrs2.compress.lzma.Lzma |
||||||
|
|
||||||
|
class UnlzmaCommand : CliktCommand(name = "unlzma") { |
||||||
|
private val input by option().inputStream().defaultStdin() |
||||||
|
private val length by option().long().required() |
||||||
|
private val output by option().outputStream().defaultStdout() |
||||||
|
|
||||||
|
override fun run() { |
||||||
|
Lzma.createHeaderlessInputStream(input, length).use { input -> |
||||||
|
output.use { output -> |
||||||
|
input.copyTo(output) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,28 @@ |
|||||||
|
package dev.openrs2.compress.lzma |
||||||
|
|
||||||
|
import com.google.common.io.LittleEndianDataInputStream |
||||||
|
import com.google.common.io.LittleEndianDataOutputStream |
||||||
|
import org.tukaani.xz.LZMA2Options |
||||||
|
import org.tukaani.xz.LZMAInputStream |
||||||
|
import org.tukaani.xz.LZMAOutputStream |
||||||
|
import java.io.InputStream |
||||||
|
import java.io.OutputStream |
||||||
|
|
||||||
|
object Lzma { |
||||||
|
fun createHeaderlessInputStream(input: InputStream, length: Long): InputStream { |
||||||
|
val headerInput = LittleEndianDataInputStream(input) |
||||||
|
|
||||||
|
val properties = headerInput.readByte() |
||||||
|
val dictionarySize = headerInput.readInt() |
||||||
|
|
||||||
|
return LZMAInputStream(input, length, properties, dictionarySize) |
||||||
|
} |
||||||
|
|
||||||
|
fun createHeaderlessOutputStream(output: OutputStream, options: LZMA2Options): OutputStream { |
||||||
|
val headerOutput = LittleEndianDataOutputStream(output) |
||||||
|
headerOutput.writeByte((options.pb * 5 + options.lp) * 9 + options.lc) |
||||||
|
headerOutput.writeInt(options.dictSize) |
||||||
|
|
||||||
|
return LZMAOutputStream(output, options, false) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue