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
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
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 }