home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Packaging / Create / Create.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  5.5 KB  |  177 lines

  1. //
  2. // Create.java
  3. //
  4. // This application demonstrates how to use the CabCreate class to create
  5. // cabinets
  6. //
  7. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  8. // All rights reserved 
  9. //
  10. import com.ms.util.cab.CabCreator;
  11. import com.ms.util.cab.CabConstants;
  12.  
  13.  
  14. final class Create implements CabConstants
  15. {
  16.     // the default compression mode
  17.     static int compression_method = COMPRESSION_MSZIP;
  18.     static int compression_window = 16;
  19.  
  20.     // recurse into subdirectories when given a directory name?
  21.     static boolean recurse_into_subdirs = false;
  22.  
  23.     //
  24.     // Entrypoint
  25.     //
  26.     public static void main(String args[])
  27.     {
  28.         if (args.length < 2)
  29.         {
  30.             displayHelp();
  31.         }
  32.         else
  33.         {
  34.             int cab_filename_arg;
  35.  
  36.             // if this returns -1, it means invalid arguments
  37.             cab_filename_arg = parseArgs(args);
  38.  
  39.             if (cab_filename_arg != -1)
  40.             {
  41.                 CabMaker creator;
  42.  
  43.                 // Need a non-static class object to pass to the CabCreator class
  44.                 creator = new CabMaker(
  45.                     args[cab_filename_arg], // cabinet name
  46.                     args,                   // file list
  47.                     cab_filename_arg + 1,   // where file list starts in args[] 
  48.                     compression_method,
  49.                     compression_window,
  50.                     recurse_into_subdirs
  51.                 );
  52.  
  53.                 creator.create();
  54.             }
  55.         }
  56.     }
  57.  
  58.  
  59.     //
  60.     // Argument parsing
  61.     //
  62.     static int parseArgs(String args[])
  63.     {
  64.         int arg;
  65.  
  66.         for (arg = 0; arg < args.length; arg++)
  67.         {
  68.             // look for command-line switches
  69.             if (args[arg].charAt(0) == '/' || args[arg].charAt(0) == '-')
  70.             {
  71.                 if (args[arg].length() < 2)
  72.                 {
  73.                     System.out.println("Invalid arguments");
  74.                     return -1;
  75.                 }
  76.  
  77.                 switch (args[arg].charAt(1))
  78.                 {
  79.                     default:
  80.                     {
  81.                         System.out.println("Unknown option "+args[arg]);
  82.                         return -1;
  83.                     }
  84.  
  85.                     case 'h':
  86.                     case '?':
  87.                     {
  88.                         displayHelp();
  89.                         return -1;
  90.                     }
  91.  
  92.                     case 'r':
  93.                     {
  94.                         recurse_into_subdirs = true;
  95.                         break;
  96.                     }
  97.  
  98.                     case 'm':
  99.                     {
  100.                         String compression_mode_name;
  101.  
  102.                         if (arg + 1 >= args.length)
  103.                         {
  104.                             System.out.println("-m option must be followed by compression method");
  105.                             return -1;
  106.                         }
  107.  
  108.                         compression_mode_name = args[arg+1];
  109.  
  110.                         if (compression_mode_name.equals("NONE"))
  111.                         {
  112.                             compression_method = COMPRESSION_NONE;
  113.                             compression_window = 0;
  114.                         }
  115.                         else if (compression_mode_name.equals("MSZIP"))
  116.                         {
  117.                             compression_method = COMPRESSION_MSZIP;
  118.                             compression_window = 16;
  119.                         }
  120.                         else if (compression_mode_name.startsWith("LZX"))
  121.                         {
  122.                             compression_method = COMPRESSION_LZX;
  123.  
  124.                             // catch string out-of-bounds and integer conversion
  125.                             // exceptions
  126.                             try
  127.                             {
  128.                                 compression_window = Integer.valueOf(compression_mode_name.substring(4)).intValue();
  129.                             }
  130.                             catch (Exception e)
  131.                             {
  132.                                 System.out.println("LZX compression requires a window size parameter");
  133.                                 return -1;
  134.                             }
  135.  
  136.                             if (compression_window < 15 || compression_window > 21)
  137.                             {
  138.                                 System.out.println("LZX compression window must be in 15..21 range");
  139.                                 return -1;
  140.                             }
  141.                         }
  142.                         else
  143.                         {
  144.                             System.out.println("Unknown compression method "+compression_mode_name);
  145.                             return -1;
  146.                         }
  147.  
  148.                         arg++;
  149.                         break;
  150.                     }
  151.                 }
  152.             }
  153.             else
  154.             {
  155.                 break;
  156.             }
  157.         }
  158.  
  159.         return arg;
  160.     }
  161.  
  162.  
  163.     //
  164.     // Command-line help
  165.     //
  166.     private static void displayHelp()
  167.     {
  168.         System.out.println("Cabinet creation utility");
  169.         System.out.println();
  170.         System.out.println("Usage: Create <options> <cabinet name> [<files>]");
  171.         System.out.println();
  172.         System.out.println("Options:");
  173.         System.out.println("  /m <mode>  Set compression mode [LZX:<15..21>|MSZIP|NONE]");
  174.         System.out.println("  /r         Recurse into subdirectories");
  175.     }
  176. }
  177.