0

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.

nasir
  • 107
  • 2
  • 14
  • This is [an example](https://stackoverflow.com/a/59250531/1509264) of how to do it inside the database. You can probably adapt it to do it outside of the database. – MT0 Feb 12 '20 at 15:45
  • 1
    Why are you converting binary data to a `String` (which can corrupt it, because not all bytes will yield valid characters), and then convert that string back to bytes (which again might lose information due to character set constraints)? Write the bytes from the input stream directly to the file without going to string and back to bytes. Also, you say the contents is a zip file, so why are you writing it to a file with the `.xml` extension? – Mark Rotteveel Feb 12 '20 at 16:04
  • @MarkRotteveel thanks, i changed the part where i was doing the conversion of bytes to string and vice versa, now the code is working fine and i'm able to get the desired output. – nasir Feb 13 '20 at 07:27

1 Answers1

0

Below is the working code for above requirement,

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();
        }
    }
  }
}
nasir
  • 107
  • 2
  • 14