Unzip a single file from a ZIP file with multiple files in android

I am trying to get only one file (I know its name) from a very large zip archive. This archive includes about 100,000 files because I do not want to find my file in a loop. I think it should be some kind of solution for this case, something like a command on Linux.

unzip archive.zip myfile.txt 

I wrote the following code

 try { FileInputStream fin = new FileInputStream(rootDir+"/archive.zip"); ZipInputStream zin = new ZipInputStream(fin); ZipEntry ze = new ZipEntry("myfile.txt"); FileOutputStream fout = new FileOutputStream(rootDir+"/buff/" + ze.getName()); for (int c = zin.read(); c != -1; c = zin.read()) { fout.write(c); } zin.closeEntry(); fout.close(); zin.close(); } catch(Exception e) { Log.e("Decompress", "unzip", e); } 

This code creates a new file in the buff directory, but this file is empty! Please help me with this problem!

Thank you for your time!

+4
source share
1 answer

I am new to Java, but the API documentation contains a fairly reasonable amount of information for standard Java libraries, including java.util.zip . Going from there to the ZipFile entry can be scrolled down to the list of methods to find the getEntry method. This seems to be the route you should start with!

EDIT: remember that when making a call, you will probably need to include a directory (for example: "dir \ subdirF \ subdirW \ FileThatYouWant.txt"), since it looks like the files are called when you go one by one.

EDIT 2: There is significant information: Data compression and decompression using the Java API , if you are ready to read a little: D. Depending on memory limitations, the only reasonable solution for you might be to use a ZipInputStream object, which AFAIK will require you to go through each ZipEntry in archive (average 50,000?), but you don’t need to load the entire file into memory. In terms of performance, I would suggest that manual switching will be as efficient as any current implementation of this niche functionality.

+3
source

All Articles