5

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
}
satheesh kumar
  • 139
  • 1
  • 2
  • 8
  • 1
    As written, your question is very unclear. Do you want to extract files from the ZIP or not? What *specific problem* are you trying to solve? – parsifal Feb 08 '13 at 18:32
  • bye the way: Navigation Sytems do it the same way, they keep their digital road maps data compressed in main memory, and unzip parts on demand – AlexWien Feb 08 '13 at 18:47

3 Answers3

5

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);
AlexWien
  • 28,470
  • 6
  • 53
  • 83
5

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?

Robert
  • 39,162
  • 17
  • 99
  • 152
user000001
  • 32,226
  • 12
  • 81
  • 108
2

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"));
fvu
  • 32,488
  • 6
  • 61
  • 79
  • Thats not the answer, he wants the zip in compressed form in a variable. Quote: I don't want to extract on disk the zip file every time and then read its data – AlexWien Feb 08 '13 at 18:45
  • @AlexWien It doesn't, it just allows to read the content (byte by byte or as shown line by line) **without expanding** the whole file at once, and it was my understanding that that's what OP is after. I merely wanted to point out an alternative to zipping up files that is easier to work with than a zip file. Storing a zip file as such in a variable imo has no practical application. – fvu Feb 08 '13 at 18:52
  • Its unclear, what he wanted, maybe he wanted that what your solution is, unzip whitout creating a new file on disk. Its not always silly to keep a zip file in a avariable: Its ways fasther to unzip from a byte array than reading from a file: Imagine you must unzip 100 times the second, you cannot do that with disk acces, on an ebedded device with low cache. There are many application: e.g map matching systems, that calculate the track that a vehicle was driving in Two Big countries: Reading from disk would be to slow – AlexWien Feb 08 '13 at 18:57