Linux での zip コマンド
先日から,zip 関連のコマンドの動作を調べている.
それで困っているのが,あるフォルダ以下を指定する方法.
例えば ~/Desktop/ 直下のファイルを指定する時,パスの home/pi/Desktop/ の部分を含めたくない.
pi@raspberrypi:~/Desktop $ ls bak myweb scratch1_20190107.sb scratch2_20181231.sb2 test_c 三角形.py 正方形.py pi@raspberrypi:~/Desktop $ zip /tmp/pi/aaa.zip * adding: bak/ (stored 0%) adding: myweb/ (stored 0%) adding: scratch1_20190107.sb (deflated 39%) adding: scratch2_20181231.sb2 (deflated 30%) adding: test_c/ (stored 0%) adding: 三角形.py (deflated 17%) adding: 正方形.py (deflated 16%) pi@raspberrypi:~/Desktop $ zip /tmp/pi/aa.zip ~/Desktop/* adding: home/pi/Desktop/bak/ (stored 0%) adding: home/pi/Desktop/myweb/ (stored 0%) adding: home/pi/Desktop/scratch1_20190107.sb (deflated 39%) adding: home/pi/Desktop/scratch2_20181231.sb2 (deflated 30%) adding: home/pi/Desktop/test_c/ (stored 0%) adding: home/pi/Desktop/三角形.py (deflated 17%) adding: home/pi/Desktop/正方形.py (deflated 16%) pi@raspberrypi:~/Desktop $ unzip /tmp/pi/aaa.zip -l Archive: /tmp/pi/aaa.zip caution: filename not matched: -l pi@raspberrypi:~/Desktop $ unzip -l /tmp/pi/aaa.zip Archive: /tmp/pi/aaa.zip Length Date Time Name --------- ---------- ----- ---- 0 2019-01-09 15:02 bak/ 0 2019-01-10 21:44 myweb/ 75455 2019-01-08 00:13 scratch1_20190107.sb 69629 2019-01-08 00:13 scratch2_20181231.sb2 0 2019-01-16 16:40 test_c/ 100 2019-01-04 15:13 三角形.py 99 2019-01-04 15:11 正方形.py --------- ------- 145283 7 files pi@raspberrypi:~/Desktop $ unzip -l /tmp/pi/aa.zip Archive: /tmp/pi/aa.zip Length Date Time Name --------- ---------- ----- ---- 0 2019-01-09 15:02 home/pi/Desktop/bak/ 0 2019-01-10 21:44 home/pi/Desktop/myweb/ 75455 2019-01-08 00:13 home/pi/Desktop/scratch1_20190107.sb 69629 2019-01-08 00:13 home/pi/Desktop/scratch2_20181231.sb2 0 2019-01-16 16:40 home/pi/Desktop/test_c/ 100 2019-01-04 15:13 home/pi/Desktop/三角形.py 99 2019-01-04 15:11 home/pi/Desktop/正方形.py --------- ------- 145283 7 files pi@raspberrypi:~/Desktop $
Synology NAS の 7z だと,ルートから指定?すれば省かれるっぽい.
Iwao@DS116:~/gcc_test/Test/T_zip/zip$ 7z a aa.zip t_call_z/* Scanning Creating archive aa.zip Total: 652420 Compressing t_call_z/a.out Compressing t_call_z/bak/a.out Compressing t_call_z/bak/l7z.out Compressing t_call_z/bak/t_call_z.BAK Compressing t_call_z/bak/t_call_z.cpp Compressing t_call_z/bak/t_call_z.dsp Compressing t_call_z/bak/zip.out Compressing t_call_z/t_call_z.cpp Everything is Ok Iwao@DS116:~/gcc_test/Test/T_zip/zip$ 7z a aaa.zip ~/gcc_test/Test/T_zip/zip/t_call_z/* Scanning Creating archive aaa.zip Total: 652420 Compressing a.out Compressing bak/a.out Compressing bak/l7z.out Compressing bak/t_call_z.BAK Compressing bak/t_call_z.cpp Compressing bak/t_call_z.dsp Compressing bak/zip.out Compressing t_call_z.cpp Everything is Ok Iwao@DS116:~/gcc_test/Test/T_zip/zip$ 7z l aa.zip Listing archive: aa.zip -- Path = aa.zip Type = zip Physical Size = 187879 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ------------------------ 2019-01-22 11:02:30 ..... 161544 46349 t_call_z/a.out 2019-01-22 09:38:18 D.... 0 0 t_call_z/bak 2019-01-21 16:33:02 ..... 158988 45396 t_call_z/bak/a.out 2019-01-21 16:18:56 ..... 158628 45292 t_call_z/bak/l7z.out 2019-01-21 15:15:12 ..... 2325 815 t_call_z/bak/t_call_z.BAK 2019-01-21 16:31:56 ..... 2802 962 t_call_z/bak/t_call_z.cpp 2019-01-21 14:02:06 ..... 4227 1141 t_call_z/bak/t_call_z.dsp 2019-01-21 15:44:20 ..... 158656 45303 t_call_z/bak/zip.out 2019-01-22 11:01:32 ..... 4569 1167 t_call_z/t_call_z.cpp 2019-01-22 14:41:22 D.... 0 0 t_call_z/tmp ------------------- ----- ------------ ------------ ------------------------ 651739 186425 8 files, 2 folders Iwao@DS116:~/gcc_test/Test/T_zip/zip$ 7z l aaa.zip Listing archive: aaa.zip -- Path = aaa.zip Type = zip Physical Size = 187609 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ------------------------ 2019-01-22 11:02:30 ..... 161544 46349 a.out 2019-01-22 09:38:18 D.... 0 0 bak 2019-01-21 16:33:02 ..... 158988 45396 bak/a.out 2019-01-21 16:18:56 ..... 158628 45292 bak/l7z.out 2019-01-21 15:15:12 ..... 2325 815 bak/t_call_z.BAK 2019-01-21 16:31:56 ..... 2802 962 bak/t_call_z.cpp 2019-01-21 14:02:06 ..... 4227 1141 bak/t_call_z.dsp 2019-01-21 15:44:20 ..... 158656 45303 bak/zip.out 2019-01-22 11:01:32 ..... 4569 1167 t_call_z.cpp 2019-01-22 14:41:22 D.... 0 0 tmp ------------------- ----- ------------ ------------ ------------------------ 651739 186425 8 files, 2 folders Iwao@DS116:~/gcc_test/Test/T_zip/zip$
bool linux_zip_create (LPCTSTR zip_name,LPCTSTR zip_root) { { tstring cmd_psd = _T("pushd ") + tstring(zip_root) ; _tsystem(cmd_psd.c_str()) ; } { tstring l_z_name = zip_name ; tstring l7z_name = ::Path_ChangeExt(l_z_name,_T("_7.zip")) ; tstring root_zip = _T("*") ; tstring root_7z_ = tstring(zip_root) + _T("/*") ; tstring cmd_zip = l_cmd_zip _T(" ") + l_z_name + _T(" -r ") + root_zip ; // zip zip_name -r zip_root tstring cmd_7z_ = l_cmd_7z_ _T(" a ") + l7z_name + _T(" ") + root_7z_ ; // 7z a zip_name zip_root if (which(l_cmd_zip)) { _tsystem(cmd_zip.c_str()) ; } if (which(l_cmd_7z_)) { _tsystem(cmd_7z_.c_str()) ; } _tsystem(l_cmd_popd) ; if (which(l_cmd_zip)) { return true ; } if (which(l_cmd_7z_)) { return true ; } } return false ; }
pushd で,カレントを動かすことにより対応.