LoginSignup
0
0

More than 5 years have passed since last update.

java側linuxのzipコマンドで圧縮

Last updated at Posted at 2019-05-27

java側linuxのzipコマンドを呼び出したい場合、ProcessBuilderを利用できます。
例1:
全部ディレクトリ保持の場合、
List commandList = new ArrayList();
commandList.add("-r");
ProcessBuilder pb =new ProcessBuilder(commandList);

例2:
ディレクトリ保持しない場合、(同じファイル名あれば、エラー)
List commandList = new ArrayList();
commandList.add("-j");
ProcessBuilder pb =new ProcessBuilder(commandList);
LINUXのzipコマンド一覧
-f freshen: only changed files
-u update: only changed or new files
-d delete entries in zipfile
-m move into zipfile (delete OS files)
-r recurse into directories
-j junk (don't record) directory names
-0 store only
-l convert LF to CR LF (-ll CR LF to LF)
-1 compress faster
-9 compress better
-q quiet operation
-v verbose operation/print version info
-c add one-line comments
-z add zipfile comment
-@ read names from stdin
-o make zipfile as old as latest entry
-x exclude the following names
-i include only the following names
-F fix zipfile (-FF try harder)
-D do not add directory entries
-A adjust self-extracting exe
-J junk zipfile prefix (unzipsfx)
-T test zipfile integrity
-X eXclude eXtra file attributes
-y store symbolic links as the link instead of the referenced file
-e encrypt
-n don't compress these suffixes

例3下記の場合はどうする?
フォルダの形式:/home/AB/CD/+各種ファイルとサブフォルダ
入力パース
/home/AB/CD/
圧縮後(/home/AB/CD/含まない)
各種ファイルとサブフォルダ.zip
試して、下記のコマンドはいいです。

pb = new ProcessBuilder("sh", "-c", “zip -r test.zip ./*”)).directory(new File("/home/AB/CD/"));
同じく書きの書き方でもいいです。
pb = new ProcessBuilder("/bin/sh", "-c", “zip -r test.zip ./*”)).directory(new File("/home/AB/CD/"));
pb = new ProcessBuilder("/bin/bash", "-c", “zip -r test.zip ./*”)).directory(new File("/home/AB/CD/"));

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0