データの圧縮と展開
Posted feedbacks - Groovy
horiuchiさん(Java:#8274)を参考に させていただきました。 ありがとうございます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.zip.*
def str = (('a'..'z').join()+'\n')*10
byte[] origin = str.bytes // 圧縮前
byte[] comp = compress(origin) // 圧縮後
byte[] decomp = decompress(comp) // 展開後
assert origin.size() == decomp.size() // 圧縮前と展開後のサイズは同じ
println """\
---------------------------------------
${new String(origin)}
---------------------------------------
Original Size: ${origin.size()}bytes
Compressed Size: ${comp.size()}bytes
Decompressed Size: ${decomp.size()}bytes
---------------------------------------\
"""
/** 圧縮 */
byte[] compress(byte[] input) {
def output = new ByteArrayOutputStream()
def zos = new ZipOutputStream(output)
zos.putNextEntry(new ZipEntry(''))
zos << new ByteArrayInputStream(input)
zos.closeEntry()
zos.close()
output.toByteArray()
}
/** 展開 */
byte[] decompress(byte[] input) {
def zis = new ZipInputStream(new ByteArrayInputStream(input))
zis.getNextEntry()
def output = new ByteArrayOutputStream()
output << zis
zis.close()
output.toByteArray()
}
|

mattsan
#8262()
Rating1/5=0.20
データを圧縮するcompress、展開するdecompressという関数やメソッドなどを書いてください。データはバイト列でもストリームでもそれ以外の形式でもOKです。
圧縮形式は問いませんが、できるだけ一般的なフォーマット(zip,lzhなど)でお願いします。
また、標準以外のライブラリを使う場合には出典の記載をお願いします。
「○○でも実用的な圧縮/展開プログラムがかけるんだぞ!」というのを、ぜひ示してください。
[ reply ]