import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import com.sun.media.jai.codec.ByteArraySeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGEncodeParam;
import com.sun.media.jai.codec.SeekableStream;
/**
* @author humf
*
*/
public class IdepUtil {
public static String serializer(Object ob) throws Exception{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(ob);
String serStr = byteArrayOutputStream.toString("ISO-8859-1");
serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
objectOutputStream.close();
byteArrayOutputStream.close();
return serStr;
}
public static Object unSerializer(String xml) throws Exception{
String redStr = java.net.URLDecoder.decode(xml, "UTF-8");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
Object ob = objectInputStream.readObject();
objectInputStream.close();
byteArrayInputStream.close();
return ob;
}
@SuppressWarnings("unchecked")
public static Map zipSerializer(String zipPath) throws Exception{
File file = new File(zipPath);
ZipFile zip = new ZipFile(file);
Enumeration entries = (Enumeration)zip.entries();
Map map = new HashMap();
while(entries.hasMoreElements()){
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
InputStream inputStream = zip.getInputStream(entry);
ByteArrayOutputStream out=new ByteArrayOutputStream();
byte[] buffer=new byte[1024*4];
int n=0;
while ( (n=inputStream.read(buffer)) !=-1) {
out.write(buffer,0,n);
}
// byte[] b = out.toByteArray();
byte[] b = tifToJpg(out.toByteArray());
//String s = IdepUtil.serializer(b);
String s = byte2hex(b);
map.put(entryName, s);
out.close();
inputStream.close();
//System.out.println(s);
}
return map;
}
public static byte[] tifToJpg(byte[] b) throws IOException{
SeekableStream stream = new ByteArraySeekableStream(b);
PlanarImage in = JAI.create("stream", stream);
// OutputStream os = null;
// os = new FileOutputStream(output);
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGEncodeParam param = new JPEGEncodeParam();
byte[] ret = null;
ImageEncoder enc = ImageCodec.createImageEncoder("JPEG", os, param);
try {
enc.encode(in);
os.flush();
ret = os.toByteArray();
os.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
private static String byte2hex(byte[] b){ // 二进制转字符串
StringBuffer sb = new StringBuffer();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1){
sb.append("0" + stmp);
}else{
sb.append(stmp);
}
}
return sb.toString();
}
}