Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZipEntry to byte array

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?

like image 958
SaintLike Avatar asked Jan 20 '26 19:01

SaintLike


1 Answers

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.

like image 125
Rob Avatar answered Jan 23 '26 09:01

Rob