Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

ZipUtilities.java

00001 /*
00002  *        OpenMobileIS - a free Java Framework for mobile applications
00003  *
00004  *   Copyright (C) 2002  Philippe Delrieu.
00005  *
00006  *   This program is free software; you can redistribute it and/or
00007  *   modify it under the terms of the GNU General Public
00008  *   License as published by the Free Software Foundation; either
00009  *   version 2 of the License, or (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *   General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this library; if not, write to the Free
00018  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  * 
00020  * Philippe Delrieu kept the rigth to distribute all code Copyrighted by philippe Delrieu
00021  *  under other licence term even commercial one.
00022  *  
00023  *  Modifications :
00024  *  2002 Creation P.Delrieu
00025  * 
00026  */
00027 
00028 package org.openmobileis.common.util.file;
00029 
00030 import java.io.*;
00031 import java.util.zip.*;
00032 
00040 public class ZipUtilities {
00041 
00045   public static void unZipData(InputStream inputstream, String unZipDirectory) throws IOException  {
00046     ZipInputStream zipFile = new ZipInputStream(inputstream);
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       FileOutputStream stream = new FileOutputStream(unZipDirectory+plateformFileName);
00068       try {
00069         int read_bytes = 0;
00070         byte[] buffer = new byte[2048];
00071         while ( (read_bytes = zipFile.read(buffer)) != -1) {
00072           stream.write(buffer,0, read_bytes);
00073         }
00074       } finally {
00075         stream.close();
00076       }
00077       zipFile.closeEntry();
00078     }
00079     zipFile.close();
00080   }
00081   
00082   public static void unzipEntryFromDataToDestFile(InputStream inputstream, String entryName, String destFile) throws IOException {
00083       ZipInputStream zipFile = new ZipInputStream(inputstream);
00084       ZipEntry entry;
00085       while ( (entry = zipFile.getNextEntry()) != null) {
00086         String completeName = entry.getName();
00087         if (completeName.equals(entryName))  {
00088             FileOutputStream stream = new FileOutputStream(destFile);
00089             try {
00090               int read_bytes = 0;
00091               byte[] buffer = new byte[2048];
00092               while ( (read_bytes = zipFile.read(buffer)) != -1) {
00093                 stream.write(buffer,0, read_bytes);
00094               }
00095             } finally {
00096               stream.close();
00097             }
00098         }
00099         zipFile.closeEntry();
00100       }
00101       zipFile.close();
00102   }
00103 
00104 
00105   public static byte[] getZipEntryFromStreal(InputStream inputstream, String entryName) throws IOException  {
00106     ZipInputStream zipFile = new ZipInputStream(inputstream);
00107     ZipEntry entry;
00108     byte[] outData = null;
00109     while ( (entry = zipFile.getNextEntry()) != null) {
00110       String completeName = entry.getName();
00111       if (completeName.equals(entryName))  {
00112         ByteArrayOutputStream stream = new ByteArrayOutputStream();
00113         try {
00114           int read_bytes = 0;
00115           byte[] buffer = new byte[2048];
00116           while ( (read_bytes = zipFile.read(buffer)) != -1) {
00117             stream.write(buffer,0, read_bytes);
00118           }
00119         } finally {
00120           stream.close();
00121         }
00122         outData = stream.toByteArray();
00123         zipFile.closeEntry();
00124         break;
00125       }
00126       zipFile.closeEntry();
00127     }
00128     zipFile.close();
00129     return outData;
00130   }
00131 
00132 }

Generated on Wed Dec 14 21:05:36 2005 for OpenMobileIS by  doxygen 1.4.4