2012-08-23 33 views
5

Tengo una Object comoarchivo zip de Java creado pero no la apertura, dice final inesperado de archivo

private String name; 
private int age; 
private String country; 
// getters and setters 

y funciones son

protected void write(@Nonnull final Document document, @Nonnull final OutputStream stream) throws PersistenceException { 
     try { 
      jaxbContext.createMarshaller().marshal(document, stream); 
     } catch (final JAXBException e) { 
      LOGGER.error(e.getMessage(), e); 
      throw new PersistenceException("Failed to marshall document " + docment.getUniqueId() + ": " + e.getMessage(), e); 
     } 
    } 

que convertir esto en zip archivo como

  ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      write(document, stream); 
      GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new FileOutputStream(new File(getOutputFilePath(document.getUniqueId())))); 
      gzipOutputStream.write(stream.toByteArray()); 

Esto crea el archivo zip, pero cuando intento abrirlo, dice

gzip: document.xml.gz: unexpected end of file 

¿Qué es lo que no estoy haciendo aquí?

+3

¿Cierras abierto? – gkuzmin

+0

eso fue, hice 'flush()' y 'close()' y funcionó, gracias por señalar que – daydreamer

Respuesta

11

usted necesita para asegurarse de que llamar:

gzipOutputStream.flush(); 

y flujo de salida gzip finalmente

gzipOutputStream.close(); 
+0

lo agregué y funcionó gracias @Jon – daydreamer

Cuestiones relacionadas