【问题标题】:Reading zip archive from database从数据库中读取 zip 存档
【发布时间】:2012-08-28 17:07:37
【问题描述】:

我有一个 zip 文件,我将它作为 blob 字段存储在数据库中。当我想从中下载它时,zip 文件已损坏。我只能从 7zip 打开它。当我尝试在将其上传到数据库之前打开它以及何时在数据库中时,该文件是好的。当我从数据库中以 blob 形式检索文件时,尝试在 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.

这是我从数据库中检索 zip 时的代码:

        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;

这是阅读:

                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) {
                    }
                }

            }

谁能帮帮我。

提前致谢。

【问题讨论】:

  • 检查 BLOB 字段大小是否等于文件。如果它相等,那么问题就出在@Vadzim 所说的阅读上。如果它较低,那么问题就出在写作上。
  • 这个zip文件可以jar xvf *.zip解压吗?

标签: java oracle zip blob


【解决方案1】:

比较之前和之后的文件。差异应该可以提示您出了什么问题。

可能的罪魁祸首是:

  • 末尾缺少字节
  • 转换后的字节数
  • 字节顺序混乱

我希望查看前 10 个、后 10 个和总字节数应该足以让您很好地了解发生了什么。

【讨论】:

  • 所以问题在于将 zip 文件写入 blob 的输入流中,谢谢 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 2020-03-11
相关资源
最近更新 更多