完全に個人的な備忘録

完全に個人的な備忘録。学習コストが高くなってきたので、メモしておかないと…片っ端から忘却の彼方なのです。

(windowsで) zipのパスワードをコマンドラインから与える方法

方法1 zipコマンドのオプションで指定する

Windows環境でzipのパスワードをコマンドラインから与える方法ですが、info-zipのzipコマンドで -P オプションでパスワードを指定して圧縮する事ができる。

但し、encryptionをサポートしているzipコマンドである必要がある。対応していないコマンドの場合は以下のメッセージが出力される。

>zip -P hoge hoge.zip __IME__
zip error: Invalid command arguments (encryption not supported)

本日時点のzip-infoのページによると、

LATEST RELEASES: Zip 3.00 was released on 7 July 2008.

が最新のようだ。ソースは以下からダウンロードできるようだ。

上記のソースからbuildしたzipコマンドを使用すると、ヘルプでは表示されないが、zip コマンドの -P オプションでパスワードを指定する事ができる。

>zip
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -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
  -!   use privileges (if granted) to obtain all aspects of WinNT security
  -$   include volume label         -S   include system and hidden files
  -e   encrypt                      -n   don't compress these suffixes
  -h2  show more help

上記の通り、ヘルプには表示されないが、-P オプションでパスワードを指定できる。

c:\tmp\zip30>zip -P hoge hoge.zip README
  adding: README (176 bytes security) (deflated 59%)

エクスプローラから参照すると、パスワードの入力を要求される。

方法2 zipcloak コマンドで指定(したい)

私は、バックアップをantのzipタスクで行なっている。無償の外部ストレージを使用しているので、容量がそんなに大きくない等の理由があり、特定の条件に一致するファイルはバックアップ対象から外したりしている。

zipコマンドだと、特定のパターンのファイルを外したりする事ができない(あるいは面倒)が、antのzipタスクだとファイルセットで指定可能なので、重宝している。

しかし、antのzipタスクはパスワードの指定ができない。何となく、そのまま外部ストレージにデータを預けるのは抵抗がある。せめてパスワードロックはしておきたい。

そこで、info-zipのzipcloakでパスワードを設定しようと思ったが、このコマンドはコマンドラインからパスワードを指定できない。

で、結局、ソースを改悪して、パスワードをコマンドラインから指定できるようにしちゃいました。

需要があるかどうかは不明ですが、以下にパッチを置いておきます。

--- c:/tmp/old/zipcloak.c	Wed Sep 29 14:57:03 2010
+++ c:/tmp/new/zipcloak.c	Wed Sep 29 17:49:54 2010
@@ -220,6 +220,7 @@
 "  -b  --temp-path    use \"path\" for the temporary zip file",
 #endif
 "  -O  --output-file  write output to new zip file",
+"  -p  --password     password (add by T.Furukawa)",
 "  -q  --quiet        quiet operation, suppress some informational messages",
 "  -h  --help         show this help",
 "  -v  --version      show version info",
@@ -294,6 +295,7 @@
     {"L",  "license",     o_NO_VALUE,       o_NOT_NEGATABLE, 'L',  "license"},
     {"l",  "",            o_NO_VALUE,       o_NOT_NEGATABLE, 'L',  "license"},
     {"O",  "output-file", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'O',  "output to new archive"},
+    {"p",  "password", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'p',  "password"},
     {"v",  "version",     o_NO_VALUE,       o_NOT_NEGATABLE, 'v',  "version"},
     /* the end of the list */
     {NULL, NULL,          o_NO_VALUE,       o_NOT_NEGATABLE, 0,    NULL} /* end has option_ID = 0 */
@@ -335,6 +337,7 @@
 
     char **args;               /* copy of argv that can be freed */
 
+    passwd[0] = '\0';
 #ifdef THEOS
     setlocale(LC_CTYPE, "I");
 #endif
@@ -495,6 +498,10 @@
     {
       switch (option)
       {
+      case 'p':
+          strncpy(passwd, value, IZ_PWLEN);
+          strcpy(verify, passwd);
+          break;
         case 'b':   /* Specify path for temporary file */
           if (temp_path) {
             ziperr(ZE_PARMS, "more than one temp_path");
@@ -627,23 +634,24 @@
     }
 #endif
 
-    /* Get password */
-    if (getp("Enter password: ", passwd, IZ_PWLEN+1) == NULL)
-        ziperr(ZE_PARMS,
-               "stderr is not a tty (you may never see this message!)");
-
-    if (decrypt == 0) {
-        if (getp("Verify password: ", verify, IZ_PWLEN+1) == NULL)
-               ziperr(ZE_PARMS,
-                      "stderr is not a tty (you may never see this message!)");
+    if ('\0' == passwd[0]) {
+        /* Get password */
+        if (getp("Enter password: ", passwd, IZ_PWLEN+1) == NULL)
+            ziperr(ZE_PARMS,
+                   "stderr is not a tty (you may never see this message!)");
+
+        if (decrypt == 0) {
+            if (getp("Verify password: ", verify, IZ_PWLEN+1) == NULL)
+                ziperr(ZE_PARMS,
+                       "stderr is not a tty (you may never see this message!)");
 
-        if (strcmp(passwd, verify))
-               ziperr(ZE_PARMS, "password verification failed");
+            if (strcmp(passwd, verify))
+                ziperr(ZE_PARMS, "password verification failed");
 
-        if (*passwd == '\0')
-               ziperr(ZE_PARMS, "zero length password not allowed");
+            if (*passwd == '\0')
+                ziperr(ZE_PARMS, "zero length password not allowed");
+        }
     }
-
     /* Open input zip file again, copy preamble if any */
     if ((in_file = fopen(zipfile, FOPR)) == NULL) ziperr(ZE_NAME, zipfile);
>patch -i zipcloak.diff
(Stripping trailing CRs from patch.)
patching file zipcloak.c

>nmake -f win32/makefile.w32

でbuildできます。

build済みのファイルも以下に置いています。

以下のような感じで使用しています。

     <exec executable="zipcloak.exe" failonerror="true">
      <arg line="--password ${zip.password}" />
      <arg path="${backup.file}" />
     </exec>

本当はgpgの方が良いんだろうな。