开发者

Unzip single file from .zip file with multiple files in android

I am trying to obtain only one file (I know its name) from very large zip archive. This archive include around 100000 files because I do not want find my file in loop. I think that must be some solution for this case, something like command on Linux.

unzip archive.zip myfile.txt

I wrote following code

try  { 
   FileInputStream fin = new File开发者_如何学运维InputStream(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 create new file in buff directory, but this file is empty! Please, help me with this trouble!

Thanks for your time!


I'm fairly new to Java, but the API documentation contains a pretty reasonable amount of information for the standard Java libraries, including for java.util.zip. Going from there into the ZipFile Entry, you can scroll down to the method listing to find a method called getEntry. This seems to be the route you should start with!

EDIT: bear in mind that you will probably need to include the directory (e.g.: "dir\subdirF\subdirW\FileThatYouWant.txt") when making the call, since that seems to be the way the files are named when you go through one-by-one.

EDIT 2: a considerable wealth of information is available here: Compressing and Decompressing Data Using Java APIs, if you're willing to read a bit :D. Subject to memory constraints, the only reasonable solution for you might be to use a ZipInputStream object, which AFAIK will require you to step through each ZipEntry in the archive (on average 50,000?), but will not require you to load the entire file into memory. As far as performance, I would guess manually stepping through would be just as efficient as any current implementation of this niche functionality.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜