Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Merging Java and Win32: A new way to develop Windows applications

Learn how to write Win32 applications in Java instead of C++ -- and save yourself some time and effort!

  • Print
  • Feedback

Page 4 of 6

Another item to note is a call to the SetConsoleCtrlHandler () Win32 API. This API prevents Ctrl-C or Ctrl-Break key presses -- in addition to other events -- from stopping the application before it finishes. This may or may not be desirable, depending on the application.

The ZIP application is written in Java. It gives users the ability to view the contents of zip archive files, as well as the ability to extract individual files from these archives. The following listing contains the source code to ZIP.

// ========
// zip.java
// ========
import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;
public class zip
{
   // ==================================================================
   // dump_zip
   //
   // Dump the contents of the zip file (the file names of zipped files)
   // to the standard output device.
   //
   // Arguments:
   //
   // zip_name - name of zip archive
   //
   // Return:
   //
   // none
   // ==================================================================
   public static void dump_zip (String zip_name)
   {
      Enumeration list = null;
      ZipEntry ze = null;
      ZipFile zf = null;
      // Check to see if zip_name has an extension.
      // If not (no period character detected) then
      // attach ".zip" to the file name.
      if (zip_name.indexOf ('.') == -1)
          zip_name = zip_name + ".zip";
      try
      {
          zf = new ZipFile (zip_name);
      }
      catch (ZipException e1)
      {
         System.err.println ("ZIP exception: " + e1);
      }
      catch (IOException e2)
      {
         System.err.println ("IO exception: " + e2);
      }
      list = zf.entries ();
      while (list.hasMoreElements ())
      {
         ze = (ZipEntry) list.nextElement ();
         System.out.println (ze.getName ());
      }
   }
   // ===================================
   // extract_zip
   //
   // Extract file from zip archive.  
   //
   // Arguments:
   //
   // zip_name  - name of zip archive
   // file_name - name of file to extract
   //
   // Return:
   //
   // none
   // ===================================
   public static void extract_zip (String zip_name, String file_name)
   {
      BufferedInputStream bis = null;
      byte buf [] = new byte [1024];
      Enumeration list = null;
      FileOutputStream fos = null;
      int ln = 0;
      int sz = 0;
      ZipEntry ze = null;
      ZipFile zf = null;
      // Check to see if zip_name has an extension.
      // If not (no period character detected) then
      // attach ".zip" to the file name.
      if (zip_name.indexOf ('.') == -1)
          zip_name = zip_name + ".zip";
      try
      {
          zf = new ZipFile (zip_name);
      }
      catch (ZipException e1)
      {
         System.err.println ("ZIP exception: " + e1);
      }
      catch (IOException e2)
      {
         System.err.println ("IO exception: " + e2);
      }
      list = zf.entries ();
      while (list.hasMoreElements ())
      {
         ze = (ZipEntry) list.nextElement ();
         sz = (int) ze.getSize ();
         // Compare archive file with file name argument
         // in a case-insensitive manner.
         if (ze.getName ().toUpperCase ().endsWith (file_name.toUpperCase ()))
         {
             try
             {
                bis = new BufferedInputStream (zf.getInputStream (ze));
                fos = new FileOutputStream (new File (file_name));
                while (sz > 0 &&  // workaround for bug
                       (ln = bis.read (buf, 0, Math.min (1024, sz))) != -1)
                {
                   fos.write (buf, 0, ln);
                   sz -= ln;
                }
                bis.close ();
                fos.flush ();
             }
             catch (IOException e3)
             {
                System.err.println ("IO exception: " + e3);
                return;
             }
             System.out.println ("File: " + file_name + " extracted");   
             return;
         }
      }
      System.out.println ("Not found");
   }
   // =============================================
   //
   // main
   //
   // Program entry point.
   //
   // Arguments:
   //
   // args - array of command line argument strings
   //
   // Return:
   //
   // none
   // =============================================
   public static void main (String [] args)
   {
      System.out.println ("zip v1.0\n");
      if (args.length == 1)
          dump_zip (args [0]);
      else
      if (args.length == 3 && args [0].equals ("-x"))
          extract_zip (args [2], args [1]);
      else
      {
          System.err.println ("usage: zip [-x file] zip");
          System.exit (1);
      }
   }
}


Putting it together

Follow these steps to build the ZIP application.

  • Print
  • Feedback

Resources
  • The zip archive contains all of the necessary C++/Java source and deployment files used by this article http://www.javaworld.com/jw-07-1998/java-win32/zip.zip
  • I've found the following resources beneficial when I'm writing applications that use the Java native Interface (JNI).
  • Essential JNI Java Native Interface, by Rob Gordon. PublisherPrentice Hall, Copyright1998. This book dives into JNI, and covers many techniques for calling C++ code with Java and calling Java with C++ code. One chapter focuses on the Invocation API and how to construct a C++ application which embeds the Java Virtual Machine -- in other words, how to create your own AppletViewer.exe program.
  • The JavaWorld article Use native methods to expand the Java environment provides an introductory look at the Java Native Interface http://www.javaworld.com/javaworld/jw-07-1997/jw-07-javadev.html