zip - Java SE 6 - Trouble recognizing directories with ZipEntry -
i've problem unzipping files directory written without "/" @ end of name.
the code works fine if file content is:
basedir/ basedir/file1.txt basedir/file2.txt when central directory doesn't contain "/" @ end of name, got error. in case content of zip file is:
basedir basedir/file1.txt basedir/file2.txt here code used unzip file
public static void unzip(final string zipfilename, final string unzipdir) throws zipexception { zipfile zipfile = null; try { final file directory = new file(unzipdir); // check if directory extract exists if(!directory.exists()) { // if not, create new one. new file(unzipdir).mkdir(); } final file checkfile = new file(zipfilename); if (checkfile.length() == 0) { throw new ioexception("empty file"); } zipfile = new zipfile(zipfilename); enumeration<?> e = zipfile.entries(); while(e.hasmoreelements()) { final zipentry entry = (zipentry)e.nextelement(); final file destinationfile = new file(unzipdir,entry.getname()); //create directories if required. destinationfile.getparentfile().mkdirs(); //if entry directory, leave it. otherwise extract it. if(entry.isdirectory()) { continue; } else { // inputstream current entry of zip file using inputstream = zipfile.getinputstream(entry); fileoutputstream fos = new fileoutputstream(destinationfile); writefile(is, fos); } } } catch(exception ex) { throw new zipexception("error", ex); } { if(zipfile!=null) { try { zipfile.close(); } catch(exception ex) { throw new zipexception("error", ex); } } } } i found problem zipentry.isdirectory() method works if name of entry in zip ends "/" ( http://docs.oracle.com/javase/6/docs/api/java/util/zip/zipentry.html#isdirectory%28%29 ), doesn't work in second scenario. i'm not able find fix beaviour. how can check if entry directory without using isdirectory method? (please note can not use third party library)
Comments
Post a Comment