ZipUtilities.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 
00026 package org.openmobileis.common.util.file;
00027 
00028 import java.io.*;
00029 import java.util.zip.*;
00030 
00031 
00039 public class ZipUtilities {
00040 
00044   public static void unZipData(InputStream inputstream, String unZipDirectory) throws IOException  {  
00045         BufferedInputStream bis = new BufferedInputStream(inputstream);  
00046     ZipInputStream zipFile = new ZipInputStream(bis);
00047     ZipEntry entry;
00048     while ( (entry = zipFile.getNextEntry()) != null) {
00049       String completeName = entry.getName();
00050       // get file directory and test if exist.
00051       String plateformFileName = FileUtilities.convertFileNameToSystem(completeName);
00052       if (entry.isDirectory()) {
00053           File dirToCreate = new File(unZipDirectory+plateformFileName);
00054           if (!dirToCreate.exists()) {
00055                   dirToCreate.mkdirs();
00056           }
00057           zipFile.closeEntry();
00058           continue;
00059       }
00060       int index = completeName.lastIndexOf("/");
00061       if (index != -1)  {
00062         File repToCreate = new File(unZipDirectory+plateformFileName.substring(0, index));
00063         if (!repToCreate.exists())  {
00064           repToCreate.mkdirs();
00065         }
00066       }
00067       
00068 
00069       FileOutputStream stream = new FileOutputStream(unZipDirectory+plateformFileName);
00070       BufferedOutputStream bos = null;
00071       try {
00072         int read_bytes = 0;
00073         byte[] buffer = new byte[4096];
00074         bos = new BufferedOutputStream(stream, buffer.length);
00075         //LogManager.traceDebug(0, "DEBUG TIME in ApacheHTTPCientSynchroConduit dans  unZipData() av while : "+new Date(System.currentTimeMillis()));
00076         while ( (read_bytes = zipFile.read(buffer, 0, buffer.length)) != -1) {
00077                 bos.write(buffer,0, read_bytes);                
00078         }
00079         //LogManager.traceDebug(0, "DEBUG TIME in ApacheHTTPCientSynchroConduit dans  unZipData() ap while : "+new Date(System.currentTimeMillis()));
00080       } finally {
00081         bos.flush();
00082         bos.close();
00083         stream.close();
00084       }
00085       zipFile.closeEntry();
00086     }
00087     zipFile.close();
00088   }
00089   
00090   public static void unzipEntryFromDataToDestFile(InputStream inputstream, String entryName, String destFile) throws IOException {
00091       ZipInputStream zipFile = new ZipInputStream(inputstream);
00092       ZipEntry entry;
00093       while ( (entry = zipFile.getNextEntry()) != null) {
00094         String completeName = entry.getName();
00095         if (completeName.equals(entryName))  {
00096             FileOutputStream stream = new FileOutputStream(destFile);
00097             try {
00098               int read_bytes = 0;
00099               byte[] buffer = new byte[10000];
00100               while ( (read_bytes = zipFile.read(buffer)) != -1) {
00101                 stream.write(buffer,0, read_bytes);
00102               }
00103             } finally {
00104               stream.close();
00105             }
00106         }
00107         zipFile.closeEntry();
00108       }
00109       zipFile.close();
00110   }
00111 
00112 
00113   public static byte[] getZipEntryFromStreal(InputStream inputstream, String entryName) throws IOException  {
00114     ZipInputStream zipFile = new ZipInputStream(inputstream);
00115     ZipEntry entry;
00116     byte[] outData = null;
00117     while ( (entry = zipFile.getNextEntry()) != null) {
00118       String completeName = entry.getName();
00119       if (completeName.equals(entryName))  {
00120         ByteArrayOutputStream stream = new ByteArrayOutputStream();
00121         try {
00122           int read_bytes = 0;
00123           byte[] buffer = new byte[10000];
00124           while ( (read_bytes = zipFile.read(buffer)) != -1) {
00125             stream.write(buffer,0, read_bytes);
00126           }
00127         } finally {
00128           stream.close();
00129         }
00130         outData = stream.toByteArray();
00131         zipFile.closeEntry();
00132         break;
00133       }
00134       zipFile.closeEntry();
00135     }
00136     zipFile.close();
00137     return outData;
00138   }
00139 
00140 }

Generated on Mon Jan 11 21:19:17 2010 for OpenMobileIS by  doxygen 1.5.4