I am trying to retrieve a zip from my database, but each time the generated zip is corrupted. The zip is supposed to contain 3 pdf files, but when I generate it, it only contains the first one with size 0 and when I try to open the zip "Unexpected end of archive" error popup is displayed. I cannot figure out what's wrong, as the file in the database is not corrupted and the code is working in my PC, and many other remote servers, but not on a specific remote server (all run on wildfly 10, same mysql database configuration, with the same zip stored in database). My code is the following (JDBC):
...
Statement stmt = conn.createStatement();
ResultSet rsstmt.executeQuery("SELECT document_data from table "
+ "WHERE condition");
if (rs.next() && rs.getBytes("document_data") != null) {
ByteArrayInputStream(rs.getBytes("document_data"));
File zipped= new File("exported.zip");
FileOutputStream fos = new FileOutputStream(zipped);
byte[] buffer = new byte[1];
InputStream is = rs.getBinaryStream(1);
while (is.read(buffer) > 0) {
fos.write(buffer);
}
fos.close();
}
I tried the following code too, but didn't work either:
InputStream in = null;//zip bytes
OutputStream out;//zip archive to be generated
.
.
.
if (rs.next() && rs.getBytes("document_data") != null) {
in = new ByteArrayInputStream(rs.getBytes("document_data"));
}
out = new FileOutputStream("exported.zip");
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
When executing the following SQL query, the zip generated is NOT corrupted:
SELECT document_data INTO DUMPFILE '/tmp/exported.zip' FROM table WHERE condition;
NOTE: The zip size in database(LONGBLOB field) is 830K.
NOTE: I tried with JDBC and Hibernate, but result is the same.
Any ideas on this strange behavior?