首页 > 解决方案 > 使用 Java 代码的 zip 文件中的 Blob 数据文件

问题描述

我正在尝试在 zip 文件中添加 blob 数据。但是文件在添加到 zip 文件时会损坏。下面的代码执行,但不压缩文件:

public class BlobDataExtract {
    static ZipOutputStream zos = null;
    private static ZipOutputStream zosFile;

    private static ZipOutputStream zipExtract() throws ClassNotFoundException, SQLException, IOException {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection(url, "user", "password");
        String sql = "SELECT ORIG_NM,DOC_EXT_NM,DOC_INDX_NB,DOC_BO FROM zgdt099_document";
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        java.sql.Blob docBlob = null;
        DocumentTO dto = null;
        List<DocumentTO> test = new ArrayList<DocumentTO>();
        while (rs.next()) {
            dto = new DocumentTO();
            dto.setDocIndxNb(new Long(rs.getLong(AmpoDBConstants.DOC_INDX_NB)));
            dto.setOrigNm(rs.getString(AmpoDBConstants.D_ORIG_NM));
            dto.setDocExtNm(rs.getString(AmpoDBConstants.D_DOC_EXT_NM));
            docBlob = rs.getBlob(AmpoDBConstants.D_DOC_BO);
            // String filepathzipped =rs.getString(AmpoDBConstants.D_ORIG_NM) + ".zip";
            InputStream blobStream = docBlob.getBinaryStream();
            byte[] newFile = new byte[(int) docBlob.length()];
            blobStream.read(newFile);
            blobStream.close();
            String contentType = "";
            String extName = dto.getDocExtNm();
            if ("pdf".equalsIgnoreCase(extName))
                contentType = "application/pdf";
            else if ("html".equalsIgnoreCase(extName) || "htm".equalsIgnoreCase(extName)
                    || "stm".equalsIgnoreCase(extName) || "jpeg".equalsIgnoreCase(extName)
                    || "jpg".equalsIgnoreCase(extName) || "bmp".equalsIgnoreCase(extName)
                    || "gif".equalsIgnoreCase(extName))
                contentType = "text/html";
            else if ("xls".equalsIgnoreCase(extName) || "xla".equalsIgnoreCase(extName)
                    || "xlc".equalsIgnoreCase(extName) || "xlm".equalsIgnoreCase(extName)
                    || "xlw".equalsIgnoreCase(extName) || "csv".equalsIgnoreCase(extName)
                    || "xlt".equalsIgnoreCase(extName))
                contentType = "application/vnd.ms-excel";
            else if ("doc".equalsIgnoreCase(extName) || "rtf".equalsIgnoreCase(extName)
                    || "rtx".equalsIgnoreCase(extName))
                contentType = "application/msword";
            else if ("ppt".equalsIgnoreCase(extName) || "pps".equalsIgnoreCase(extName))
                contentType = "application/vnd.ms-powerpoint";
            else if ("mpp".equalsIgnoreCase(extName))
                contentType = "application/vnd.ms-project";
            else if ("txt".equalsIgnoreCase(extName))
                contentType = "text/plain";
            else if ("zip".equalsIgnoreCase(extName))
                contentType = "application/zip";
            else if ("ics".equalsIgnoreCase(extName))
                contentType = "text/calendar";
            else if ("snp".equalsIgnoreCase(extName))
                contentType = "application/octet-stream";
            else
                contentType = "text/html";
            FileContent fileCont = new FileContent(dto.getOrigNm(), newFile, contentType);
            System.out.println("fileCont-->" + fileCont);
            dto.setDocBO(fileCont);
            test.add(dto);
            try {
                File file = new File(filePath);
                FileOutputStream fos = new FileOutputStream("C:/Users/user/Desktop/test.zip");
                zos = new ZipOutputStream(fos);
                zos.putNextEntry(new ZipEntry(dto.getDocBO().getFullName()));
                byte[] bytes = Files.readAllBytes(Paths.get(filePath));
                zos.write(bytes, 0, bytes.length);
            } catch (FileNotFoundException ex) {
                System.err.println("A file does not exist: " + ex);
            } catch (IOException ex) {
                System.err.println("I/O error: " + ex);
            }
            zos.closeEntry();
            zos.close();
        }
        return zos;
    }
}

请帮助修改此代码

标签: javafilezipblob

解决方案


您的代码似乎过于复杂。它可以简化为:

public class BlobDataExtract {
    private static void zipExtract() throws SQLException, IOException {
        String sql = "SELECT ORIG_NM,DOC_EXT_NM,DOC_INDX_NB,DOC_BO FROM zgdt099_document";
        try (
            Connection conn = DriverManager.getConnection("url", "user", "password");
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("C:/Users/user/Desktop/test.zip"));
        ) {
            while (rs.next()) {
                zos.putNextEntry(new ZipEntry(rs.getString(AmpoDBConstants.D_ORIG_NM)));
                zos.write(rs.getBytes(AmpoDBConstants.D_DOC_BO));
            }
        }
    }
}

推荐阅读