圧縮ファイルの解凍に圧倒的に便利な「unar」コマンド

2017年10月14日

Linux サーバー テクノロジー

この間、腕時計のベルトをAmazonで注文したら、どでかいダンボールに入って納品されてきました。 腕時計のベルトって、10センチぐらいの箱に入った商品なんだけど、それを大きなダンボールでこれでもかというぐらいに梱包されていて、「過剰梱包」という言葉が頭をよぎりましたが、商品の発送者も、中身が壊れてはいけないと必死なんだという事も理解できました。 こんな時、パソコンのデータって、圧縮っていう技術があり、実際の商品にもこうした圧縮技術があれば、もっと便利になるな〜と考え、ドラえもんのスモールライトを思い出していました。 そんな夢の話は置いといて、最近WEBサービスを構築している時に、圧縮ファイルを扱う場面が出てきました。 ユーザーが好き放題に圧縮ファイルをアップロードしていて、それを解凍して中身を確認するという機能を持たなくてはいけなくて、機能設計での悩みとその解決方法をブログにします。

多数の圧縮ファイル

メジャーな圧縮タイプとして「zip」「rar」がありますが、サーバーサイドでよく利用される「tar」「gz」「tgz」などもストレージ内に混在していると思います。 また、最近では、「7z」が圧縮率が高いとしてダウンローダサイトで、流行っている傾向もあり、他にも色々なアーカイブファイルフォーマットが存在することを考えると、サーバー側で一括で対応するのが非常に困難だと考えられます。 zipファイルには「unzip」、rarファイルには「unrar」、tgzファイルには、「tar」というモジュールをそれぞれ使う事で解決はできそうですが、正直サービス設計をする際に煩雑になりがちなのが懸念されます。 WEBで検索していると「unar」というモジュールを見つける事ができました。 この「unar」というモジュールは色々なフォーマットの圧縮データをこのコマンドだけで解凍処理してくれる為、非常に便利に使えるのではないかという希望を込めて色々と検証してみました。

「unar」コマンド

とても便利そうに思えるunarモジュールですが、サーバーにこのモジュールをインストールすると「lsar」という圧縮データの中身を閲覧できるモジュールも同時にインストールされます。 ただし、unarは、圧縮ファイルを作り出すことはできないので、解凍のみのモジュールです。

インストール

# Debian/Ubuntu $ sudo apt-get install unar-free # CentOS $ sudo yum install unar # Mac $ brew install unar Windows用モジュールは無さそうですね。

解凍できるフォーマット一覧

zip, RAR, 7z, tar, gzip, bzip2, LZMA, XZ, CAB, MSI, NSIS, EXE, ISO, BIN, Stuffit X, DiskDouble, Compact Pro, Packit, cpio, compress (.Z), ARJ, ARC, PAK, ACE, ZOO, LZH, ADF, DMS, LZX, PowerPacker, LBR, Squeeze, Crunch, swf, deb, rpm

使い方サンプル

普通に解凍

$ unar %アーカイブデータ%

フォルダを指定して解凍

$ unar -o %保存先フォルダ% %アーカイブデータ%

アーカイブファイル名でフォルダを作成してその中に解凍

$ unar -d %アーカイブデータ% # フォルダを作らない場合 $ unar -D %アーカイブデータ%

パスワードを指定して解凍

$ unar -p %パスワード% %アーカイブデータ%

アーカイブデータの内容を表示

$ lsar %アーカイブデータ% # jsonフォーマットで出力 $ lsar -j %アーカイブデータ%

SWFファイルの内部メディア・コンテンツを取り出す

$ unar %SWFデータ%

注意点

アーカイブデータが日本語文字列を含んでいたり、圧縮データ内に日本語ファイルが存在している場合は、unarをインストールするOSのlocaleが日本語対応していないといけません。 マルチバイトでお困りの人は、以前書いた記事を参考にしてみてください。 UbuntuのCUIで日本語ファイルを使う方法

コマンドHELP

以下参考までに乗せておきます。 unar v1.9.1, a tool for extracting the contents of archive files. Usage: unar [options] archive [files ...] Available options: -output-directory (-o) <string> The directory to write the contents of the archive to. Defaults to the current directory. -force-overwrite (-f) Always overwrite files when a file to be unpacked already exists on disk. By default, the program asks the user if possible, otherwise skips the file. -force-rename (-r) Always rename files when a file to be unpacked already exists on disk. -force-skip (-s) Always skip files when a file to be unpacked already exists on disk. -force-directory (-d) Always create a containing directory for the contents of the unpacked archive. By default, a directory is created if there is more than one top-level file or folder. -no-directory (-D) Never create a containing directory for the contents of the unpacked archive. -password (-p) <string> The password to use for decrypting protected archives. -encoding (-e) <encoding name> The encoding to use for filenames in the archive, when it is not known. If not specified, the program attempts to auto-detect the encoding used. Use "help" or "list" as the argument to give a listing of all supported encodings. -password-encoding (-E) <name> The encoding to use for the password for the archive, when it is not known. If not specified, then either the encoding given by the -encoding option or the auto-detected encoding is used. -indexes (-i) Instead of specifying the files to unpack as filenames or wildcard patterns, specify them as indexes, as output by lsar. -no-recursion (-nr) Do not attempt to extract archives contained in other archives. For instance, when unpacking a .tar.gz file, only unpack the .gz file and not its contents. -copy-time (-t) Copy the file modification time from the archive file to the containing directory, if one is created. -forks (-k) <visible |hidden|skip> How to handle Mac OS resource forks. "visible" creates AppleDouble files with the extension ".rsrc", "hidden" creates AppleDouble files with the prefix "._", and "skip" discards all resource forks. Defaults to "visible". -quiet (-q) Run in quiet mode. -version (-v) Print version and exit. -help (-h) Display this information.

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。