ZipProductSource.java

  1. package gov.usgs.earthquake.product.io;

  2. import gov.usgs.earthquake.product.InputStreamContent;
  3. import gov.usgs.earthquake.product.Product;
  4. import gov.usgs.earthquake.product.ProductId;

  5. import java.io.File;
  6. import java.util.Date;
  7. import java.util.Enumeration;

  8. import java.util.zip.ZipFile;
  9. import java.util.zip.ZipEntry;

  10. /**
  11.  * Load a product from an InputStream containing ZIP.
  12.  *
  13.  * ZipProductSource reads an input stream containing a product zip file.
  14.  *
  15.  * This zip file's first entry must be a product xml file containing all product
  16.  * metadata and inline content.
  17.  */
  18. public class ZipProductSource implements ProductSource {

  19.     /** The input stream where zip content is read. */
  20.     private File zip;

  21.     /**
  22.      * Construct a new ZipProductSource.
  23.      *
  24.      * @param zip
  25.      *            the input stream where zip content is read.
  26.      */
  27.     public ZipProductSource(final File zip) {
  28.         this.zip = zip;
  29.     }

  30.     /**
  31.      * Parse the zip stream and send product to product output.
  32.      *
  33.      * @param out
  34.      *            ProductOutput that will receive the product.
  35.      */
  36.     public void streamTo(ProductHandler out) throws Exception {
  37.         ZipFile zis = null;

  38.         try {
  39.             zis = new ZipFile(this.zip);
  40.    
  41.             Enumeration<? extends ZipEntry> entries = zis.entries();
  42.             ZipEntry entry = entries.nextElement();
  43.    
  44.             if (!entry.getName()
  45.                     .equals(ZipProductHandler.PRODUCT_XML_ZIP_ENTRYNAME)) {
  46.                 throw new Exception("Unexpected first entry " + entry.getName()
  47.                         + ", expected "
  48.                         + ZipProductHandler.PRODUCT_XML_ZIP_ENTRYNAME);
  49.             }
  50.    
  51.             Product product = ObjectProductHandler.getProduct(new XmlProductSource(
  52.                     zis.getInputStream(entry)));
  53.             ProductId id = product.getId();
  54.    
  55.             // send all except signature and end product, until after all
  56.             // contents
  57.             new ObjectProductSource(product) {
  58.                 public void sendSignature(final ProductHandler out)
  59.                         throws Exception {
  60.                     // do nothing
  61.                 }
  62.    
  63.                 public void sendEndProduct(final ProductHandler out)
  64.                         throws Exception {
  65.                     // do nothing
  66.                 }
  67.             }.streamTo(out);
  68.    
  69.             // send other contents
  70.             while (entries.hasMoreElements()) {
  71.                 entry = entries.nextElement();
  72.                 InputStreamContent content = new InputStreamContent(
  73.                         zis.getInputStream(entry));
  74.                 content.setLength(entry.getSize());
  75.                 content.setLastModified(new Date(entry.getTime()));
  76.                 content.setContentType(entry.getComment());
  77.                 out.onContent(id, entry.getName(), content);
  78.             }
  79.    
  80.             // finish sending product
  81.             out.onSignature(id, product.getSignature());
  82.             out.onEndProduct(id);
  83.         } finally {
  84.             try {
  85.                 zis.close();
  86.             } catch (Exception ignore) {
  87.             }
  88.         }
  89.     }

  90.     /**
  91.      * Free any resources associated with this handler.
  92.      */
  93.     @Override
  94.     public void close() {
  95.         this.zip = null;
  96.     }

  97. }