android - Java:Unzipping a file, without creating target folder -
i have nice java code unzips .zip file. problem code is
- i need create target folders(note:only folders not file) before running code.
- otherwise path not found exception.
so code wont work if zip file content not known before. think useless code. have better logic? or bellow code need edited?
package com.mireader; import android.os.environment; import android.util.log; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.util.zip.zipentry; import java.util.zip.zipinputstream; /** * * @author jon */ public class decompress { private string _zipfile; private string _location; public decompress(string zipfile, string location) { _zipfile = zipfile; _location = location; _dirchecker(""); } public void unzip() { try { fileinputstream fin = new fileinputstream(_zipfile); zipinputstream zin = new zipinputstream(fin); zipentry ze = null; byte[] buffer = new byte[1024]; int length; int i=0; while ((ze = zin.getnextentry()) != null) { log.v("t", ze.tostring()); log.v("decompress", "unzipping " + ze.getname()); if(ze.isdirectory()) { log.i("my","comes if"); _dirchecker(ze.getname()); } else { log.i("my","comes else"); fileoutputstream fout = new fileoutputstream(_location + ze.getname()); while ((length = zin.read(buffer))>0) { fout.write(buffer, 0, length); } zin.closeentry(); fout.close(); } } zin.close(); log.i("my tag","success"); }catch(exception e) { log.e("decompress", "unzip", e); } } private void _dirchecker(string dir) { file f = new file(_location + dir); if(!f.isdirectory()) { log.i("mytag", "creating new folder"); f.mkdirs(); system.out.print("stp:"+f.getname()); } } }
you can avoid following piece of code
if(ze.isdirectory()) { log.i("my","comes if"); _dirchecker(ze.getname()); }
and add code similar 1 below in file creator else part. worked me creating entire parent folders.
file file = createfile((basedirectory +"/" + zipfile.getname())); file.getparentfile().mkdirs();
Comments
Post a Comment