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

  1. //
  2. // CabMaker.java
  3. //
  4. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  5. // All rights reserved 
  6. //
  7. import com.ms.util.cab.*;
  8. import java.io.*;
  9.  
  10. final class CabMaker implements CabConstants, CabProgressInterface
  11. {
  12.     // name of cabinet to create
  13.     String      cabinet_name;
  14.  
  15.     // command line argument list
  16.     String[]    file_list;
  17.  
  18.     // location of first file in command line argument list
  19.     int         file_list_begin_index;
  20.  
  21.     // CabCreator object for creating the cabinets
  22.     CabCreator  cab_creator;
  23.  
  24.     // compression method and window to use
  25.     int         compression_method, compression_window;
  26.  
  27.     // recurse into subdirectories?
  28.     boolean     can_recurse;
  29.  
  30.     // output stream of the cabinet we are creating
  31.     FileOutputStream    output_cabinet;
  32.  
  33.  
  34.     // constructor
  35.     CabMaker(
  36.         String      new_cabinet_name, 
  37.         String[]    new_file_list, 
  38.         int         new_file_list_begin_index,
  39.         int         new_compression_method,
  40.         int         new_compression_window,
  41.         boolean     new_can_recurse
  42.     )
  43.     {
  44.         cabinet_name            = new_cabinet_name;
  45.         file_list               = new_file_list;
  46.         file_list_begin_index   = new_file_list_begin_index;
  47.         can_recurse             = new_can_recurse;
  48.         compression_method      = new_compression_method;
  49.         compression_window      = new_compression_window;
  50.     }
  51.  
  52.  
  53.     //
  54.     // Create the cabinet
  55.     //
  56.     // Returns whether cabinet creation was successful
  57.     //
  58.     boolean create()
  59.     {
  60.         CabFolderEntry folder;
  61.  
  62.         System.out.println("Creating new cabinet: "+cabinet_name);
  63.  
  64.         try
  65.         {
  66.             output_cabinet = new FileOutputStream(cabinet_name);
  67.         }
  68.         catch (IOException ioe)
  69.         {
  70.             System.out.println("Error opening " + cabinet_name + " for output");
  71.             return false;
  72.         }
  73.  
  74.         //
  75.         // Create a new CabCreator object, and pass ourselves in as the
  76.         // CabProgressInterface.
  77.         //
  78.         cab_creator = new CabCreator(this);
  79.  
  80.         try
  81.         {
  82.             cab_creator.create(output_cabinet);
  83.         }
  84.         catch (Exception e)
  85.         {
  86.             closeAndDeleteCabinet();
  87.             System.out.println("Error initializing cabinet creation: "+e.getMessage());
  88.             return false;
  89.         }
  90.  
  91.         //
  92.         // Start a new folder, select compression method
  93.         //
  94.         folder = new CabFolderEntry();
  95.  
  96.         try
  97.         {
  98.             folder.setCompression(compression_method, compression_window);
  99.             cab_creator.newFolder(folder);
  100.         }
  101.         catch (Exception e)
  102.         {
  103.             closeAndDeleteCabinet();
  104.             System.out.println("Error starting new folder: "+e.getMessage());
  105.         }
  106.  
  107.         //
  108.         // Add the specified files to the cabinet.
  109.         //
  110.         // Read the file names from the command line argument list.
  111.         //
  112.         for (int i = file_list_begin_index; i < file_list.length; i++)
  113.         {
  114.             File file_to_add;
  115.  
  116.             file_to_add = new File(file_list[i]);
  117.  
  118.             if (file_to_add.isDirectory())
  119.             {
  120.                 addDirectoryTree(file_list[i]);
  121.             }
  122.             else
  123.             {
  124.                 addFile(file_list[i]);
  125.             }
  126.         }           
  127.  
  128.         //
  129.         // Add files added; now complete cabinet creation
  130.         //
  131.         try
  132.         {
  133.             cab_creator.complete();
  134.         }
  135.         catch (Exception e)
  136.         {
  137.             closeAndDeleteCabinet();
  138.             System.out.println("Cabinet creation failed: "+e.getMessage());
  139.             return false;
  140.         }
  141.  
  142.         //
  143.         // Close the output stream we have been writing the cabinet to
  144.         //
  145.         try
  146.         { 
  147.             output_cabinet.close(); 
  148.         } 
  149.         catch (IOException ioe) 
  150.         {
  151.             System.out.println("Error closing cabinet: "+ioe.getMessage());
  152.             return false;
  153.         }
  154.  
  155.         return true;
  156.     }
  157.  
  158.  
  159.     //
  160.     // This method is called if an error occurs; we close the cabinet we were
  161.     // creating, and delete it
  162.     //
  163.     private void closeAndDeleteCabinet()
  164.     {
  165.         try
  166.         { 
  167.             output_cabinet.close(); 
  168.  
  169.             File file = new File(cabinet_name);
  170.             file.delete();
  171.         } 
  172.         catch (Exception e) 
  173.         {
  174.             // ignore
  175.         }
  176.     }
  177.  
  178.  
  179.     //
  180.     // Implements CabProgressInterface
  181.     //
  182.     public Object progress(
  183.         int             progress_type,
  184.         long            val1,
  185.         long            val2,
  186.         Object[]        progress_data
  187.     )
  188.     {
  189.         if (progress_type == CAB_PROGRESS_INPUT)
  190.         {
  191.             System.out.print(".");
  192.             System.out.flush();
  193.         }
  194.         // ignore messages we don't know how to handle
  195.  
  196.         return null;
  197.     }
  198.  
  199.  
  200.     //
  201.     // Adds a file to the cabinet
  202.     //
  203.     private void addFile(String filename)
  204.     {
  205.         CabFileEntry    cab_file_entry;
  206.         File            file;
  207.         FileInputStream input;
  208.  
  209.         System.out.print("  " + filename + " ");
  210.  
  211.         file = new File(filename);
  212.  
  213.         cab_file_entry = new CabFileEntry();
  214.  
  215.         // determine the name under which to store the file in the cabinet
  216.         
  217.         if (file.isAbsolute())
  218.         {
  219.             // if this is an absolute path, then don't store any of the path data
  220.             cab_file_entry.setName(file.getName());
  221.         }
  222.         else
  223.         {
  224.             // if this is a relative path, store the path data
  225.             cab_file_entry.setName(filename);
  226.         }
  227.  
  228.         try
  229.         {
  230.             input = new FileInputStream(file);
  231.         }
  232.         catch (IOException ioe)
  233.         {
  234.             System.out.println("Error, cannot open " + filename + " for input");
  235.             return;
  236.         }
  237.  
  238.         // set file attributes
  239.         if (file.canWrite() == false)
  240.             cab_file_entry.setReadOnly(true);
  241.  
  242.         try
  243.         {
  244.             cab_creator.addStream(input, cab_file_entry);
  245.         }
  246.         catch (Exception e)
  247.         {
  248.             System.out.println();
  249.             System.out.println("Error adding file: "+e.getMessage());
  250.         }
  251.  
  252.         System.out.println();
  253.     }
  254.  
  255.  
  256.     //
  257.     // Adds a directory tree to the cabinet
  258.     //
  259.     private void addDirectoryTree(String path)
  260.     {
  261.         String[]    list;
  262.         File        current_dir;
  263.  
  264.         current_dir = new File(path);
  265.  
  266.         // get a list of files in this directory
  267.         list = current_dir.list();
  268.  
  269.         if (list != null)
  270.         {
  271.             for (int i = 0; i < list.length; i++)
  272.             {
  273.                 String  full_name;
  274.                 File    current_file;
  275.             
  276.                 // the full name of this file
  277.                 full_name       = path + File.separator + list[i];
  278.  
  279.                 // get a handle on this file
  280.                 current_file    = new File(full_name);
  281.  
  282.                 if (current_file.isFile())
  283.                 {
  284.                     //  it's a file, so add it
  285.                     addFile(full_name);
  286.                 }
  287.                 else if (current_file.isDirectory())
  288.                 {
  289.                     // it's a directory, so recurse into it if the recurse
  290.                     // into subdirs command-line option was selected
  291.                     if (can_recurse)
  292.                         addDirectoryTree(full_name);
  293.                 }
  294.             }
  295.         }
  296.     }
  297. }
  298.