I'm trying to add blob data in a zip file. But files are getting corrupted while adding to a zip file. Below code executes, but not zipping the files:
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;
}
}
Please help in modifying this code
Your code seems overly complex. It can be reduced to:
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));
}
}
}
}
Issue is resolved with the below code,
public class BlobDataExtract {
static ZipOutputStream zos =null;
static String url = "jdbc:oracle:thin:@hostname:1521:SID";
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url, "user", "password");
String sql="select Blob_Data,ORIG_NM from table";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
byte[] docBlob = null;
String filename = null;
FileOutputStream fos = new FileOutputStream("C:/Users/test.zip");
zos = new ZipOutputStream(fos);
while (rs.next()) {
docBlob = rs.getBytes("Blob_Data");
filename = rs.getString("ORIG_NM");
try {
zos.putNextEntry(new ZipEntry(filename));
zos.write(docBlob, 0, Blob_Data.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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.