從數據庫中讀取 zip 存檔 (Reading zip archive from database)


問題描述

從數據庫中讀取 zip 存檔 (Reading zip archive from database)

I have a zip file which I store in the database as a blob field. When I want to download it from it the zip file is corrupted. I can open it only from 7zip. The file is ok when I try to open it before upload it in the DB and when is in the DB. When I retrieve the file  from the database as a blob I get this error when try to unzip it on unix

    Archive:  test.zip
      End‑of‑central‑directory signature not found.  Either this file is not
      a zipfile, or it constitutes one disk of a multi‑part archive.  In the
      latter case the central directory and zipfile comment will be found on
      the last disk(s) of this archive.
    unzip:  cannot find zipfile directory in one of test.zip or
            test.zip.zip, and cannot find test.zip.ZIP, period.

Here is the code when I retrieve the zip from the database : 

        public oracle.sql.BLOB GetBlob(Connection myConn, 
                                       CallableStatement cstmt) throws Exception {
            String strSql = null;

            BLOB tempBlob = null;
            try {

                strSql = .... // Here is the sql procedure which I called to retrieve the blobl field.
                cstmt = myConn.prepareCall(strSql);
                cstmt.registerOutParameter(1, OracleTypes.BLOB);
                cstmt.setLong(2, request_id);
                cstmt.execute();
                tempBlob = (oracle.sql.BLOB)cstmt.getObject(1);
                int bufsize = tempBlob.getBufferSize();

            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
            return tempBlob;

Here is the reading : 

                oracle.sql.BLOB tempBlob = null;
            Connection myConn = null;
            CallableStatement cstmt = null;

            try {
                myConn = DBHelper.getConnection();
                if (null == myConn)
                    throw new SQLException();
                tempBlob = GetBlob(myConn, cstmt);

                int bufsize = tempBlob.getBufferSize();
                InputStream in = tempBlob.getBinaryStream();
                int length = 0;

                byte buf[] = new byte[bufsize];
                while ((in != null) && ((length = in.read(buf)) != ‑1)) {
                    out.write(buf, 0, length);

                }
                in.close();
                //          out.flush();
                //          out.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != myConn) {
                    try {
                        myConn.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (cstmt != null) {
                    try {
                        cstmt.close();
                    } catch (SQLException e) {
                    }
                }

            }

Could somebody help me.

Thanks in advance.

‑‑‑‑‑

參考解法

方法 1:

Compare the files before and after. The difference should give you some hint what is going wrong.

Possible culprits are:

  • Missing bytes at the end
  • converted bytes
  • messed up order of bytes

I'd expect looking at the first 10, the last 10 and the total number of bytes should be sufficient to give you a good idea what is going on.

(by Jordan BorisovJens Schauder)

參考文件

  1. Reading zip archive from database (CC BY‑SA 3.0/4.0)

#java #blob #zip #oracle






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論