How to Run PKZIP From Java

How to Run PKZIP From Java thumbnail
Java programs can make use of the full power of PKZIP.

When writing Java code, you often need to invoke other programs for functions not implemented by your own code. Often, the external programs or applications being invoked are not written in Java. Sometimes, you do not even have access to their source codes. You need a way of invoking other Windows applications from Java code. That entails providing input to them, passing control to them in the execution environment they expect, and having access to their output. In particular, it is easy to invoke the PKZIP file compression/decompression utility from Java on Windows.

Things You'll Need

  • Computer running Windows
  • Java virtual machine and bytecode compiler installed on the computer
  • PKZIP installed on the computer
Show More

Instructions

    • 1

      Import "java.io.*" and "java.util.*" in your Java code.

    • 2

      You will run PKZIP in the same runtime context where your Java application is running, but as a separate process. That means that your Java code will create a new process running PKZIP, then wait until the new process is done, then resume executing. For this, first include

      Runtime myruntime = Runtime.getRuntime();

      in your code.

    • 3

      Include

      Process newproc = myruntime.exec("\"c:/program files/pkware/pkzipc.exe\"");

      in your code in order to create and fork the new process. The version of PKZIP designed to be invoked from the command line is called "pkzipc.exe".

    • 4

      Wait until PKZIP is done by including

      newproc.waitFor();

    • 5

      If you want to pass arguments to PKZIP, pass a String array as the argument to the Runtime.exec() method, as shown. The first string must contain the path to, and name of, the executable. The second and subsequent strings contain the arguments. See the PKZIP 6.0 Command Line User's Manual for an extensive explanation of how you can do this by passing arguments to PKZIP. For example, say that we want to add the file named "summary.doc" to the already existing archive named "JanuarySales.zip". Invoke Runtime.exec() this way:

      String[] commandwithargs = {

      "\"c:/program files/pkware/pkzipc.exe\"",

      "-a JanuarySales.zip",

      "summary.doc"

      };

      myruntime.exec(commandwithargs);

Tips & Warnings

  • All paths passed to the Runtime.exec() method must be fully specified on Windows. The example above assumes that both "JanuarySales.zip" and "summary.doc" are in the current directory for the Java application's runtime environment. If they are not, prepend the paths for them within the argument to Runtime.exec()

Related Searches:

References

  • Photo Credit coffee in coffee image by Maria Brzostowska from Fotolia.com

Comments

You May Also Like

  • How to Install Pkzip

    If you used bulletin board systems for downloading files in the 1990s, you may be familiar with the utility PKZIP. PKZIP is...

  • How to Extract Zip Files in Java

    Extracting zip files is accomplished using an internal Java library of classes that makes it easy for programmers. Java uses a zip...

  • How Do I Zip Files With Pkzip?

    PKZIP lets you compress not only files to make them smaller in size, but also folders. The utility, originally for MS-DOS systems,...

  • How to Use PKZIP

    PKZIP is file compression software that was originally developed by Phil Katz, whose initials the make up part of its name. The...

  • Java Random Number Method

    Random Number methods in Java return random numbers for use in the program. There are two ways to generate a random number...

  • How to Decompress a Tar

    If you want to deliver a batch of files from a Linux system, you usually use the Tar command. When you "tar"...

  • How to Run XP System Restore From the Command Prompt Line

    System Restore is a utility program built into the Windows XP operating system. The tool keeps track of changes that are made...

  • How to Make a Custom XP CD

    Creating a custom Windows XP CD allows you to add additional settings, drivers, utilities and other programs that will automatically install whenever...

Related Ads

Featured