wwwchenbing 发表于 2013-1-15 02:37:45

Java 压缩/解压本地字节流文件 ,Flex 解压缩本地字节流文件方式例子

Java 压缩..........
import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.HashMap;import java.util.zip.Deflater;import java.util.zip.Inflater;import flex.messaging.io.SerializationContext;import flex.messaging.io.amf.Amf3Output;public class Test {/*** @param args*/public static void main(String[] args) {SerializationContext serializationContext = new SerializationContext();Amf3Output amfOut = new Amf3Output(serializationContext);ByteArrayOutputStream outStream = new ByteArrayOutputStream();DataOutputStream dataOutStream = new DataOutputStream(outStream);amfOut.setOutputStream(dataOutStream);HashMap<String, Object> map = new HashMap<String, Object>();//      Double[] arr = new Double;//      for (int index = 0; index < 10000; index++) {//            arr = Math.random();//      }//      map.put("arr", arr);map.put("name", "weni");map.put("ad", "27");map.put("cc", "12");map.put("bb", "33");map.put("ee", "44");try {amfOut.writeObject(map);dataOutStream.flush();} catch (IOException e) {e.printStackTrace();}byte[] messageBytes = outStream.toByteArray();try {FileOutputStream os;os = new FileOutputStream("C://test.bin");messageBytes = compressBytes(messageBytes); // 将数据进行压缩os.write(messageBytes);os.flush();os.close();System.out.println("OK");} catch (Exception e) {System.out.println("error:" + e);}}private static int cachesize = 1024;private static Deflater compresser = new Deflater();public static byte[] compressBytes(byte input[]) {compresser.reset();compresser.setInput(input);compresser.finish();byte output[] = new byte;ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);try {byte[] buf = new byte;int got;while (!compresser.finished()) {got = compresser.deflate(buf);o.write(buf, 0, got);}output = o.toByteArray();} finally {try {o.close();} catch (IOException e) {e.printStackTrace();}}return output;}}

Java 解压..........
import java.io.BufferedInputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.zip.Inflater;import flex.messaging.io.SerializationContext;import flex.messaging.io.amf.ASObject;import flex.messaging.io.amf.Amf3Input;public class Test2 {/*** @param args*/public static void main(String[] args) {SerializationContext serializationContext = new SerializationContext();Amf3Input amfInput = new Amf3Input(serializationContext);ByteArrayInputStream inputStream ;ASObject asObj=new ASObject();try {inputStream= new ByteArrayInputStream(decompressBytes(readFile("C:\\test.bin")));amfInput.setInputStream(inputStream);asObj=(ASObject) amfInput.readObject();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.print("ASObject's size is   "+asObj.size());}public static byte[] readFile(String filename) throws IOException{File file =new File(filename);if(filename==null || filename.equals("")){throw new NullPointerException("无效的文件路径");}long len = file.length();byte[] bytes = new byte[(int)len];BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));int r = bufferedInputStream.read( bytes );if (r != len)throw new IOException("读取文件不正确");bufferedInputStream.close();return bytes;}private static int cachesize = 1024;private static Inflater decompresser = new Inflater();public static byte[] decompressBytes(byte input[]) {byte output[] = new byte;decompresser.reset();decompresser.setInput(input);ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);try {byte[] buf = new byte;int got;while (!decompresser.finished()) {got = decompresser.inflate(buf);o.write(buf, 0, got);}output = o.toByteArray();} catch (Exception e) {e.printStackTrace();} finally {try {o.close();} catch (IOException e) {e.printStackTrace();}}return output;}}Flex 解压....package {import flash.display.Sprite;import flash.events.Event;import flash.net.URLLoader;import flash.net.URLLoaderDataFormat;import flash.net.URLRequest;import flash.utils.ByteArray;public class AMF3Test extends Sprite{private var loader:URLLoader;public function AMF3Test(){loader=new URLLoader();loader.load(new URLRequest("C://test.bin"));loader.addEventListener(Event.COMPLETE,onComplete);loader.dataFormat=URLLoaderDataFormat.BINARY;}private function onComplete(evt:Event):void{var byte:ByteArray=loader.data as ByteArray;byte.uncompress() //将数据进行解压缩var obj:Object=byte.readObject();}} }
页: [1]
查看完整版本: Java 压缩/解压本地字节流文件 ,Flex 解压缩本地字节流文件方式例子