Java将文件从源zip压缩到Oracle数据库

时间:2017-07-10 12:23:05

标签: java oracle zip

将Word DOCX文件存储在表格(zip文件)中。使用Java存储过程,我喜欢替换“word / document.xml”文件中的一些字符串并将所有文件存储到新的存档中。如果我在Oracle数据库上运行代码,我会收到损坏的zip文件。 ouptut文件的文件大小小于输入文件... 如果我在客户端(Netbeans IDE)上运行相同的应用程序,那么一切正常。我找不到问题在哪里!?

 public static void zamenjajVsebino(oracle.sql.BLOB srcBlob1, oracle.sql.BLOB[] dstBlob) throws Exception {

    InputStream zipBuffer = srcBlob1.getBinaryStream();
    OutputStream outBuffer = dstBlob[0].getBinaryOutputStream();

    ZipInputStream zipIn   = new ZipInputStream(zipBuffer);
    ZipOutputStream zipOut = new ZipOutputStream(outBuffer);
    ZipEntry inEntry;

    while ((inEntry = zipIn.getNextEntry()) != null) {

        ZipEntry outEntry = new ZipEntry(inEntry.getName());
        zipOut.putNextEntry(outEntry);


        if (inEntry.getName().equals("content.xml") | inEntry.getName().equals("word/document.xml")) {
            String contentIn = new String(getStringByte(zipIn), "UTF-8");


            contentIn = contentIn.replaceAll("%TEXT%", "BLA BLA");

            zipOut.write(contentIn.getBytes(), 0, contentIn.getBytes().length);
        } else {
            copy(zipIn, zipOut);
        }

        zipOut.closeEntry();
    }

    zipIn.close();
    zipOut.flush();
    zipOut.finish();

}

 public static void copy(InputStream in, OutputStream out) throws IOException {

    int n;
    byte[] buffer = new byte[1024];
    while ((n = in.read(buffer)) > -1) {
        out.write(buffer, 0, n);   // Don't allow any extra bytes to creep in, final write
    }
    out.flush();
}

如果我打开源DOCX文件是这样的:

enter image description here

输出文件如下: enter image description here

这是我的plsql源代码:

-- select source docx file from blob...
   select vdb.vsebina
   into   SrcBlobLocator
   from   dok_vsebina_dokumenta_blob vdb
   where  id=p_vdb_id;

   -- prepare output blob
   p_vdb_id_out:=dok_lob.f_pripravi_blob;

   update dok_vsebina_dokumenta_blob set 
   naziv_datoteke='test.docx', 
   vsebina = empty_blob()
   where  id=p_vdb_id_out;

   --select output blob for update...
   select vdb.vsebina
   into   DstBlobLocator
   from   dok_vsebina_dokumenta_blob  vdb
   where  id=p_vdb_id_out for update; 


  -- call java stored procedure with source and dest blob... 
  ZamenjajVsebino(SrcBlobLocator, DstBlobLocator);

0 个答案:

没有答案