I had the same problem! Luckily for me, I was able to solve it.
I first reset the blob data in the database, and then used java code to zip it using ZipInputStream. Although I'm not sure, ZipEntry's null issue could be due to two things:
1. Blob data in the database is not saved correctly (or may already be compressed, some databases compress blob data during storage, you can do this too).
2. I / O streams can also cause problems, see this
Here is a detailed description of what I did:
1. reset the blob field in the database using EMPTY_BLOB and commit the changes
2. used the java program below to update the blob field with the .xls file
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver ()); // register driver Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@my-local-database:1521:test", "test1", "test1"); // It faster when auto commit is off: conn.setAutoCommit (false); try { PreparedStatement pstmt = conn.prepareStatement("update content set file_content = ? where CONTENT_ID=2006"); File blob = new File("C:/Users/ankur/Desktop/Book1.xls"); FileInputStream in = new FileInputStream(blob); pstmt.setBinaryStream(1, in); pstmt.executeUpdate(); conn.commit(); conn.close(); System.out.println("file updated"); } catch (SQLException e) { e.printStackTrace(); }
Please note that the above code will work, but it absolutely does not demonstrate coding standards and methods.
3. The zip method used below for data compression
public byte[] zipByteArray(String primaryKey, byte[] input) throws IOException{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); ZipEntry entry = new ZipEntry(primaryKey); entry.setSize(input.length); zos.putNextEntry(entry); zos.write(input); zos.closeEntry(); zos.close(); return baos.toByteArray(); }
The above method takes a byte array, encrypts it, puts it in a ByteArrayOutputStream. You can use ByteArrayOutputStream itself, due to some requirements I convert it to an array of bytes.
4. Then I insert the above byte array into the blob field using the prepared statement
5. If I use the unzip code below, it works great!
public byte[] unzipInputStream(InputStream is) throws IOException { ByteArrayOutputStream byteArrayOutputStream = null; ZipInputStream zipIs = new ZipInputStream(new BufferedInputStream(is)); byteArrayOutputStream = new ByteArrayOutputStream(); ZipEntry entry = zipIs.getNextEntry(); while (entry != null) { byte[] tmp = new byte[2048]; BufferedOutputStream bos = null; bos = new BufferedOutputStream(byteArrayOutputStream); int size = 0; while ((size = zipIs.read(tmp)) != -1) { bos.write(tmp, 0, size); } bos.flush(); bos.close(); entry = zipIs.getNextEntry(); } zipIs.close(); return byteArrayOutputStream.toByteArray();
The output of the above method is the unpacked data.
tewari2312
source share