0

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?

FunnyJava
  • 446
  • 1
  • 6
  • 17
  • Wrong charset maybe? – Raymond Nijland Sep 07 '17 at 10:48
  • @RaymondNijland no... – FunnyJava Sep 07 '17 at 12:40
  • 1
    If the code is okay (runs on diferent servers) then it's likely to be something wrong with the server... Did you check file path for access / free storange and the many quirks possible there? – Jan Sep 07 '17 at 16:53
  • @Jan the only difference I can think of that maybe changes the way the zip is fetched from the db is that the environment in which the problematic scenario takes place is clustered. All the other servers are not. Can something like that create a problem? – FunnyJava Sep 08 '17 at 10:41

1 Answers1

0

You are both calling getBytes(), which reads out the blob, and then reading from its input stream, which by this stage will be empty.

Get rid of the getBytes() call.

user207421
  • 305,947
  • 44
  • 307
  • 483