user1631306
user1631306

Reputation: 4470

read zip file with in a java jar file

I have a java application. The executable jar of this application also includes some zip and text file in it, which are read on starting of the application. I can easily deal with reading of the text file using

getResourceAsStream

, but the issue is with reading of zip file.

I tried to use following code, but that simply increased the memory usage 4 times.

      // location of the file
        InputStream is = ChemicalSynonyms.class.getClassLoader().getResourceAsStream( strFileName);
        ZipInputStream zis = new ZipInputStream(is);
        ZipEntry ze = zis.getNextEntry();
        Scanner sc = new Scanner(zis);
        String[] wordsArray;

        while (sc.hasNextLine())
        {
            // split on tab and use only the first column
            wordsArray = sc.nextLine().toLowerCase().split("\t");
            termSet.add(wordsArray[0]);
        }

        sc.close();
        zis.close();
        is.close();

How can efficiently read a zip file, which is with in the same jar file.

**** Edit**** It seems the issues lies at sc.nextLine().toLowerCase().split("\t"); I found couple of forums where they mentioned the splitting can cause consumption of lots of memory.

Upvotes: 0

Views: 2368

Answers (1)

Charles Knell
Charles Knell

Reputation: 573

Starting with a zip file, SampleText.zip, located within a java program's jar file, the following code will extract (unzip) the files in the zip file to disk. I've tested this with 2 files in the zip file. I placed the zip file in the jar file in the package/directory with the class files.

package readzipfilefromjar;

import java.lang.Class;
import java.net.URL;
import java.io.InputStream;
import java.util.zip.ZipInputStream; 
import java.util.zip.ZipEntry; 
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

/**
 * @author Charles
 * 
 * unzips zip file contained in jar 
 */
public class ReadZipFileFromJar {

    public static void main(String[] args) {
        (new UnZip()).unzip("SampleText.zip");
    }    
}

class UnZip {

    void unzip(String zipFileNameStr) {

        final int BUFFER = 2048;
        Class myClass = this.getClass();   
        InputStream inStream = myClass.getResourceAsStream(zipFileNameStr);       
        ZipInputStream zis = new ZipInputStream(inStream);
        ZipEntry ze; 
        try {
            BufferedOutputStream dest;
            while( (ze = zis.getNextEntry()) != null) {
               System.out.println("Extracting: " + ze);
               int count;
               byte data[] = new byte [BUFFER];
               // write the current file to the disk
               FileOutputStream fos = new FileOutputStream(ze.getName());
               dest = new BufferedOutputStream(fos, BUFFER);
               while ((count = zis.read(data, 0, BUFFER)) != -1) {
                   dest.write(data, 0, count);
               }
               dest.flush();
               dest.close();
            }
            zis.close();
        }
        catch (IOException e) {
            System.out.println("IOException: " + e);
        }
    }
}

Upvotes: 1

Related Questions