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 
00038 public class ZipUtilities {
00039 
00043   public static void unZipData(InputStream inputstream, String unZipDirectory) throws IOException  {
00044     ZipInputStream zipFile = new ZipInputStream(inputstream);
00045     ZipEntry entry;
00046     while ( (entry = zipFile.getNextEntry()) != null) {
00047       String completeName = entry.getName();
00048       // get file directory and test if exist.
00049       String plateformFileName = FileUtilities.convertFileNameToSystem(completeName);
00050       if (entry.isDirectory()) {
00051           File dirToCreate = new File(unZipDirectory+plateformFileName);
00052           if (!dirToCreate.exists()) {
00053                   dirToCreate.mkdirs();
00054           }
00055           zipFile.closeEntry();
00056           continue;
00057       }
00058       int index = completeName.lastIndexOf("/");
00059       if (index != -1)  {
00060         File repToCreate = new File(unZipDirectory+plateformFileName.substring(0, index));
00061         if (!repToCreate.exists())  {
00062           repToCreate.mkdirs();
00063         }
00064       }
00065       FileOutputStream stream = new FileOutputStream(unZipDirectory+plateformFileName);
00066       try {
00067         int read_bytes = 0;
00068         byte[] buffer = new byte[2048];
00069         while ( (read_bytes = zipFile.read(buffer)) != -1) {
00070           stream.write(buffer,0, read_bytes);
00071         }
00072       } finally {
00073         stream.close();
00074       }
00075       zipFile.closeEntry();
00076     }
00077     zipFile.close();
00078   }
00079   
00080   public static void unzipEntryFromDataToDestFile(InputStream inputstream, String entryName, String destFile) throws IOException {
00081       ZipInputStream zipFile = new ZipInputStream(inputstream);
00082       ZipEntry entry;
00083       while ( (entry = zipFile.getNextEntry()) != null) {
00084         String completeName = entry.getName();
00085         if (completeName.equals(entryName))  {
00086             FileOutputStream stream = new FileOutputStream(destFile);
00087             try {
00088               int read_bytes = 0;
00089               byte[] buffer = new byte[2048];
00090               while ( (read_bytes = zipFile.read(buffer)) != -1) {
00091                 stream.write(buffer,0, read_bytes);
00092               }
00093             } finally {
00094               stream.close();
00095             }
00096         }
00097         zipFile.closeEntry();
00098       }
00099       zipFile.close();
00100   }
00101 
00102 
00103   public static byte[] getZipEntryFromStreal(InputStream inputstream, String entryName) throws IOException  {
00104     ZipInputStream zipFile = new ZipInputStream(inputstream);
00105     ZipEntry entry;
00106     byte[] outData = null;
00107     while ( (entry = zipFile.getNextEntry()) != null) {
00108       String completeName = entry.getName();
00109       if (completeName.equals(entryName))  {
00110         ByteArrayOutputStream stream = new ByteArrayOutputStream();
00111         try {
00112           int read_bytes = 0;
00113           byte[] buffer = new byte[2048];
00114           while ( (read_bytes = zipFile.read(buffer)) != -1) {
00115             stream.write(buffer,0, read_bytes);
00116           }
00117         } finally {
00118           stream.close();
00119         }
00120         outData = stream.toByteArray();
00121         zipFile.closeEntry();
00122         break;
00123       }
00124       zipFile.closeEntry();
00125     }
00126     zipFile.close();
00127     return outData;
00128   }
00129 
00130 }

Generated on Mon Dec 4 11:03:31 2006 for OpenMobileIS by  doxygen 1.5.1-p1