satheesh kumar
satheesh kumar

Reputation: 139

How to read content of the Zipped file without extracting in java

I have file with names like ex.zip. In this example, the Zip file contains only one file with the same name(ie. `ex.txt'), which is quite large. I don't want to extract the zip file every time.Hence I need to read the content of the file(ex.txt) without extracting the zip file. I tried some code like below But i can only read the name of the file in the variable.

How do I read the content of the file and stores it in the variable?

Thank you in Advance

fis=new FileInputStream("C:/Documents and Settings/satheesh/Desktop/ex.zip");
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;

while((entry = zis.getNextEntry()) != null) {
    i=i+1; 
    System.out.println(entry);
    System.out.println(i);
    //read from zis until available
}

Upvotes: 5

Views: 28926

Answers (3)

user000001
user000001

Reputation: 33387

Try this:

    String zipFile = "ex.zip";
    try (ZipFile zip = new ZipFile(zipFile)) {
        int i = 0;
        for (Enumeration<? extends ZipEntry> e = zip.entries(); e.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            System.out.println(entry);
            System.out.println(i);

            InputStream in = zip.getInputStream(entry);
        }
    }

For example, if the file contains text, and you want to print it as a String, you can read the InputStream like this: How do I read / convert an InputStream into a String in Java?

Upvotes: 5

AlexWien
AlexWien

Reputation: 28767

Your idea is to read the zip file as it is into a byte array and store it in a variable. Later when you need the zip you extract it on demand, saving memory:

First read the content of the Zip file in a byte array zipFileBytes

If you have Java 1.7:

Path path = Paths.get("path/to/file");
byte[] zipFileBytes= Files.readAllBytes(path);

otherwise use Appache.commons lib

byte[] zipFileBytes;
zipFileBytes = IOUtils.toByteArray(InputStream input);

Now your Zip file is stored in a variable zipFileBytes, still in compressed form.

Then when you need to extract something use

ByteArrayInputStream bis = new ByteArrayInputStream(zipFileBytes));
ZipInputStream zis = new ZipInputStream(bis);

Upvotes: 5

fvu
fvu

Reputation: 32973

I think that in your case the fact that a zipfile is a container that can hold many files (and thus forces you to navigate to the right contained file each time you open it) seriously complicates things, as you state that each zipfile only contains one textfile. Maybe it's a lot easier to just gzip the text file (gzip is not a container, just a compressed version of your data). And it's very simple to use:

GZIPInputStream gis = new GZIPInputStream(new FileInputStream("file.txt.gz"));
// and a BufferedReader on top to comfortably read the file
BufferedReader in = new BufferedReader(new InputStreamReader(gis) );

Producing them is equally simple:

GZIPOutputStream gos = new GZIPOutputStream(new FileOutputStream("file.txt.gz"));

Upvotes: 2

Related Questions