I have an zip file stored in an Oracle database table which is consisting of three different xml files. What I'm trying to do is get this zip file in my local file system. To retrieve this zip file I have written below java code:
package com.pack.IOT;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test2 {
public static Connection con;
public static PreparedStatement stmt;
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url,uname,pwd);
stmt=con.prepareStatement("select * from FileStore");
ResultSet rs=stmt.executeQuery();
while (rs.next()) {
InputStream is = rs.getBinaryStream(3);
convert(is);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void convert(InputStream is) {
FileOutputStream outputStream = null;
File file = new File("C:\\Users\\nshaik\\Desktop\\IOTZip.zip");
try {
outputStream = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result;
result = bis.read();
while (result != -1) {
buf.write((byte) result);
result = bis.read();
}
outputStream.write(buf.toByteArray());
System.out.println("File write success....");
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
outputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
The code is running fine, but I'm seeing an error "Unexpected end of archive", and this zip file should contain three different files, but I see only one file and it has a size of zero.
I'm not sure what I'm doing wrong, anyone's help would be really appreciated.