Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

create zip file from multiple files using pl sql and java

KalpataruOct 6 2015 — edited Oct 7 2015

Hi All,

      I have written the below java code in oracle database 11gr2.

      

DROP JAVA SOURCE SCOTT."CreateZip";

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED SCOTT."CreateZip"

as

  import java.io.BufferedInputStream;

  import java.io.BufferedOutputStream;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileNotFoundException;

  import java.io.FileOutputStream;

  import java.io.IOException;

  import java.util.zip.Deflater;

  import java.util.zip.ZipEntry;

  import java.util.zip.ZipInputStream;

  import java.util.zip.ZipOutputStream;

 

  public class CreateZip

  { 

   

    //public static final int BUFFER_SIZE = 4096;

   

    public static void Zip(String zipFileName, String zipEntries) throws IOException

    {

     

      //String strName = zipEntries;

      String[] srcFiles = new String[] {zipEntries};

      //String[] srcFiles = {zipEntries};

      

      try

      {

          byte[] buffer = new byte[4096];

          FileOutputStream fos = new FileOutputStream(zipFileName);

          ZipOutputStream zos = new ZipOutputStream(fos);       

     

          for (int i = 0; i < srcFiles.length; i++)

          {

              File entryFile = new File(srcFiles[i]);

              FileInputStream fis = new FileInputStream(entryFile);

              zos.putNextEntry(new ZipEntry(entryFile.getName()));

              int length;

             

              while ((length = fis.read(buffer)) > 0)

              {

               zos.write(buffer, 0, length);

              }

              zos.closeEntry();

              // close the InputStream

              fis.close();

          }

          // close the ZipOutputStream

          zos.close();

      }

      catch (IOException e)

      {

       e.printStackTrace();

      }

      

    }

   

    public static void UnZip(String zipFilePath, String destDirectory) throws IOException

    {

    

     try

      {

       byte[] buffer = new byte[4096];  

       //create output directory is not exists

        File folder = new File(destDirectory);

        if(!folder.exists())

        {

            folder.mkdir();

        }

        //get the zip file content

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath));

        //get the zipped file list entry

        ZipEntry ze = zis.getNextEntry();

       

        while(ze!=null)

        {

          String fileName = ze.getName();

          File newFile = new File(destDirectory + File.separator + fileName);

          //create all non exists folders

          //else you will hit FileNotFoundException for compressed folder

          new File(newFile.getParent()).mkdirs();

          FileOutputStream fos = new FileOutputStream(newFile);

          int len;

          while ((len = zis.read(buffer)) > 0)

             {

              fos.write(buffer, 0, len);

             }

            

          fos.close();  

          ze = zis.getNextEntry();

         

        }

        zis.closeEntry();

        zis.close();

      }

      catch (IOException e)

      {

       e.printStackTrace();

      }

     

    }

   

   

  }

/

and the pl/sql wrapper is below.

CREATE OR REPLACE PACKAGE SCOTT.CreateZip AS

PROCEDURE UnZip(Param1 VARCHAR2, Param2 VARCHAR2)

AS

  LANGUAGE java

    NAME 'CreateZip.UnZip(java.lang.String, java.lang.String)';

PROCEDURE Zip(Param1 VARCHAR2, Param2 VARCHAR2)

AS

  LANGUAGE java

    NAME 'CreateZip.Zip(java.lang.String, java.lang.String)';

end;

/

but my problem is Unzip code is working file while the Zip function is not working.

i passing the path with file name like this for creating array in java into the Zip function

Zip('D:\EXCEL_ORACLE_DIR\Test.zip','D:\EXCEL_ORACLE_DIR\abc.txt,D:\EXCEL_ORACLE_DIR\abc.docx').

the zip is creating with zero byte size where is the problem.

This post has been answered by PeterValencic on Oct 7 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 4 2015
Added on Oct 6 2015
5 comments
3,323 views