UTLZip

Here is an alternative way to compress file using Java

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED “UTLZip” AS
import java.util.zip.*;
import java.io.*;
public class UTLZip
{
public static void compressFile(String infilename, String
outfilename)
{
try
{
FileOutputStream fout = new FileOutputStream(outfilename);
ZipOutputStream zout = new ZipOutputStream(fout);
ZipEntry ze = new ZipEntry((new File(infilename)).getName());
try
{
FileInputStream fin = new FileInputStream(infilename);
zout.putNextEntry(ze);
copy(fin, zout);
zout.closeEntry();
fin.close();
}
catch (IOException ie)
{
System.out.println(“IO Exception occurred: ” + ie);
}
zout.close();
}
catch(Exception e)
{
System.out.println(“Exception occurred: ” + e);
}
}

public static void copy(InputStream in, OutputStream out)
throws IOException
{
byte[] buffer = new byte[4096];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}
}
}
/

CREATE OR REPLACE PACKAGE UTLZip
IS
PROCEDURE compressFile (p_in_file IN VARCHAR2, p_out_file IN
VARCHAR2)
AS
LANGUAGE JAVA
NAME ‘UTLZip.compressFile(java.lang.String,
java.lang.String)’;
END;
/

SQL> exec UTLZip.compressFile(‘/home/oracle/test.txt’,’/home/oracle/test.zip’)

You should grant permission to read,write and execute in a directory using dbms_java.grant_permission before use it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s