3

I was trying to serialize a ZipEntry object into a byte array and I've understood that it's not possible.

Here's what I'm doing:

ZipEntry entryToDocumentum = null;
for (ZipEntry oneEntry : entries) { //entries is a ZipEntry arraylist
   if (oneEntry.getName().equals(details.getId()+"_"+details.getCodEntidade()+"_"+details.getNrDocumento()+".pdf")) {
         entryToDocumentum = oneEntry;

   }
}
byte[] entryBytes =  serializeEntry(entryToDocumentum);

serializeEntry method:

private static byte[] serializeEntry(Object obj) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(obj); //here is where I get the NotSerializable exception
    return b.toByteArray();
}

If a ZipEntry is not serializable, how can I convert a ZipEntry to a byte array?

SaintLike
  • 9,119
  • 11
  • 39
  • 69

1 Answers1

4

ZipEntry does not implement Serializable. But that is ok, because nobody has actually had a reason to serialize an instance of ZipEntry.

You almost certainly want the bytes of the item to which the ZipEntry refers. The ZipEntry class contains metadata about a file that is in the ZipFile. To get the contents of that file, use

InputStream ZipFile.getInputStream(ZipEntry)

You can wrap the returned input stream to retrieve the data in whichever of the normal ways that makes sense for your application and read the data from there. For example, see this question for converting to a byte array.

Community
  • 1
  • 1
Rob
  • 6,247
  • 2
  • 25
  • 33