Magicode logo
Magicode
0
9 min read

zlibで文字列を圧縮

https://cdn.apollon.ai/media/notebox/anger-g679818700_1280.jpg
メッセージなどを圧縮したいときがあったりなかったりするかと思います。
そんなときに気軽に圧縮できるとよいかなと思って書いてみました。
zlib = require "zlib"

deflate = (pt)-> new Promise (f,r)->
  zlib.deflate pt, (e,d)->
    if e?
      r e
    else
      f d.toString("hex")

inflate = (comp)-> new Promise (f,r)->
  zlib.inflate Buffer.from(comp, "hex"), (e,d)->
    if e?
      r e
    else
      f d.toString()
こんな感じでどうでしょうか。
実際に圧縮できてみるか試しています。
ipsumはこちらを利用しました。
https://baconipsum.com/?paras=5&type=all-meat&start-with-lorem=1&make-it-spicy=1
文字を入れて、バイト数を算出します。
text = """
Spicy jalapeno bacon ipsum dolor amet kielbasa brisket pork belly buffalo jowl tail sausage jerky corned beef landjaeger venison. Chicken leberkas doner filet mignon, turducken chislic landjaeger burgdoggen. Boudin shoulder pig ham hock tail. Beef biltong picanha brisket ball tip jowl bacon.

Shoulder pancetta cow prosciutto. Shankle brisket chuck jerky swine beef kielbasa cupim ribeye short ribs turkey sausage rump buffalo. Meatloaf porchetta jerky pork chop ground round flank beef landjaeger alcatra frankfurter, brisket salami ball tip pork sausage. Alcatra rump jowl pork ribeye, frankfurter shoulder kielbasa ham hock filet mignon jerky boudin cow tongue.

Beef meatloaf pig boudin pork, chicken andouille. Filet mignon short loin capicola, doner cow tenderloin corned beef pork jowl bresaola flank frankfurter. Venison frankfurter tail, pork chop kielbasa swine burgdoggen andouille ribeye hamburger buffalo porchetta jowl boudin. T-bone pastrami alcatra shankle strip steak prosciutto tri-tip chuck frankfurter turkey landjaeger pork swine. Beef ground round porchetta doner fatback bresaola tongue ham hock jowl pancetta chuck salami.

Shank drumstick ball tip, tail picanha turducken pig beef ribs. Spare ribs t-bone chuck meatloaf alcatra tenderloin pig pastrami sirloin pork belly. Pork chop pastrami prosciutto pork belly ground round swine meatloaf, tongue picanha. Fatback cupim prosciutto landjaeger turducken, pastrami tail bacon jerky shoulder turkey. Fatback andouille sirloin turkey ball tip meatball frankfurter bacon alcatra beef ribs.

Biltong fatback kielbasa turducken pork belly. Pork chop kielbasa capicola ribeye, landjaeger pork shoulder burgdoggen pancetta. Fatback tongue turducken, kielbasa hamburger filet mignon jerky cow. Venison pork loin ball tip frankfurter t-bone biltong cow, strip steak pork short loin fatback. Doner chuck bresaola, beef meatloaf venison flank pork loin rump. Beef ball tip tongue, sausage tenderloin andouille swine pork belly chislic shoulder venison jerky.
"""

console.log Buffer.from(text).length
2022
改行とか考慮してませんがおおよそ2022バイトと。

圧縮

deflate text
.then (d)->
  console.log Buffer.from(d).length
  console.log d
1510
789c6d55c96edc300cbde72bf8018effa10b7a2b502045efb42cdb1a6b24434b82f9fb52a2b6417219602c99e4dbe8b74b8907dc50e3258d85058535a02e1fefb05a6d1de05d063895d40b7a84c5297fd283cbba1316a9f50396b86da82ddcec8786804a83c7e871977093ee7c80b0cec8952ecb0d349af58672970edea551de9a197e1c4a9cd280960bdd474f7d0d9d6f4a539fbbda8d351384e8d698af894379adc4586a896e5fedbe4baaf6ddc65519f0878d7aa5b34bed70e01d0e2bce3c1d5d49932c4a076b763a17688e0e6c414d28d4c570321df3cbcb5b2b8746c81090507dc0e5ac172a866067783bd09c5ab63ae2a0710b03fe4319c904342245bcd41d9c5ae443a6695d487f7c027aca47a3d0c5fb55199ee1b7c4a02d6e897e71e439b84396431cf682ddd96856e0df8d483a3f318f5a6070089ba3d32dba20ddd4e6f6e484bbea34e4ca659a19be9557f35899a17cce30a6b16257a0416e328cd216000bcb96584db24449a4679dee0d3209596ea59e533242360401b351694de3fd1a0b33a9daa6b248325b8d53f1566e230d4dc7c78343331ed6de498ff4526171c036c33f36ef13e064ae6950a2e12ef23793f689abfec44c3ace56e6300d02e75932f019febe2e04805ce8490592a94ae98bfbe83169e683c4737027d0d3d724269bf26968b6dbe00e163c8d5c92f2e4a83e57492906cac8d9c962f5bad66c92969a3c007b2cc72a31bb929b7c50a94a71ddc47ba466b3873f9b200d95b242a1bbd0c9921b66861b34d3547e06b15389c69f57e5615b6733fc690ab66b0395c3e27b228645ae8da7ca438140d62c4471ee878203f50de7d45b6722782b97655273c5d2f5cadd55155511b765394d97ff8c0ee0da95a84e2ee5afacc8aa7133f4a0c797bcf5255772d716c4279f5530433aaa593ab2c2e540cfb8534a72be582a14f31ed5dc2fd3d2f8780a02fba77e16e8d5e9394d65dcba520a2933fce48d927d575330318fcd86ef755de44dd227496bb47e8fea500c766a5f80c1ba83c2d96d8317eb57b1115a5b6626e6ff38f4ece4
短くなりましたね。

解凍

inflate d
.then (c)->
  console.log Buffer.from(c).length
  console.log c
2022
Spicy jalapeno bacon ipsum dolor amet kielbasa brisket pork belly buffalo jowl tail sausage jerky corned beef landjaeger venison. Chicken leberkas doner filet mignon, turducken chislic landjaeger burgdoggen. Boudin shoulder pig ham hock tail. Beef biltong picanha brisket ball tip jowl bacon.

Shoulder pancetta cow prosciutto. Shankle brisket chuck jerky swine beef kielbasa cupim ribeye short ribs turkey sausage rump buffalo. Meatloaf porchetta jerky pork chop ground round flank beef landjaeger alcatra frankfurter, brisket salami ball tip pork sausage. Alcatra rump jowl pork ribeye, frankfurter shoulder kielbasa ham hock filet mignon jerky boudin cow tongue.

Beef meatloaf pig boudin pork, chicken andouille. Filet mignon short loin capicola, doner cow tenderloin corned beef pork jowl bresaola flank frankfurter. Venison frankfurter tail, pork chop kielbasa swine burgdoggen andouille ribeye hamburger buffalo porchetta jowl boudin. T-bone pastrami alcatra shankle strip steak prosciutto tri-tip chuck frankfurter turkey landjaeger pork swine. Beef ground round porchetta doner fatback bresaola tongue ham hock jowl pancetta chuck salami.

Shank drumstick ball tip, tail picanha turducken pig beef ribs. Spare ribs t-bone chuck meatloaf alcatra tenderloin pig pastrami sirloin pork belly. Pork chop pastrami prosciutto pork belly ground round swine meatloaf, tongue picanha. Fatback cupim prosciutto landjaeger turducken, pastrami tail bacon jerky shoulder turkey. Fatback andouille sirloin turkey ball tip meatball frankfurter bacon alcatra beef ribs.

Biltong fatback kielbasa turducken pork belly. Pork chop kielbasa capicola ribeye, landjaeger pork shoulder burgdoggen pancetta. Fatback tongue turducken, kielbasa hamburger filet mignon jerky cow. Venison pork loin ball tip frankfurter t-bone biltong cow, strip steak pork short loin fatback. Doner chuck bresaola, beef meatloaf venison flank pork loin rump. Beef ball tip tongue, sausage tenderloin andouille swine pork belly chislic shoulder venison jerky.
もどせました。
次回はこの圧縮解凍を利用して暗号化を試します。

Discussion

コメントにはログインが必要です。