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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With