2

Need to execute the below winzip command in a java program.

C:\Program Files\WinZip>winzip32 -a -s"password" C:\abc.zip C:\abc.doc
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Prasanna
  • 73
  • 1
  • 3
  • 6
  • Are you aware that java includes native support for compressing/decompressing zip files? It's better to use pure java because your application will be cross platform as opposed to Windows-only. – Asaph Feb 15 '10 at 02:53
  • @Asaph: He want to create a password protected zip file. – BalusC Feb 15 '10 at 02:55
  • @BalusC: I noticed that after I posted my comment, when I read your answer, which I upvoted. :) – Asaph Feb 15 '10 at 02:58

3 Answers3

5

Use Runtime#exec() (tutorial here, caveats here) or just pure Java code (related SO topic).

By the way, if you didn't need password protection, you could just go ahead with java.util.zip (tutorial here).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Also, if you intend to have lots of threads be able to call out I suggest limiting the number of threads that can call out at a time. – Epsilon Prime Feb 15 '10 at 02:59
  • 1
    +1: Using java.util.zip gives you cross-platform support, doesn't rely on an install of WinZip etc. If it's an option to use, where possible, do so. –  Feb 15 '10 at 03:30
2

You can simple use Runtime.exec();

String[] cmd = {"C:\\Program Files\\WinZip\\winzip32", "-a", "-s", "password", "C:\\abc.zip", "C:\\abc.doc"};
Runtime.getRuntime().exec(cmd);
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
1

Taking a look around the internet, would this do what you're looking for: https://truezip.dev.java.net/ without having to rely on an external install of WinZip which may or may not exist on the end user system? Just a suggestion.