1 25 26 package net.yagga.util; 27 28 import java.io.*; 29 import java.util.zip.*; 30 import java.util.*; 31 32 50 public class Unzip { 51 52 55 private Vector extractedFiles; 56 private Vector createdDirs; 57 58 public static void main(String argv[]){ 59 Unzip u=new Unzip(argv[0],argv[1]); 60 int ret; 64 int acc=0; 65 int sg=3; 66 while((ret=u.chunkuncompress(false,true,true))!=-1){ 67 acc=ret; 68 if(acc>=sg){ 69 System.out.print("."); 70 sg+=3; 71 } 72 } 73 } 74 75 78 public Vector getExtractedFilesList(){ 79 return extractedFiles; 80 } 81 84 public Vector getCreatedDirsList(){ 85 return createdDirs ; 86 } 87 88 String zipFile; 89 String destDir; 90 long maxSize=0; 92 long currSize=0; 93 94 public Unzip(String zFile, String dDir) { 95 zipFile=zFile; 96 extractedFiles=new Vector(); 97 createdDirs=new Vector(); 98 destDir=dDir; 99 getStats2(); 100 startChunking(); 101 } 102 103 public long getMax(){ 104 return maxSize; 105 } 106 107 private void getStats(){ 108 109 ZipFile zip=null; 110 try{ 111 zip=new ZipFile(zipFile); 112 Enumeration e=zip.entries(); 113 while(e.hasMoreElements()){ 114 ZipEntry ze=(ZipEntry)e.nextElement(); 115 maxSize+=ze.getSize(); 116 } 117 zip.close(); 119 }catch(IOException ioe){ 120 Ut.error("Error reading zip file '"+zipFile+"':"+ioe); 121 System.exit(0); 122 } 123 124 } 125 126 private void getStats2(){ 127 ZipInputStream zis=getZIS(); 128 try{ 129 ZipEntry ze=null; 130 while((ze=zis.getNextEntry())!=null){ 131 maxSize+=ze.getSize(); 132 } 133 zis.close(); 135 } 136 catch(ZipException ze){ 137 Ut.error("Error reading Zip file '"+zipFile+"':"+ze); 138 System.exit(0); 139 } 140 catch(IOException ioe){ 141 Ut.error("Error reading zip file '"+zipFile+"':"+ioe); 142 System.exit(0); 143 } 144 145 } 146 147 public void startChunking(){ 148 currSize=0; 149 } 150 169 public void uncompress(ZipEntry _ze, boolean verbose){ 170 ZipInputStream zis=getZIS(); 171 ZipEntry ze=null; 172 try{ 173 while ((ze=zis.getNextEntry())!=null) { 174 if (ze==_ze) { 175 int size=(int)ze.getSize(); 176 if (size==-1) { 178 Ut.error("Unknown size for "+ze); 179 return; 180 } 181 byte[] b=new byte[(int)size]; 182 int rb=0; 183 int chunk=0; 184 while (((int)size - rb) > 0) { 185 chunk=zis.read(b,rb,(int)size - rb); 186 if (chunk==-1) { 187 break; 188 } 189 rb+=chunk; 190 } 191 writeUncompressedFile(ze,b,verbose); 194 195 return; 196 } 197 } 198 } 199 catch(IOException ioe){ 200 Ut.error(" decompressing "+ze+":"+ioe); 201 } 202 } 203 204 208 public void uncompress(boolean verbose){ 209 try { 210 214 ZipInputStream zis=getZIS(); 215 ZipEntry ze=null; 216 while ((ze=zis.getNextEntry())!=null) { 217 if (ze.isDirectory()) { 218 continue; 219 } 220 221 int size=(int)ze.getSize(); 222 if (size==-1) { 224 Ut.error("Unknown size for "+ze); 225 return; 226 } 227 byte[] b=new byte[(int)size]; 228 int rb=0; 229 int chunk=0; 230 while (((int)size - rb) > 0) { 231 chunk=zis.read(b,rb,(int)size - rb); 232 if (chunk==-1) { 233 break; 234 } 235 rb+=chunk; 236 } 237 writeUncompressedFile(ze,b,verbose); 240 } 241 } catch (NullPointerException e) { 242 Ut.error("zip: done."); 243 } catch (FileNotFoundException e) { 244 e.printStackTrace(); 245 } catch (IOException e) { 246 e.printStackTrace(); 247 } 248 } 249 250 private ZipInputStream getZIS(){ 251 InputStream is=ResourceMgr.openResource(zipFile); 252 return new ZipInputStream(is); 253 } 254 257 ZipInputStream chunkzis=null; 258 259 268 public int chunkuncompress(boolean verbose, boolean acc, boolean percent){ 269 try { 270 if(chunkzis==null){ 272 chunkzis=getZIS(); 273 } 274 ZipEntry ze=null; 275 if((ze=chunkzis.getNextEntry())!=null) { 277 if (ze.isDirectory()) { 278 if(acc){ 279 if(percent) 280 return (int)(currSize*100.0f/(float)maxSize); 281 else 282 return (int)currSize; 283 } 284 return 0; 285 } 286 287 int size=(int)ze.getSize(); 288 if (size==-1) { 290 Ut.error("Unknown size for "+ze); 291 return -1; 292 } 293 byte[] b=new byte[(int)size]; 294 int rb=0; 295 int chunk=0; 296 while (((int)size - rb) > 0) { 297 chunk=chunkzis.read(b,rb,(int)size - rb); 298 if (chunk==-1) { 299 break; 300 } 301 rb+=chunk; 302 } 303 writeUncompressedFile(ze,b,verbose); 306 if(acc) 307 { 308 currSize+=size; 309 if(percent){ 310 float perc=(currSize*100.0f/(float)maxSize); 311 return (int)perc; 313 } 314 else 315 return (int)currSize; 316 } 317 else{ 318 if(percent) 319 return (int)((float)size*100.0f/maxSize); 320 else 321 return size; 322 } 323 } } catch (NullPointerException e) { 325 Ut.error("zip:done."); 326 } catch (FileNotFoundException e) { 327 e.printStackTrace(); 328 } catch (IOException e) { 329 e.printStackTrace(); 330 } 331 return -1; 332 } 333 334 341 private void writeUncompressedFile(ZipEntry ze, byte[] buffer, boolean verbose){ 342 if(ze.isDirectory()) 345 return; 346 File f=new File(destDir); 347 File n=new File(f.getAbsolutePath()+File.separator+ze.getName()); 348 File dir=n.getParentFile(); 350 if(!dir.exists()){ 351 createdDirs.add(dir); 353 if(!dir.mkdirs()){ 354 Ut.error("Error: impossible to create directory(ies)"+dir); 355 return; 356 } 357 } 358 try{ 359 if(Ut.writeBinFile(n.getCanonicalPath(),buffer,true)) { 360 extractedFiles.add(n); 361 if(verbose) 362 Ut.infoln(ze.getName()+"> "+n.getCanonicalPath()); 363 } 364 else 365 Ut.error("Error uncompressing/writing to file "+n); 366 } 367 catch(IOException ioe){ 368 Ut.error("Error uncompressing/writing to file "+n+":"+ioe); 369 } 370 } 371 372 } 373 374 375 376 | Popular Tags |