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