00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00076 while ( (read_bytes = zipFile.read(buffer, 0, buffer.length)) != -1) {
00077 bos.write(buffer,0, read_bytes);
00078 }
00079
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 }