Created
March 17, 2020 07:36
-
-
Save sin-tanaka/81a985c28deb7aa6257f4add812d29bc to your computer and use it in GitHub Desktop.
tar→brotliで圧縮してあるファイルをインメモリ上で解凍するサンプル お焚き上げ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
やったこと | |
- brotliの解凍 | |
- tarballの解凍 | |
- brotliで解凍したものをインメモリ上で保持してファイルとして扱い、更にtarballで展開する | |
背景 | |
- AWS Lambda上の/tmp領域にそこそこ大きめのbinaryを展開したかった | |
- Lambdaの/tmp領域の制限(500mぐらい)の関係でインメモリ上でやりたい(brotliで展開したやつを一回書き出すみたいなことをしたくない) | |
- IOのロス、展開後の容量とか気にしなくていい場合、brotliで解凍→普通にファイルをwrite→更にread→tarballで解凍のようにリッチにやるのも可 | |
前準備 | |
- pip install brotli | |
""" | |
import tarfile | |
import brotli | |
filepath = './foo.tar.br' | |
with open(filepath, 'rb') as f: | |
data = f.read() | |
decompressed_data = brotli.decompress(data) | |
# ByteIOにbinaryデータを渡すとfile like objectとして扱ってくれる | |
fileobj = io.BytesIO(decompressed_data) | |
with tarfile.open('./', fileobj=fileobj, mode='r:*') as tar: | |
tar.extractall() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment