title: How to extract an XML document from a BLOB column in Oracle thats been compressed in Java
body: <p>I have a table in Oracle 11G (11.1) that has a BLOB column containing XML documents.</p>
  
  <p>The XML document has been written to the table using a java program and has been serialised and compressed using java.util.zip deflator.</p>
  
  <p>Is there a simple way to extract this document using SQL or PLSQL to reverse the process used to create the BLOB column to get back the original XML document ?</p>
  
  <p>I identified a piece of java from a web application that reads the column that looks like the code below but for support purposes, would like to be able to access the column data from the back end. </p>
  
  <pre><code>private IntegrationStagingDataBean fromObjectToBean(StagedMessage message) throws ServerException {
      IntegrationStagingDataBean bean = new IntegrationStagingDataBean();
      bean.setBusinessId(message.getBusinessId());
      bean.setBusinessType(message.getBusinessType());
      bean.setCreateTime(message.getCreateTime());
      bean.setDeleted(message.hasBeenDeleted() ? "Y" : "N");
      bean.setErrorMessage(message.getErrorMessage());
      bean.setId(message.getId());
      bean.setStoreId(message.getStoreId());
      bean.setMessageDirection(message.getMessageDirection().getDbCode());
      bean.setMessageFamily(message.getMessageFamily().getDbCode());
      bean.setMessageType(message.getMessageType().getDbCode());
      bean.setProcessed(message.hasBeenProcessed() ? "Y" : "N");
      bean.setRetryCount(message.getRetryCount());
      bean.setUpdateTime(message.getUpdateTime());
      try {
          bean.setSerializedDeo(ObjectUtils.toByteArray(new CompressedObject(message.getDeo())));
      } catch (IOException ioex) {
          throw new ServerException("Problem setting compressed serialized object", ioex);
      }
      return bean;
  }
  
  private StagedMessage fromBeanToObject(IntegrationStagingDataBean bean) throws ServerException {
      DataExchangeObject deo = null;
      Blob blob = (Blob) bean.getSerializedDeo();
      try {
          CompressedObject compressedObject = (CompressedObject) new              
                ObjectInputStream(blob.getBinaryStream()).readObject();
          deo = (DataExchangeObject) compressedObject.recoverObject();
      } catch (Exception e) {
          throw new ServerException("Couldn't uncompress/deserialize DEO from BLOB", e);
      }
  </code></pre>
  
  <p>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p>
  
  <p>and found the following additional functions spread across a handful of packages which indicate it is using the java function deflater from java.util.zip.*;</p>
  
  <p>public CompressedObject(Object object)
          throws IOException
      {
          dataToSend = null;
          if(object != null)
              dataToSend = ObjectUtils.toCompressedByteArray(object);
      }</p>
  
  <pre><code>public static final byte[] toCompressedByteArray(Object o)
      throws IOException
  {
      return compressByteArray(toByteArray(o));
  }
  
  public static final byte[] compressByteArray(byte input[])
  {
      Deflater compressor = new Deflater(9);
      compressor.setInput(input);
      compressor.finish();
      ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
      byte buf[] = new byte[1024];
      int count;
      for(; !compressor.finished(); bos.write(buf, 0, count))
          count = compressor.deflate(buf);
  
      try
      {
          bos.close();
      }
      catch(IOException e) { }
      return bos.toByteArray();
  }
  
  public static final byte[] decompressByteArray(byte arr[])
  {
      Inflater decompressor = new Inflater();
      decompressor.setInput(arr);
      ByteArrayOutputStream bos = new ByteArrayOutputStream(arr.length);
      byte buf[] = new byte[1024];
      while(!decompressor.finished()) 
          try
          {
              int count = decompressor.inflate(buf);
              bos.write(buf, 0, count);
          }
          catch(DataFormatException e) { }
      try
      {
          bos.close();
      }
      catch(IOException e) { }
      return bos.toByteArray();
  }
  </code></pre>
  
  <p>Thanks in advance.</p>