Java 从数据库读取zip存档文件

Java 从数据库读取zip存档文件,java,oracle,zip,blob,Java,Oracle,Zip,Blob,我有一个zip文件,作为blob字段存储在数据库中。当我想从它下载时,zip文件已损坏。我只能从7zip打开它。当我尝试在数据库中上传之前打开该文件时,以及当文件在数据库中时,该文件是正常的。当我从数据库中以blob形式检索文件时,在unix上尝试解压缩时会出现此错误 Archive: test.zip End-of-central-directory signature not found. Either this file is not a zipfile

我有一个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) {
                    }
                }

            }
谁能帮帮我吗


提前感谢。

比较之前和之后的文件。这一差异应该会给你一些错误的提示

可能的罪魁祸首有:

  • 结尾缺少字节
  • 转换字节
  • 混乱的字节顺序

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

检查BLOB字段大小是否等于file。如果它是相等的,那么问题就在于@Vadzim所说的阅读。如果较低,那么问题在于写入。这个zip文件可以通过
jar xvf*.zip
解压吗?所以问题在于在blob的输入流中写入zip文件,谢谢:)